/** * Encode a group of bytes and remove them from the buffer. If the input * stream has more data, we need to limit the encoding to a multiple of 3, * to avoid prematurely padding the result with equals signs. * * @param howMany * how many bytes should be encoded and remove from the buffer. * @return the Base64-encoded characters. */ private String encodeBytesFromBuffer(int howMany) { String result; if (innerStreamHasMoreData) { howMany = howMany - howMany % 3; } if (howMany == 0) { return ""; } byte[] encodeBuffer = new byte[howMany]; System.arraycopy(buffer, 0, encodeBuffer, 0, howMany); result = Base64.encodeToString(encodeBuffer); bytesInBuffer -= howMany; if (bytesInBuffer != 0) { System.arraycopy(buffer, howMany, buffer, 0, bytesInBuffer); } return result; }
/** * Encode a group of bytes and remove them from the buffer. If the input * stream has more data, we need to limit the encoding to a multiple of 3, * to avoid prematurely padding the result with equals signs. * * @param howMany * how many bytes should be encoded and remove from the buffer. * @return the Base64-encoded characters. */ private String encodeBytesFromBuffer(int howMany) { String result; if (innerStreamHasMoreData) { howMany = howMany - howMany % 3; } if (howMany == 0) { return ""; } byte[] encodeBuffer = new byte[howMany]; System.arraycopy(buffer, 0, encodeBuffer, 0, howMany); result = Base64.encodeToString(encodeBuffer); bytesInBuffer -= howMany; if (bytesInBuffer != 0) { System.arraycopy(buffer, howMany, buffer, 0, bytesInBuffer); } return result; }
/** * Use the key to produce a ciphered String from the clearText. */ public static String encipher(String key, String clearText) { if (key == null) { throw new NullPointerException("key may not be null"); } if (key == "") { return clearText; } if (clearText == null) { return null; } byte[] keyBytes = convertKeyToByteArray(key); byte[] clearTextBytes = convertClearTextToByteArray(clearText); byte[] cipherBytes = applyCipher(keyBytes, clearTextBytes); return Base64.encodeToString(cipherBytes); }
/** * Use the key to produce a ciphered String from the clearText. */ public static String encipher(String key, String clearText) { if (key == null) { throw new NullPointerException("key may not be null"); } if (key == "") { return clearText; } if (clearText == null) { return null; } byte[] keyBytes = convertKeyToByteArray(key); byte[] clearTextBytes = convertClearTextToByteArray(clearText); byte[] cipherBytes = applyCipher(keyBytes, clearTextBytes); return Base64.encodeToString(cipherBytes); }
binaryContent.setTextContent(Base64.encodeToString(bytes.toByteArray()));
@Test public void testEncodeToStringInputStream() { assertEquals(FOO_STRING_ENCODED, Base64.encodeToString(new ByteArrayInputStream(FOO_BYTES))); }
@Test public void testEncodeToStringByteArray() { assertEquals(FOO_STRING_ENCODED, Base64.encodeToString(FOO_BYTES)); }