I have to encode in base64 an array of bytes (that is a byte stream of a Zipfile) but the array is too big. So I split the array in two parts, encode the first half, then the second one, and in the end I append the second part to the first part. The thing is that sometimes when i decode the result the zipfile seems to be corrupted.
FileInputStream fis = new FileInputStream(new File(filePath));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while((len = fis.read(buffer)) >= 0) {
baos.write(buffer);
}
byte[] bArray = baos.toByteArray();
int bArrayLength = bArray.length;
byte[] part1 = new byte[bArray.length / 2];
byte[] part2 = new byte[ (bArrayLength%2 == 0) ? (bArray.length / 2) : (bArray.length / 2 + 1)];
System.arraycopy(bArray, 0, part1, 0, part1.length);
System.arraycopy(bArray, part1.length, part2, 0, part2.length);
resultArray[0] = Base64.encodeBytes(part1);
resultArray[1] = Base64.encodeBytes(part2);
What is the problem? Is that split that causes the issue?
This probably doesn’t work because of padding. The Base64 algorithm pads its output with a == if its encoding results in a trailing group (of 24 bits) that contains only 1 byte, or = if it contains only 2 bytes. That means you cannot just add two encoded strings together and expect to get the correct result.
But how do you know the array is too big — do you get an OutOfMemoryException?
You may try to split not exactly at the halfway point, but choose a different point until you no longer get a trailing = for that part.
See also http://en.wikipedia.org/wiki/Base64.