简体   繁体   English

Java - 有没有办法从 base64 输入确定文件大小?

[英]Java - Is there a way to determine file size from a base64 input?

We have a java program where the input is a base64 string version of the file.我们有一个 java 程序,其中输入是文件的 base64 字符串版本。 Files would be processed differently depending on their sizes so we have to find a way to determine its size based on its base64 input.文件将根据其大小进行不同的处理,因此我们必须找到一种方法根据其 base64 输入确定其大小。

is there a way to do this?有没有办法做到这一点? I'm thinking of recreating the file from the base64 then get the size but that would mean having to store it temporarily.我正在考虑从 base64 重新创建文件然后获取大小,但这意味着必须暂时存储它。 We don't want that.我们不想那样。 Whats the best way to do this?什么是最好的方法来做到这一点?

We are using Java 8我们正在使用 Java 8

Basically, yes.基本上,是的。 In basis, Base64 encodes 3 bytes using 4 characters.在基础上,Base64 使用 4 个字符编码 3 个字节。 However, you must tackle 2 additional major issues:但是,您必须解决另外两个主要问题:

  • Base64 is sometimes split up into lines; Base64 有时会分成几行; the spec says whitespace is fine, and must be ignored.规范说空格很好,必须忽略。 The 'new line' is one character (or sometimes two) that therefore must not be counted. “新行”是一个字符(有时是两个),因此不能被计算在内。
  • What if the file is not an exact multiple of 3?如果文件不是 3 的精确倍数怎么办? Base64 handles this using a padding algorithm. Base64 使用填充算法处理此问题。 You always get the Base64 characters in sets of 4, but it is possible that the last set-of-4 encodes only 1 or 2 bytes instead of the usual 3. The = sign is used for padding.您总是以 4 个为一组获得 Base64 字符,但最后一组 4 个字符可能仅编码 1 或 2 个字节,而不是通常的 3 个。 =符号用于填充。

Both of these issues can be addressed fairly easily:这两个问题都可以很容易地解决:

It's not hard to loop over a string and increment a counter unless the character at that position in the string is whitespace.除非字符串中该位置的字符是空格,否则循环遍历字符串并增加计数器并不难。

Then multiply by 3 and divide by 4.然后乘以3并除以4。

Then subtract 2 if the string ends in == .如果字符串以==结尾,则减去 2。 If it ends in = , subtract 1.如果以=结尾,则减 1。

You can count = signs during your loop.您可以在循环中计算=符号。

int countBase64Size(String in) {
  int count = 0;
  int pad = 0;
  for (int i = 0; i < in.length(); i++) {
    char c = in.charAt(i);
    if (c == '=') pad++;
    if (!Character.isWhitespace(c)) count++;
  }
  return (count * 3 / 4) - pad;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM