Issue Details (XML | Word | Printable)

Key: CODEC-69
Type: Improvement Improvement
Status: Closed Closed
Resolution: Fixed
Priority: Minor Minor
Assignee: Jochen Wiedmann
Reporter: Julius Davies
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Commons Codec

Streaming Base64

Created: 18/Jun/08 04:44 AM   Updated: 16/Jul/09 03:47 AM
Return to search
Component/s: None
Affects Version/s: None
Fix Version/s: 1.4

Time Tracking:
Not Specified

File Attachments:
  Size
Text File Licensed for inclusion in ASF works streaming-base64.patch 2008-06-18 04:50 AM Julius Davies 67 kB
Issue Links:
Incorporates
 
Reference
 

Resolution Date: 18/Jun/08 07:18 PM


 Description  « Hide
Would be nice to have Base64InputStream and Base64OutputStream, with both supporting ENCODE or DECODE modes.

 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Julius Davies added a comment - 18/Jun/08 04:50 AM
Patch introduces Base64InputStream and Base64OutputStream. They wrap any pre-existing InputStream or OutputStream, like so:

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/


Julius Davies added a comment - 18/Jun/08 04:52 AM
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!)

Jochen Wiedmann added a comment - 18/Jun/08 07:18 PM
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.

Julius Davies added a comment - 19/Jun/08 04:52 PM
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.


Julius Davies added a comment - 19/Jun/08 04:59 PM
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.


Julius Davies added a comment - 19/Jun/08 05:27 PM
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
2. read a byte... nothing emitted
3. read a byte... 4 bytes emitted.
4. read a byte... nothing emitted.
5. EOF! 4 bytes emitted (e.g. AA==).

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 ?


Sebb added a comment - 19/Jun/08 06:27 PM
Base64 constructors have been made public:

URL: http://svn.apache.org/viewvc?rev=669616&view=rev
Log:
CODEC-69 (part) - make Base64 constructors public


Sergei S. Ivanov added a comment - 19/Jun/08 06:56 PM
Linked this issue and CODEC-8