|
Related to CODEC-8, but I wasn't brave enough to try a general approach, and I need streaming Base64 urgently! (500MB+ files sometimes encrypted/decrypted in not-yet-commons-ssl!)
Applied. There is, of course, CODEC-8, but IMO this is so far the best piece of code that commons-codec has seen, so that we may think about the API later on.
Thanks so much for accepting this patch!
I noticed some minor typos in some of my javadocs, and a spurious (byte) cast.... I'll probably upload an additional patch in a few days. Oops - Base64 constructors are not public. In previous version there was no declared constructor, which is identical to having "public Base64() {}".
The constructors need to be public to support anyone who is using the "BinaryEncoder" or "BinaryDecoder" interfaces instead of using Base64 directly. To reply to Sebb's comment on CODEC-8:
Base64 encoding will only emit bytes in chunks of 4. For example: 1. read a byte... nothing emitted So even if a "lineLength" of 75 is passed to the constructor, the lines will still be 76 characters long. Not because we ever actually round the number up, but because of this part of the encode() method (the += 4 in particular): buf[pos++] = intToBase64[(x >> 18) & 0x3f]; buf[pos++] = intToBase64[(x >> 12) & 0x3f]; buf[pos++] = intToBase64[(x >> 6) & 0x3f]; buf[pos++] = intToBase64[x & 0x3f]; currentLinePos += 4; if (lineLength > 0 && lineLength <= currentLinePos) { System.arraycopy(lineSeparator, 0, buf, pos, lineSeparator.length); pos += lineSeparator.length; currentLinePos = 0; } I can't think of a good name for this magic number! Maybe ENCODE_UNIT ? Base64 constructors have been made public:
URL: http://svn.apache.org/viewvc?rev=669616&view=rev Linked this issue and CODEC-8
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
InputStream in = new FileInputStream("/path/to/file.base64");
in = new Base64InputStream(in, false); // false == DECODE
The guts of Base64 have been ripped out and rearranged to better suit this way of working, but all the JUnit tests still pass!
Thanks to "commons" project in ws.apache.org for much of this code.
http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/