我想使用 Base64.java 来编码和解码文件。 Encode.wrap(InputStream)
和 decode.wrap(InputStream)
工作但运行缓慢。所以我使用了以下代码。
public static void decodeFile(String inputFileName,
String outputFileName)
throws FileNotFoundException, IOException {
Base64.Decoder decoder = Base64.getDecoder();
InputStream in = new FileInputStream(inputFileName);
OutputStream out = new FileOutputStream(outputFileName);
byte[] inBuff = new byte[BUFF_SIZE]; //final int BUFF_SIZE = 1024;
byte[] outBuff = null;
while (in.read(inBuff) > 0) {
outBuff = decoder.decode(inBuff);
out.write(outBuff);
}
out.flush();
out.close();
in.close();
}
但是,它总是抛出
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Input byte array has wrong 4-byte ending unit
at java.util.Base64$Decoder.decode0(Base64.java:704)
at java.util.Base64$Decoder.decode(Base64.java:526)
at Base64Coder.JavaBase64FileCoder.decodeFile(JavaBase64FileCoder.java:69)
...
在我将 final int BUFF_SIZE = 1024;
更改为 final int BUFF_SIZE = 3*1024;
后,代码可以正常工作。由于“BUFF_SIZE”也用于对文件进行编码,我认为编码的文件有问题(1024 % 3 = 1,这意味着在文件中间添加了填充)。
此外,正如@Jon Skeet 和@Tagir Valeev 提到的,我不应该忽略 InputStream.read()
的返回值。所以,我修改了如下代码。
(但是,我不得不提到代码确实比使用 wrap()
运行得快得多。我注意到了速度差异,因为我已经编码并大量使用了 Base64.encodeFile()/decodeFile( ) 早在 jdk8 发布之前。现在,我的 buffed jdk8 代码运行速度与我的原始代码一样快。所以,我不知道 wrap()
发生了什么... )
public static void decodeFile(String inputFileName,
String outputFileName)
throws FileNotFoundException, IOException
{
Base64.Decoder decoder = Base64.getDecoder();
InputStream in = new FileInputStream(inputFileName);
OutputStream out = new FileOutputStream(outputFileName);
byte[] inBuff = new byte[BUFF_SIZE];
byte[] outBuff = null;
int bytesRead = 0;
while (true)
{
bytesRead = in.read(inBuff);
if (bytesRead == BUFF_SIZE)
{
outBuff = decoder.decode(inBuff);
}
else if (bytesRead > 0)
{
byte[] tempBuff = new byte[bytesRead];
System.arraycopy(inBuff, 0, tempBuff, 0, bytesRead);
outBuff = decoder.decode(tempBuff);
}
else
{
out.flush();
out.close();
in.close();
return;
}
out.write(outBuff);
}
}
特别感谢@Jon Skeet 和@Tagir Valeev。
最佳答案
我强烈怀疑问题是您忽略了 InputStream.read
的返回值,而不是检查流的结尾。所以这个:
while (in.read(inBuff) > 0) {
// This always decodes the *complete* buffer
outBuff = decoder.decode(inBuff);
out.write(outBuff);
}
应该是
int bytesRead;
while ((bytesRead = in.read(inBuff)) > 0) {
outBuff = decoder.decode(inBuff, 0, bytesRead);
out.write(outBuff);
}
不过,我预计这会比使用wrap
更快。
关于java - 使用 Java8 Base64 解码器的 IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32858758/