|
Right, that's exactly what I meant and wrote. The codec could encode bytes TO Strings (with a String encode(byte[]) method) and decode bytes FROM Strings (with a byte[] decode(String) method). These Strings would only contain characters from the base 64 character set (which is a subset of ASCII).
I've had a need for this in the past as well. This will happen much quicker if you submit a patch for code and unit tests with your request
No patches to date, so moving out of the 1.4 release plan.
codec59.patch attached. It introduces two new methods:
public String Base64.encodeToString( byte[] b ) public byte[] Base64.decodeFromString( String s ) The String the first method produces chunks the output into 76-character lines. If consumers want very-long single lines instead they can run .replaceAll("\r\n", "") against the output. (I chose chunking as default behaviour because starting with a very-long-line and breaking it into chunks is more of a pain). Even if this patch is not accepted, I strongly recommend taking in these 5 lines, since they fix a very subtle and rare bug (newly introduced in @@ -698,7 +715,11 @@
len += 4 - mod;
}
if (isChunked) {
- len += (1 + (len / CHUNK_SIZE)) * CHUNK_SEPARATOR.length;
+ boolean lenChunksPerfectly = len % CHUNK_SIZE == 0;
+ len += (len / CHUNK_SIZE) * CHUNK_SEPARATOR.length;
+ if (!lenChunksPerfectly) {
+ len += CHUNK_SEPARATOR.length;
+ }
}
About the "5 lines" fix: can you provide a test that shows the bug. It would be good to see a test to address this fix specifically.
The tests already included in the patch cover the bug. If you accept everything in the patch except Base64.java, I think you just need to add a test like this:
byte[] result Base64.encodeBase64(Base64TestData.DECODED, true); Because Base64TestData.DECODED encodes into a multiple of 76 the original code creates an array that is slightly too big to hold it. The result array ends up having two 0 bytes at the very end. Attached file (codec59-but-only-after-codec81.patch) seems to work!
Attached patch introduces following "String" methods to Base64.java:
Non-Static methods: Static methods: The patch can only be applied after both of the Attached codec59-2009-07-28.patch.
Introduces the String oriented methods previously mentioned in comments here. Also improves StringUtils handling of null input, as well as Base64.decode() of null. The reset() calls added to Base64 are pretty important I think in case people try to re-use Base64 objects, e.g.: Base64 b64 = new Base64();
b64.encode( someThing );
b64.encode( someOtherThing );
This will only work with the reset() calls I've added as part of this patch. Patch applied with one added test code line to keep line code coverage at 100%. Branch coverage up to 92% from 91%.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
CODEC-58the term character refers 8-bit values in the RFC and 16-bit values in Java. The encoding can never receive a String, since it has no idea about the encoding of the String and the algorithm encodes arbitrary bytes. Same applies to the result of a decoding. The result of an encoding and the input of the decoding could be Strings in ASCII encoding.