Java Code Examples for java.util.Base64.Encoder#encode()
The following examples show how to use
java.util.Base64.Encoder#encode() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestBase64Golden.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
private static void test1() throws Exception { byte[] src = new byte[] { 46, -97, -35, -44, 127, -60, -39, -4, -112, 34, -57, 47, -14, 67, 40, 18, 90, -59, 68, 112, 23, 121, -91, 94, 35, 49, 104, 17, 30, -80, -104, -3, -53, 27, 38, -72, -47, 113, -52, 18, 5, -126 }; Encoder encoder = Base64.getMimeEncoder(49, new byte[] { 0x7e }); byte[] encoded = encoder.encode(src); Decoder decoder = Base64.getMimeDecoder(); byte[] decoded = decoder.decode(encoded); if (!Objects.deepEquals(src, decoded)) { throw new RuntimeException(); } }
Example 2
Source File: TestBase64Golden.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
private static void test1() throws Exception { byte[] src = new byte[] { 46, -97, -35, -44, 127, -60, -39, -4, -112, 34, -57, 47, -14, 67, 40, 18, 90, -59, 68, 112, 23, 121, -91, 94, 35, 49, 104, 17, 30, -80, -104, -3, -53, 27, 38, -72, -47, 113, -52, 18, 5, -126 }; Encoder encoder = Base64.getMimeEncoder(49, new byte[] { 0x7e }); byte[] encoded = encoder.encode(src); Decoder decoder = Base64.getMimeDecoder(); byte[] decoded = decoder.decode(encoded); if (!Objects.deepEquals(src, decoded)) { throw new RuntimeException(); } }
Example 3
Source File: TestBase64Golden.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 5 votes |
private static void test1() throws Exception { byte[] src = new byte[] { 46, -97, -35, -44, 127, -60, -39, -4, -112, 34, -57, 47, -14, 67, 40, 18, 90, -59, 68, 112, 23, 121, -91, 94, 35, 49, 104, 17, 30, -80, -104, -3, -53, 27, 38, -72, -47, 113, -52, 18, 5, -126 }; Encoder encoder = Base64.getMimeEncoder(49, new byte[] { 0x7e }); byte[] encoded = encoder.encode(src); Decoder decoder = Base64.getMimeDecoder(); byte[] decoded = decoder.decode(encoded); if (!Objects.deepEquals(src, decoded)) { throw new RuntimeException(); } }
Example 4
Source File: TestBase64Golden.java From native-obfuscator with GNU General Public License v3.0 | 5 votes |
private static void test1() throws Exception { byte[] src = new byte[] { 46, -97, -35, -44, 127, -60, -39, -4, -112, 34, -57, 47, -14, 67, 40, 18, 90, -59, 68, 112, 23, 121, -91, 94, 35, 49, 104, 17, 30, -80, -104, -3, -53, 27, 38, -72, -47, 113, -52, 18, 5, -126 }; Encoder encoder = Base64.getMimeEncoder(49, new byte[] { 0x7e }); byte[] encoded = encoder.encode(src); Decoder decoder = Base64.getMimeDecoder(); byte[] decoded = decoder.decode(encoded); if (!Objects.deepEquals(src, decoded)) { throw new RuntimeException(); } }
Example 5
Source File: Base64Test.java From j2objc with Apache License 2.0 | 5 votes |
/** * Tests {@link Decoder#decode(byte[], byte[])} for correctness. */ public void testEncoder_encodeArrayToArray() { Encoder encoder = Base64.getEncoder(); // Empty input assertEquals(0, encoder.encode(new byte[0], new byte[0])); // Test data for non-empty input byte[] input = "abcefghi".getBytes(US_ASCII); String expectedString = "YWJjZWZnaGk="; byte[] encodedBytes = expectedString.getBytes(US_ASCII); // Non-empty input: output array too short byte[] tooShort = new byte[encodedBytes.length - 1]; try { encoder.encode(input, tooShort); fail(); } catch (IllegalArgumentException expected) { } // Non-empty input: output array longer than required byte[] tooLong = new byte[encodedBytes.length + 1]; int tooLongBytesEncoded = encoder.encode(input, tooLong); assertEquals(encodedBytes.length, tooLongBytesEncoded); assertEquals(0, tooLong[tooLong.length - 1]); assertArrayPrefixEquals(tooLong, encodedBytes.length, encodedBytes); // Non-empty input: output array has exact minimum required size byte[] justRight = new byte[encodedBytes.length]; int justRightBytesEncoded = encoder.encode(input, justRight); assertEquals(encodedBytes.length, justRightBytesEncoded); assertArrayEquals(encodedBytes, justRight); }
Example 6
Source File: TestBase64Golden.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
private static void test1() throws Exception { byte[] src = new byte[] { 46, -97, -35, -44, 127, -60, -39, -4, -112, 34, -57, 47, -14, 67, 40, 18, 90, -59, 68, 112, 23, 121, -91, 94, 35, 49, 104, 17, 30, -80, -104, -3, -53, 27, 38, -72, -47, 113, -52, 18, 5, -126 }; Encoder encoder = Base64.getMimeEncoder(49, new byte[] { 0x7e }); byte[] encoded = encoder.encode(src); Decoder decoder = Base64.getMimeDecoder(); byte[] decoded = decoder.decode(encoded); if (!Objects.deepEquals(src, decoded)) { throw new RuntimeException(); } }
Example 7
Source File: TestBase64Golden.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
private static void test1() throws Exception { byte[] src = new byte[] { 46, -97, -35, -44, 127, -60, -39, -4, -112, 34, -57, 47, -14, 67, 40, 18, 90, -59, 68, 112, 23, 121, -91, 94, 35, 49, 104, 17, 30, -80, -104, -3, -53, 27, 38, -72, -47, 113, -52, 18, 5, -126 }; Encoder encoder = Base64.getMimeEncoder(49, new byte[] { 0x7e }); byte[] encoded = encoder.encode(src); Decoder decoder = Base64.getMimeDecoder(); byte[] decoded = decoder.decode(encoded); if (!Objects.deepEquals(src, decoded)) { throw new RuntimeException(); } }
Example 8
Source File: TestBase64Golden.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
private static void test1() throws Exception { byte[] src = new byte[] { 46, -97, -35, -44, 127, -60, -39, -4, -112, 34, -57, 47, -14, 67, 40, 18, 90, -59, 68, 112, 23, 121, -91, 94, 35, 49, 104, 17, 30, -80, -104, -3, -53, 27, 38, -72, -47, 113, -52, 18, 5, -126 }; Encoder encoder = Base64.getMimeEncoder(49, new byte[] { 0x7e }); byte[] encoded = encoder.encode(src); Decoder decoder = Base64.getMimeDecoder(); byte[] decoded = decoder.decode(encoded); if (!Objects.deepEquals(src, decoded)) { throw new RuntimeException(); } }
Example 9
Source File: TestBase64Golden.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
private static void test1() throws Exception { byte[] src = new byte[] { 46, -97, -35, -44, 127, -60, -39, -4, -112, 34, -57, 47, -14, 67, 40, 18, 90, -59, 68, 112, 23, 121, -91, 94, 35, 49, 104, 17, 30, -80, -104, -3, -53, 27, 38, -72, -47, 113, -52, 18, 5, -126 }; Encoder encoder = Base64.getMimeEncoder(49, new byte[] { 0x7e }); byte[] encoded = encoder.encode(src); Decoder decoder = Base64.getMimeDecoder(); byte[] decoded = decoder.decode(encoded); if (!Objects.deepEquals(src, decoded)) { throw new RuntimeException(); } }
Example 10
Source File: TestBase64Golden.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
private static void test1() throws Exception { byte[] src = new byte[] { 46, -97, -35, -44, 127, -60, -39, -4, -112, 34, -57, 47, -14, 67, 40, 18, 90, -59, 68, 112, 23, 121, -91, 94, 35, 49, 104, 17, 30, -80, -104, -3, -53, 27, 38, -72, -47, 113, -52, 18, 5, -126 }; Encoder encoder = Base64.getMimeEncoder(49, new byte[] { 0x7e }); byte[] encoded = encoder.encode(src); Decoder decoder = Base64.getMimeDecoder(); byte[] decoded = decoder.decode(encoded); if (!Objects.deepEquals(src, decoded)) { throw new RuntimeException(); } }
Example 11
Source File: TestBase64Golden.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
private static void test1() throws Exception { byte[] src = new byte[] { 46, -97, -35, -44, 127, -60, -39, -4, -112, 34, -57, 47, -14, 67, 40, 18, 90, -59, 68, 112, 23, 121, -91, 94, 35, 49, 104, 17, 30, -80, -104, -3, -53, 27, 38, -72, -47, 113, -52, 18, 5, -126 }; Encoder encoder = Base64.getMimeEncoder(49, new byte[] { 0x7e }); byte[] encoded = encoder.encode(src); Decoder decoder = Base64.getMimeDecoder(); byte[] decoded = decoder.decode(encoded); if (!Objects.deepEquals(src, decoded)) { throw new RuntimeException(); } }
Example 12
Source File: TestBase64Golden.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
public static void test0(Base64Type type, Encoder encoder, Decoder decoder, String srcFile, String encodedFile) throws Exception { String[] srcLns = Files.readAllLines(Paths.get(SRCDIR, srcFile), DEF_CHARSET) .toArray(new String[0]); String[] encodedLns = Files.readAllLines(Paths.get(SRCDIR, encodedFile), DEF_CHARSET) .toArray(new String[0]); int lns = 0; for (String srcStr : srcLns) { String encodedStr = null; if (type != Base64Type.MIME) { encodedStr = encodedLns[lns++]; } else { while (lns < encodedLns.length) { String s = encodedLns[lns++]; if (s.length() == 0) break; if (encodedStr != null) { encodedStr += DEFAULT_CRLF + s; } else { encodedStr = s; } } if (encodedStr == null && srcStr.length() == 0) { encodedStr = ""; } } System.out.printf("%n src[%d]: %s%n", srcStr.length(), srcStr); System.out.printf("encoded[%d]: %s%n", encodedStr.length(), encodedStr); byte[] srcArr = srcStr.getBytes(DEF_CHARSET); byte[] encodedArr = encodedStr.getBytes(DEF_CHARSET); ByteBuffer srcBuf = ByteBuffer.wrap(srcArr); ByteBuffer encodedBuf = ByteBuffer.wrap(encodedArr); byte[] resArr = new byte[encodedArr.length]; // test int encode(byte[], byte[]) int len = encoder.encode(srcArr, resArr); assertEqual(len, encodedArr.length); assertEqual(resArr, encodedArr); // test byte[] encode(byte[]) resArr = encoder.encode(srcArr); assertEqual(resArr, encodedArr); // test ByteBuffer encode(ByteBuffer) int limit = srcBuf.limit(); ByteBuffer resBuf = encoder.encode(srcBuf); assertEqual(srcBuf.position(), limit); assertEqual(srcBuf.limit(), limit); assertEqual(resBuf, encodedBuf); srcBuf.rewind(); // reset for next test // test String encodeToString(byte[]) String resEncodeStr = encoder.encodeToString(srcArr); assertEqual(resEncodeStr, encodedStr); // test int decode(byte[], byte[]) resArr = new byte[srcArr.length]; len = decoder.decode(encodedArr, resArr); assertEqual(len, srcArr.length); assertEqual(resArr, srcArr); // test byte[] decode(byte[]) resArr = decoder.decode(encodedArr); assertEqual(resArr, srcArr); // test ByteBuffer decode(ByteBuffer) limit = encodedBuf.limit(); resBuf = decoder.decode(encodedBuf); assertEqual(encodedBuf.position(), limit); assertEqual(encodedBuf.limit(), limit); assertEqual(resBuf, srcBuf); encodedBuf.rewind(); // reset for next test // test byte[] decode(String) resArr = decoder.decode(encodedStr); assertEqual(resArr, srcArr); // test compatible with sun.misc.Base64Encoder if (type == Base64Type.MIME) { sun.misc.BASE64Encoder miscEncoder = new BASE64Encoder(); sun.misc.BASE64Decoder miscDecoder = new BASE64Decoder(); resArr = decoder.decode(miscEncoder.encode(srcArr)); assertEqual(resArr, srcArr); resArr = encoder.encode(miscDecoder.decodeBuffer(encodedStr)); assertEqual(new String(resArr, DEF_CHARSET), encodedStr); } } }
Example 13
Source File: TestBase64Golden.java From dragonwell8_jdk with GNU General Public License v2.0 | 4 votes |
public static void test0(Base64Type type, Encoder encoder, Decoder decoder, String srcFile, String encodedFile) throws Exception { String[] srcLns = Files.readAllLines(Paths.get(SRCDIR, srcFile), DEF_CHARSET) .toArray(new String[0]); String[] encodedLns = Files.readAllLines(Paths.get(SRCDIR, encodedFile), DEF_CHARSET) .toArray(new String[0]); int lns = 0; for (String srcStr : srcLns) { String encodedStr = null; if (type != Base64Type.MIME) { encodedStr = encodedLns[lns++]; } else { while (lns < encodedLns.length) { String s = encodedLns[lns++]; if (s.length() == 0) break; if (encodedStr != null) { encodedStr += DEFAULT_CRLF + s; } else { encodedStr = s; } } if (encodedStr == null && srcStr.length() == 0) { encodedStr = ""; } } System.out.printf("%n src[%d]: %s%n", srcStr.length(), srcStr); System.out.printf("encoded[%d]: %s%n", encodedStr.length(), encodedStr); byte[] srcArr = srcStr.getBytes(DEF_CHARSET); byte[] encodedArr = encodedStr.getBytes(DEF_CHARSET); ByteBuffer srcBuf = ByteBuffer.wrap(srcArr); ByteBuffer encodedBuf = ByteBuffer.wrap(encodedArr); byte[] resArr = new byte[encodedArr.length]; // test int encode(byte[], byte[]) int len = encoder.encode(srcArr, resArr); assertEqual(len, encodedArr.length); assertEqual(resArr, encodedArr); // test byte[] encode(byte[]) resArr = encoder.encode(srcArr); assertEqual(resArr, encodedArr); // test ByteBuffer encode(ByteBuffer) int limit = srcBuf.limit(); ByteBuffer resBuf = encoder.encode(srcBuf); assertEqual(srcBuf.position(), limit); assertEqual(srcBuf.limit(), limit); assertEqual(resBuf, encodedBuf); srcBuf.rewind(); // reset for next test // test String encodeToString(byte[]) String resEncodeStr = encoder.encodeToString(srcArr); assertEqual(resEncodeStr, encodedStr); // test int decode(byte[], byte[]) resArr = new byte[srcArr.length]; len = decoder.decode(encodedArr, resArr); assertEqual(len, srcArr.length); assertEqual(resArr, srcArr); // test byte[] decode(byte[]) resArr = decoder.decode(encodedArr); assertEqual(resArr, srcArr); // test ByteBuffer decode(ByteBuffer) limit = encodedBuf.limit(); resBuf = decoder.decode(encodedBuf); assertEqual(encodedBuf.position(), limit); assertEqual(encodedBuf.limit(), limit); assertEqual(resBuf, srcBuf); encodedBuf.rewind(); // reset for next test // test byte[] decode(String) resArr = decoder.decode(encodedStr); assertEqual(resArr, srcArr); // test compatible with sun.misc.Base64Encoder if (type == Base64Type.MIME) { sun.misc.BASE64Encoder miscEncoder = new BASE64Encoder(); sun.misc.BASE64Decoder miscDecoder = new BASE64Decoder(); resArr = decoder.decode(miscEncoder.encode(srcArr)); assertEqual(resArr, srcArr); resArr = encoder.encode(miscDecoder.decodeBuffer(encodedStr)); assertEqual(new String(resArr, DEF_CHARSET), encodedStr); } } }
Example 14
Source File: TestBase64Golden.java From hottub with GNU General Public License v2.0 | 4 votes |
public static void test0(Base64Type type, Encoder encoder, Decoder decoder, String srcFile, String encodedFile) throws Exception { String[] srcLns = Files.readAllLines(Paths.get(SRCDIR, srcFile), DEF_CHARSET) .toArray(new String[0]); String[] encodedLns = Files.readAllLines(Paths.get(SRCDIR, encodedFile), DEF_CHARSET) .toArray(new String[0]); int lns = 0; for (String srcStr : srcLns) { String encodedStr = null; if (type != Base64Type.MIME) { encodedStr = encodedLns[lns++]; } else { while (lns < encodedLns.length) { String s = encodedLns[lns++]; if (s.length() == 0) break; if (encodedStr != null) { encodedStr += DEFAULT_CRLF + s; } else { encodedStr = s; } } if (encodedStr == null && srcStr.length() == 0) { encodedStr = ""; } } System.out.printf("%n src[%d]: %s%n", srcStr.length(), srcStr); System.out.printf("encoded[%d]: %s%n", encodedStr.length(), encodedStr); byte[] srcArr = srcStr.getBytes(DEF_CHARSET); byte[] encodedArr = encodedStr.getBytes(DEF_CHARSET); ByteBuffer srcBuf = ByteBuffer.wrap(srcArr); ByteBuffer encodedBuf = ByteBuffer.wrap(encodedArr); byte[] resArr = new byte[encodedArr.length]; // test int encode(byte[], byte[]) int len = encoder.encode(srcArr, resArr); assertEqual(len, encodedArr.length); assertEqual(resArr, encodedArr); // test byte[] encode(byte[]) resArr = encoder.encode(srcArr); assertEqual(resArr, encodedArr); // test ByteBuffer encode(ByteBuffer) int limit = srcBuf.limit(); ByteBuffer resBuf = encoder.encode(srcBuf); assertEqual(srcBuf.position(), limit); assertEqual(srcBuf.limit(), limit); assertEqual(resBuf, encodedBuf); srcBuf.rewind(); // reset for next test // test String encodeToString(byte[]) String resEncodeStr = encoder.encodeToString(srcArr); assertEqual(resEncodeStr, encodedStr); // test int decode(byte[], byte[]) resArr = new byte[srcArr.length]; len = decoder.decode(encodedArr, resArr); assertEqual(len, srcArr.length); assertEqual(resArr, srcArr); // test byte[] decode(byte[]) resArr = decoder.decode(encodedArr); assertEqual(resArr, srcArr); // test ByteBuffer decode(ByteBuffer) limit = encodedBuf.limit(); resBuf = decoder.decode(encodedBuf); assertEqual(encodedBuf.position(), limit); assertEqual(encodedBuf.limit(), limit); assertEqual(resBuf, srcBuf); encodedBuf.rewind(); // reset for next test // test byte[] decode(String) resArr = decoder.decode(encodedStr); assertEqual(resArr, srcArr); // test compatible with sun.misc.Base64Encoder if (type == Base64Type.MIME) { sun.misc.BASE64Encoder miscEncoder = new BASE64Encoder(); sun.misc.BASE64Decoder miscDecoder = new BASE64Decoder(); resArr = decoder.decode(miscEncoder.encode(srcArr)); assertEqual(resArr, srcArr); resArr = encoder.encode(miscDecoder.decodeBuffer(encodedStr)); assertEqual(new String(resArr, DEF_CHARSET), encodedStr); } } }
Example 15
Source File: TestBase64Golden.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
public static void test0(Base64Type type, Encoder encoder, Decoder decoder, String srcFile, String encodedFile) throws Exception { String[] srcLns = Files.readAllLines(Paths.get(SRCDIR, srcFile), DEF_CHARSET) .toArray(new String[0]); String[] encodedLns = Files.readAllLines(Paths.get(SRCDIR, encodedFile), DEF_CHARSET) .toArray(new String[0]); int lns = 0; for (String srcStr : srcLns) { String encodedStr = null; if (type != Base64Type.MIME) { encodedStr = encodedLns[lns++]; } else { while (lns < encodedLns.length) { String s = encodedLns[lns++]; if (s.length() == 0) break; if (encodedStr != null) { encodedStr += DEFAULT_CRLF + s; } else { encodedStr = s; } } if (encodedStr == null && srcStr.length() == 0) { encodedStr = ""; } } System.out.printf("%n src[%d]: %s%n", srcStr.length(), srcStr); System.out.printf("encoded[%d]: %s%n", encodedStr.length(), encodedStr); byte[] srcArr = srcStr.getBytes(DEF_CHARSET); byte[] encodedArr = encodedStr.getBytes(DEF_CHARSET); ByteBuffer srcBuf = ByteBuffer.wrap(srcArr); ByteBuffer encodedBuf = ByteBuffer.wrap(encodedArr); byte[] resArr = new byte[encodedArr.length]; // test int encode(byte[], byte[]) int len = encoder.encode(srcArr, resArr); assertEqual(len, encodedArr.length); assertEqual(resArr, encodedArr); // test byte[] encode(byte[]) resArr = encoder.encode(srcArr); assertEqual(resArr, encodedArr); // test ByteBuffer encode(ByteBuffer) int limit = srcBuf.limit(); ByteBuffer resBuf = encoder.encode(srcBuf); assertEqual(srcBuf.position(), limit); assertEqual(srcBuf.limit(), limit); assertEqual(resBuf, encodedBuf); srcBuf.rewind(); // reset for next test // test String encodeToString(byte[]) String resEncodeStr = encoder.encodeToString(srcArr); assertEqual(resEncodeStr, encodedStr); // test int decode(byte[], byte[]) resArr = new byte[srcArr.length]; len = decoder.decode(encodedArr, resArr); assertEqual(len, srcArr.length); assertEqual(resArr, srcArr); // test byte[] decode(byte[]) resArr = decoder.decode(encodedArr); assertEqual(resArr, srcArr); // test ByteBuffer decode(ByteBuffer) limit = encodedBuf.limit(); resBuf = decoder.decode(encodedBuf); assertEqual(encodedBuf.position(), limit); assertEqual(encodedBuf.limit(), limit); assertEqual(resBuf, srcBuf); encodedBuf.rewind(); // reset for next test // test byte[] decode(String) resArr = decoder.decode(encodedStr); assertEqual(resArr, srcArr); // test compatible with sun.misc.Base64Encoder if (type == Base64Type.MIME) { sun.misc.BASE64Encoder miscEncoder = new BASE64Encoder(); sun.misc.BASE64Decoder miscDecoder = new BASE64Decoder(); resArr = decoder.decode(miscEncoder.encode(srcArr)); assertEqual(resArr, srcArr); resArr = encoder.encode(miscDecoder.decodeBuffer(encodedStr)); assertEqual(new String(resArr, DEF_CHARSET), encodedStr); } } }
Example 16
Source File: TestBase64Golden.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
public static void test0(Base64Type type, Encoder encoder, Decoder decoder, String srcFile, String encodedFile) throws Exception { String[] srcLns = Files.readAllLines(Paths.get(SRCDIR, srcFile), DEF_CHARSET) .toArray(new String[0]); String[] encodedLns = Files.readAllLines(Paths.get(SRCDIR, encodedFile), DEF_CHARSET) .toArray(new String[0]); int lns = 0; for (String srcStr : srcLns) { String encodedStr = null; if (type != Base64Type.MIME) { encodedStr = encodedLns[lns++]; } else { while (lns < encodedLns.length) { String s = encodedLns[lns++]; if (s.length() == 0) break; if (encodedStr != null) { encodedStr += DEFAULT_CRLF + s; } else { encodedStr = s; } } if (encodedStr == null && srcStr.length() == 0) { encodedStr = ""; } } System.out.printf("%n src[%d]: %s%n", srcStr.length(), srcStr); System.out.printf("encoded[%d]: %s%n", encodedStr.length(), encodedStr); byte[] srcArr = srcStr.getBytes(DEF_CHARSET); byte[] encodedArr = encodedStr.getBytes(DEF_CHARSET); ByteBuffer srcBuf = ByteBuffer.wrap(srcArr); ByteBuffer encodedBuf = ByteBuffer.wrap(encodedArr); byte[] resArr = new byte[encodedArr.length]; // test int encode(byte[], byte[]) int len = encoder.encode(srcArr, resArr); assertEqual(len, encodedArr.length); assertEqual(resArr, encodedArr); // test byte[] encode(byte[]) resArr = encoder.encode(srcArr); assertEqual(resArr, encodedArr); // test ByteBuffer encode(ByteBuffer) int limit = srcBuf.limit(); ByteBuffer resBuf = encoder.encode(srcBuf); assertEqual(srcBuf.position(), limit); assertEqual(srcBuf.limit(), limit); assertEqual(resBuf, encodedBuf); srcBuf.rewind(); // reset for next test // test String encodeToString(byte[]) String resEncodeStr = encoder.encodeToString(srcArr); assertEqual(resEncodeStr, encodedStr); // test int decode(byte[], byte[]) resArr = new byte[srcArr.length]; len = decoder.decode(encodedArr, resArr); assertEqual(len, srcArr.length); assertEqual(resArr, srcArr); // test byte[] decode(byte[]) resArr = decoder.decode(encodedArr); assertEqual(resArr, srcArr); // test ByteBuffer decode(ByteBuffer) limit = encodedBuf.limit(); resBuf = decoder.decode(encodedBuf); assertEqual(encodedBuf.position(), limit); assertEqual(encodedBuf.limit(), limit); assertEqual(resBuf, srcBuf); encodedBuf.rewind(); // reset for next test // test byte[] decode(String) resArr = decoder.decode(encodedStr); assertEqual(resArr, srcArr); } }
Example 17
Source File: TestBase64Golden.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
public static void test0(Base64Type type, Encoder encoder, Decoder decoder, String srcFile, String encodedFile) throws Exception { String[] srcLns = Files.readAllLines(Paths.get(SRCDIR, srcFile), DEF_CHARSET) .toArray(new String[0]); String[] encodedLns = Files.readAllLines(Paths.get(SRCDIR, encodedFile), DEF_CHARSET) .toArray(new String[0]); int lns = 0; for (String srcStr : srcLns) { String encodedStr = null; if (type != Base64Type.MIME) { encodedStr = encodedLns[lns++]; } else { while (lns < encodedLns.length) { String s = encodedLns[lns++]; if (s.length() == 0) break; if (encodedStr != null) { encodedStr += DEFAULT_CRLF + s; } else { encodedStr = s; } } if (encodedStr == null && srcStr.length() == 0) { encodedStr = ""; } } System.out.printf("%n src[%d]: %s%n", srcStr.length(), srcStr); System.out.printf("encoded[%d]: %s%n", encodedStr.length(), encodedStr); byte[] srcArr = srcStr.getBytes(DEF_CHARSET); byte[] encodedArr = encodedStr.getBytes(DEF_CHARSET); ByteBuffer srcBuf = ByteBuffer.wrap(srcArr); ByteBuffer encodedBuf = ByteBuffer.wrap(encodedArr); byte[] resArr = new byte[encodedArr.length]; // test int encode(byte[], byte[]) int len = encoder.encode(srcArr, resArr); assertEqual(len, encodedArr.length); assertEqual(resArr, encodedArr); // test byte[] encode(byte[]) resArr = encoder.encode(srcArr); assertEqual(resArr, encodedArr); // test ByteBuffer encode(ByteBuffer) int limit = srcBuf.limit(); ByteBuffer resBuf = encoder.encode(srcBuf); assertEqual(srcBuf.position(), limit); assertEqual(srcBuf.limit(), limit); assertEqual(resBuf, encodedBuf); srcBuf.rewind(); // reset for next test // test String encodeToString(byte[]) String resEncodeStr = encoder.encodeToString(srcArr); assertEqual(resEncodeStr, encodedStr); // test int decode(byte[], byte[]) resArr = new byte[srcArr.length]; len = decoder.decode(encodedArr, resArr); assertEqual(len, srcArr.length); assertEqual(resArr, srcArr); // test byte[] decode(byte[]) resArr = decoder.decode(encodedArr); assertEqual(resArr, srcArr); // test ByteBuffer decode(ByteBuffer) limit = encodedBuf.limit(); resBuf = decoder.decode(encodedBuf); assertEqual(encodedBuf.position(), limit); assertEqual(encodedBuf.limit(), limit); assertEqual(resBuf, srcBuf); encodedBuf.rewind(); // reset for next test // test byte[] decode(String) resArr = decoder.decode(encodedStr); assertEqual(resArr, srcArr); // test compatible with sun.misc.Base64Encoder if (type == Base64Type.MIME) { sun.misc.BASE64Encoder miscEncoder = new BASE64Encoder(); sun.misc.BASE64Decoder miscDecoder = new BASE64Decoder(); resArr = decoder.decode(miscEncoder.encode(srcArr)); assertEqual(resArr, srcArr); resArr = encoder.encode(miscDecoder.decodeBuffer(encodedStr)); assertEqual(new String(resArr, DEF_CHARSET), encodedStr); } } }
Example 18
Source File: TestBase64Golden.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
public static void test0(Base64Type type, Encoder encoder, Decoder decoder, String srcFile, String encodedFile) throws Exception { String[] srcLns = Files.readAllLines(Paths.get(SRCDIR, srcFile), DEF_CHARSET) .toArray(new String[0]); String[] encodedLns = Files.readAllLines(Paths.get(SRCDIR, encodedFile), DEF_CHARSET) .toArray(new String[0]); int lns = 0; for (String srcStr : srcLns) { String encodedStr = null; if (type != Base64Type.MIME) { encodedStr = encodedLns[lns++]; } else { while (lns < encodedLns.length) { String s = encodedLns[lns++]; if (s.length() == 0) break; if (encodedStr != null) { encodedStr += DEFAULT_CRLF + s; } else { encodedStr = s; } } if (encodedStr == null && srcStr.length() == 0) { encodedStr = ""; } } System.out.printf("%n src[%d]: %s%n", srcStr.length(), srcStr); System.out.printf("encoded[%d]: %s%n", encodedStr.length(), encodedStr); byte[] srcArr = srcStr.getBytes(DEF_CHARSET); byte[] encodedArr = encodedStr.getBytes(DEF_CHARSET); ByteBuffer srcBuf = ByteBuffer.wrap(srcArr); ByteBuffer encodedBuf = ByteBuffer.wrap(encodedArr); byte[] resArr = new byte[encodedArr.length]; // test int encode(byte[], byte[]) int len = encoder.encode(srcArr, resArr); assertEqual(len, encodedArr.length); assertEqual(resArr, encodedArr); // test byte[] encode(byte[]) resArr = encoder.encode(srcArr); assertEqual(resArr, encodedArr); // test ByteBuffer encode(ByteBuffer) int limit = srcBuf.limit(); ByteBuffer resBuf = encoder.encode(srcBuf); assertEqual(srcBuf.position(), limit); assertEqual(srcBuf.limit(), limit); assertEqual(resBuf, encodedBuf); srcBuf.rewind(); // reset for next test // test String encodeToString(byte[]) String resEncodeStr = encoder.encodeToString(srcArr); assertEqual(resEncodeStr, encodedStr); // test int decode(byte[], byte[]) resArr = new byte[srcArr.length]; len = decoder.decode(encodedArr, resArr); assertEqual(len, srcArr.length); assertEqual(resArr, srcArr); // test byte[] decode(byte[]) resArr = decoder.decode(encodedArr); assertEqual(resArr, srcArr); // test ByteBuffer decode(ByteBuffer) limit = encodedBuf.limit(); resBuf = decoder.decode(encodedBuf); assertEqual(encodedBuf.position(), limit); assertEqual(encodedBuf.limit(), limit); assertEqual(resBuf, srcBuf); encodedBuf.rewind(); // reset for next test // test byte[] decode(String) resArr = decoder.decode(encodedStr); assertEqual(resArr, srcArr); // test compatible with sun.misc.Base64Encoder if (type == Base64Type.MIME) { sun.misc.BASE64Encoder miscEncoder = new BASE64Encoder(); sun.misc.BASE64Decoder miscDecoder = new BASE64Decoder(); resArr = decoder.decode(miscEncoder.encode(srcArr)); assertEqual(resArr, srcArr); resArr = encoder.encode(miscDecoder.decodeBuffer(encodedStr)); assertEqual(new String(resArr, DEF_CHARSET), encodedStr); } } }
Example 19
Source File: TestBase64Golden.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
public static void test0(Base64Type type, Encoder encoder, Decoder decoder, String srcFile, String encodedFile) throws Exception { String[] srcLns = Files.readAllLines(Paths.get(SRCDIR, srcFile), DEF_CHARSET) .toArray(new String[0]); String[] encodedLns = Files.readAllLines(Paths.get(SRCDIR, encodedFile), DEF_CHARSET) .toArray(new String[0]); int lns = 0; for (String srcStr : srcLns) { String encodedStr = null; if (type != Base64Type.MIME) { encodedStr = encodedLns[lns++]; } else { while (lns < encodedLns.length) { String s = encodedLns[lns++]; if (s.length() == 0) break; if (encodedStr != null) { encodedStr += DEFAULT_CRLF + s; } else { encodedStr = s; } } if (encodedStr == null && srcStr.length() == 0) { encodedStr = ""; } } System.out.printf("%n src[%d]: %s%n", srcStr.length(), srcStr); System.out.printf("encoded[%d]: %s%n", encodedStr.length(), encodedStr); byte[] srcArr = srcStr.getBytes(DEF_CHARSET); byte[] encodedArr = encodedStr.getBytes(DEF_CHARSET); ByteBuffer srcBuf = ByteBuffer.wrap(srcArr); ByteBuffer encodedBuf = ByteBuffer.wrap(encodedArr); byte[] resArr = new byte[encodedArr.length]; // test int encode(byte[], byte[]) int len = encoder.encode(srcArr, resArr); assertEqual(len, encodedArr.length); assertEqual(resArr, encodedArr); // test byte[] encode(byte[]) resArr = encoder.encode(srcArr); assertEqual(resArr, encodedArr); // test ByteBuffer encode(ByteBuffer) int limit = srcBuf.limit(); ByteBuffer resBuf = encoder.encode(srcBuf); assertEqual(srcBuf.position(), limit); assertEqual(srcBuf.limit(), limit); assertEqual(resBuf, encodedBuf); srcBuf.rewind(); // reset for next test // test String encodeToString(byte[]) String resEncodeStr = encoder.encodeToString(srcArr); assertEqual(resEncodeStr, encodedStr); // test int decode(byte[], byte[]) resArr = new byte[srcArr.length]; len = decoder.decode(encodedArr, resArr); assertEqual(len, srcArr.length); assertEqual(resArr, srcArr); // test byte[] decode(byte[]) resArr = decoder.decode(encodedArr); assertEqual(resArr, srcArr); // test ByteBuffer decode(ByteBuffer) limit = encodedBuf.limit(); resBuf = decoder.decode(encodedBuf); assertEqual(encodedBuf.position(), limit); assertEqual(encodedBuf.limit(), limit); assertEqual(resBuf, srcBuf); encodedBuf.rewind(); // reset for next test // test byte[] decode(String) resArr = decoder.decode(encodedStr); assertEqual(resArr, srcArr); // test compatible with sun.misc.Base64Encoder if (type == Base64Type.MIME) { sun.misc.BASE64Encoder miscEncoder = new BASE64Encoder(); sun.misc.BASE64Decoder miscDecoder = new BASE64Decoder(); resArr = decoder.decode(miscEncoder.encode(srcArr)); assertEqual(resArr, srcArr); resArr = encoder.encode(miscDecoder.decodeBuffer(encodedStr)); assertEqual(new String(resArr, DEF_CHARSET), encodedStr); } } }
Example 20
Source File: TestBase64Golden.java From TencentKona-8 with GNU General Public License v2.0 | 4 votes |
public static void test0(Base64Type type, Encoder encoder, Decoder decoder, String srcFile, String encodedFile) throws Exception { String[] srcLns = Files.readAllLines(Paths.get(SRCDIR, srcFile), DEF_CHARSET) .toArray(new String[0]); String[] encodedLns = Files.readAllLines(Paths.get(SRCDIR, encodedFile), DEF_CHARSET) .toArray(new String[0]); int lns = 0; for (String srcStr : srcLns) { String encodedStr = null; if (type != Base64Type.MIME) { encodedStr = encodedLns[lns++]; } else { while (lns < encodedLns.length) { String s = encodedLns[lns++]; if (s.length() == 0) break; if (encodedStr != null) { encodedStr += DEFAULT_CRLF + s; } else { encodedStr = s; } } if (encodedStr == null && srcStr.length() == 0) { encodedStr = ""; } } System.out.printf("%n src[%d]: %s%n", srcStr.length(), srcStr); System.out.printf("encoded[%d]: %s%n", encodedStr.length(), encodedStr); byte[] srcArr = srcStr.getBytes(DEF_CHARSET); byte[] encodedArr = encodedStr.getBytes(DEF_CHARSET); ByteBuffer srcBuf = ByteBuffer.wrap(srcArr); ByteBuffer encodedBuf = ByteBuffer.wrap(encodedArr); byte[] resArr = new byte[encodedArr.length]; // test int encode(byte[], byte[]) int len = encoder.encode(srcArr, resArr); assertEqual(len, encodedArr.length); assertEqual(resArr, encodedArr); // test byte[] encode(byte[]) resArr = encoder.encode(srcArr); assertEqual(resArr, encodedArr); // test ByteBuffer encode(ByteBuffer) int limit = srcBuf.limit(); ByteBuffer resBuf = encoder.encode(srcBuf); assertEqual(srcBuf.position(), limit); assertEqual(srcBuf.limit(), limit); assertEqual(resBuf, encodedBuf); srcBuf.rewind(); // reset for next test // test String encodeToString(byte[]) String resEncodeStr = encoder.encodeToString(srcArr); assertEqual(resEncodeStr, encodedStr); // test int decode(byte[], byte[]) resArr = new byte[srcArr.length]; len = decoder.decode(encodedArr, resArr); assertEqual(len, srcArr.length); assertEqual(resArr, srcArr); // test byte[] decode(byte[]) resArr = decoder.decode(encodedArr); assertEqual(resArr, srcArr); // test ByteBuffer decode(ByteBuffer) limit = encodedBuf.limit(); resBuf = decoder.decode(encodedBuf); assertEqual(encodedBuf.position(), limit); assertEqual(encodedBuf.limit(), limit); assertEqual(resBuf, srcBuf); encodedBuf.rewind(); // reset for next test // test byte[] decode(String) resArr = decoder.decode(encodedStr); assertEqual(resArr, srcArr); // test compatible with sun.misc.Base64Encoder if (type == Base64Type.MIME) { sun.misc.BASE64Encoder miscEncoder = new BASE64Encoder(); sun.misc.BASE64Decoder miscDecoder = new BASE64Decoder(); resArr = decoder.decode(miscEncoder.encode(srcArr)); assertEqual(resArr, srcArr); resArr = encoder.encode(miscDecoder.decodeBuffer(encodedStr)); assertEqual(new String(resArr, DEF_CHARSET), encodedStr); } } }