/** * Decodes bytes from base 64, returning a string. * * @param in bytes to decode * @return decoded string */ public static String decodeToString(byte[] in) { return getString(decode(in)); }
/** * Decodes a string from base 64, returning bytes. * * @param in string to decode * @return decoded bytes */ public static byte[] decode(String in) { return decode(getBytes(in)); }
/** * Decodes bytes from base 64, returning a string. * * @param in bytes to decode * @return decoded string */ public static String decodeToString(byte[] in) { return getString(decode(in)); }
/** * Decodes a string from base 64, returning bytes. * * @param in string to decode * @return decoded bytes */ public static byte[] decode(String in) { return decode(getBytes(in)); }
/** * Add Base64-encoded characters to be decoded. This is not a trivial * operation for two reasons: any characters that are not valid for * Base64-encoding must be ignored, and we can only decode groups of 4 * characters. So, when data is received, we remove any invalid characters * and then strip off any trailing characters that don't fit in the * 4-character groups. Those trailing characters will be prefixed to the * next set of data, and hopefully we will have none left over when the * writer is closed. * * @throws IllegalStateException * if called after close(). * @throws IOException * from the inner OutputStream. */ public void write(String data) throws IOException { if (!open) { throw new IllegalStateException("Stream has already been closed."); } String buffer = pattern.matcher(residual + data).replaceAll(""); int usableLength = buffer.length() - buffer.length() % 4; stream.write(Base64.decode((buffer.substring(0, usableLength)))); residual = buffer.substring(usableLength); }
/** * Add Base64-encoded characters to be decoded. This is not a trivial * operation for two reasons: any characters that are not valid for * Base64-encoding must be ignored, and we can only decode groups of 4 * characters. So, when data is received, we remove any invalid characters * and then strip off any trailing characters that don't fit in the * 4-character groups. Those trailing characters will be prefixed to the * next set of data, and hopefully we will have none left over when the * writer is closed. * * @throws IllegalStateException * if called after close(). * @throws IOException * from the inner OutputStream. */ public void write(String data) throws IOException { if (!open) { throw new IllegalStateException("Stream has already been closed."); } String buffer = pattern.matcher(residual + data).replaceAll(""); int usableLength = buffer.length() - buffer.length() % 4; stream.write(Base64.decode((buffer.substring(0, usableLength)))); residual = buffer.substring(usableLength); }
/** * Use the key to produce a clear text String from the cipherText. If no key * is provided, or if no type is specified, just return the text as is. */ public static String decipher(String key, String cipherText, String cipherType) { if (key == null || key == "") { return cipherText; } if (cipherText == null) { return null; } if (cipherType == null || cipherType == "") { return cipherText; } else if ("1".equalsIgnoreCase(cipherType)) { byte[] keyBytes = convertKeyToByteArray(key); byte[] cipherBytes = Base64.decode(cipherText); sanityCheckOnCipherBytes(cipherText, cipherBytes); byte[] clearTextBytes = applyCipher(keyBytes, cipherBytes); return convertByteArrayToClearText(clearTextBytes); } else { throw new IllegalArgumentException("Unrecognized cipher type: '" + cipherType + "'"); } }
/** * Use the key to produce a clear text String from the cipherText. If no key * is provided, or if no type is specified, just return the text as is. */ public static String decipher(String key, String cipherText, String cipherType) { if (key == null || key == "") { return cipherText; } if (cipherText == null) { return null; } if (cipherType == null || cipherType == "") { return cipherText; } else if ("1".equalsIgnoreCase(cipherType)) { byte[] keyBytes = convertKeyToByteArray(key); byte[] cipherBytes = Base64.decode(cipherText); sanityCheckOnCipherBytes(cipherText, cipherBytes); byte[] clearTextBytes = applyCipher(keyBytes, cipherBytes); return convertByteArrayToClearText(clearTextBytes); } else { throw new IllegalArgumentException("Unrecognized cipher type: '" + cipherType + "'"); } }
m_elementContent.toString() .replaceAll("\\s", ""); byte elementBytes[] = Base64.decode(elementStr); os.write(elementBytes); os.close();
m_elementContent.toString() .replaceAll("\\s", ""); byte elementBytes[] = Base64.decode(elementStr); os.write(elementBytes); os.close();
new FileOutputStream(m_binaryContentTempFile); byte elementBytes[] = Base64.decode(m_elementContent.toInputStream()); os.write(elementBytes); os.close();
new FileOutputStream(m_binaryContentTempFile); byte elementBytes[] = Base64.decode(m_elementContent.toInputStream()); os.write(elementBytes); os.close();
@Test public void testDecodeByteArray() { assertTrue(sameBytes(FOO_BYTES, Base64.decode(FOO_BYTES_ENCODED))); }
@Test public void testDecodeString() { assertTrue(sameBytes(FOO_BYTES, Base64.decode(FOO_STRING_ENCODED))); }