public void write(int c) throws IOException { inbuf[i] = c; i++; if (i == 3) { super.write(toBase64[(inbuf[0] & 0xFC) >> 2]); super.write(toBase64[((inbuf[0] & 0x03) << 4) | ((inbuf[1] & 0xF0) >> 4)]); super.write(toBase64[((inbuf[1] & 0x0F) << 2) | ((inbuf[2] & 0xC0) >> 6)]); super.write(toBase64[inbuf[2] & 0x3F]); col += 4; i = 0; if (col >= 76) { super.write('\n'); col = 0; } } }
public void flush() throws IOException { if (i == 1) { super.write(toBase64[(inbuf[0] & 0xFC) >> 2]); super.write(toBase64[(inbuf[0] & 0x03) << 4]); super.write('='); super.write('='); } else if (i == 2) { super.write(toBase64[(inbuf[0] & 0xFC) >> 2]); super.write(toBase64[((inbuf[0] & 0x03) << 4) | ((inbuf[1] & 0xF0) >> 4)]); super.write(toBase64[(inbuf[1] & 0x0F) << 2]); super.write('='); } if (col > 0) { super.write('\n'); col = 0; } }
/** * Closes the stream, this MUST be called to ensure proper padding is written to the end of the * output stream. * * @exception IOException if an I/O error occurs */ public void close() throws IOException { // Handle leftover bytes if (charCount % 3 == 1) { // one leftover int lookup = (carryOver << 4) & 63; out.write(chars[lookup]); out.write('='); out.write('='); } else if (charCount % 3 == 2) { // two leftovers int lookup = (carryOver << 2) & 63; out.write(chars[lookup]); out.write('='); } super.close(); }