Java 8 added Base64 with its nested classes Encoder and Decoder. These both have methods to wrap an OutputStream / InputStream respectively. However, base64 is text; the alphabets exist out of ASCII characters only. This is (somewhat) acknowledged by the presence of the encodeToString(byte[]) and decode(String) methods. Doesn't it then also make sense to add methods to wrap a Writer and Reader respectively? (Perhaps Appendable / Readable is even better to provide more flexibility.) When reading an exception can be thrown if an unsupported character is encountered.

I have already written a simple example to wrap a StringBuilder in an OutputStream that I then wrap using Base64.Encoder: https://github.com/robtimus/data-url/blob/master/src/main/java/com/github/robtimus/net/protocol/data/DataURLs.java. This could probably add some validation that characters are limited to the actual alphabet in the convert method, but I was a bit lazy there.

If an Appendable is wrapped the flush and close methods should delegate to the wrapped object if that implements Flushable / Closeable respectively.

Reply via email to