I wanted to use Base64.java to encode and decode files. Encode.wrap(InputStream) and decode.wrap(InputStream) worked, but they ran slowly. So I used the following code.
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];
However he always throws
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) ...
After I changed final int BUFF_SIZE = 1024; on final int BUFF_SIZE = 3*1024; The code worked. Since "BUFF_SIZE" is also used to encode the file, I believe that something is wrong with the encoded file (1024% 3 = 1, which means that gaskets are added in the middle of the file).
Also, as @Jon Skeet and @Tagir Valeev mentioned, I should not ignore the return value from InputStream.read() . So, I changed the code as shown below.
(However, I have to mention that the code is much faster than using wrap() . I noticed a difference in speed because I encoded and heavily used Base64.encodeFile () / decodeFile () long before jdk8 came out. Now my jdk8 encoded code is as fast as my source code. So I don't know what happens with 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); } }
Special thanks to @Jon Skeet and @Tagir Valeev.