/** * Creates a Base64OutputStream such that all data written is either * Base64-encoded or Base64-decoded to the original provided OutputStream. * * @param out OutputStream to wrap. * @param doEncode true if we should encode all data written to us, * false if we should decode. */ public Base64OutputStream(OutputStream out, boolean doEncode) { super(out); this.doEncode = doEncode; this.base64 = new Base64(); }
/** * Creates a Base64OutputStream such that all data written is either * Base64-encoded or Base64-decoded to the original provided OutputStream. * * @param out OutputStream to wrap. * @param doEncode true if we should encode all data written to us, * false if we should decode. * @param lineLength If doEncode is true, each line of encoded * data will contain lineLength characters. * If lineLength <=0, the encoded data is not divided into lines. * If doEncode is false, lineLength is ignored. * @param lineSeparator If doEncode is true, each line of encoded * data will be terminated with this byte sequence (e.g. \r\n). * If lineLength <= 0, the lineSeparator is not used. * If doEncode is false lineSeparator is ignored. */ public Base64OutputStream(OutputStream out, boolean doEncode, int lineLength, byte[] lineSeparator) { super(out); this.doEncode = doEncode; this.base64 = new Base64(lineLength, lineSeparator); }
public static String decode(String encoded) { if (encoded == null) { return null; } byte[] decoded = new Base64().decode(encoded.getBytes()); return new String(decoded); }
public static String encode(String s) { if (s == null) { return null; } byte[] encoded = new Base64().encode(s.getBytes()); return new String(encoded); }
/** * Decodes Base64 data into octets * * @param base64Data Byte array containing Base64 data * @return Array containing decoded data. */ public static byte[] decodeBase64(byte[] base64Data) { if (base64Data == null || base64Data.length == 0) { return base64Data; } Base64 b64 = new Base64(); long len = (base64Data.length * 3) / 4; byte[] buf = new byte[(int) len]; b64.setInitialBuffer(buf, 0, buf.length); b64.decode(base64Data, 0, base64Data.length); b64.decode(base64Data, 0, -1); // Notify decoder of EOF. // We have no idea what the line-length was, so we // cannot know how much of our array wasn't used. byte[] result = new byte[b64.pos]; b64.readResults(result, 0, result.length); return result; }