Java Code Examples for java.util.Base64.Encoder#encodeToString()
The following examples show how to use
java.util.Base64.Encoder#encodeToString() .
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: Base64Test.java From j2objc with Apache License 2.0 | 5 votes |
/** * Checks round-trip encoding/decoding of {@code plain}. * * @param plain an ASCII String * @return the Base64-encoded value of the ASCII codepoints from {@code plain} */ private static String encodeFromAscii(Encoder encoder, Decoder decoder, String plain) throws Exception { String encoded = encoder.encodeToString(plain.getBytes(US_ASCII)); String decoded = decodeToAscii(decoder, encoded); assertEquals(plain, decoded); return encoded; }
Example 2
Source File: SampleIDs.java From browserprint with MIT License | 5 votes |
/** * Encrypt an integer to a String. * * @param integer * @param context * @return * @throws ServletException */ private static String encryptInteger(Integer integer, ServletContext context) throws ServletException { /* Get password. */ String password = context.getInitParameter("SampleSetIDEncryptionPassword"); /* Generate salt. */ SecureRandom rand = new SecureRandom(); byte salt[] = new byte[8]; rand.nextBytes(salt); byte[] iv; byte[] ciphertext; try { /* Derive the key, given password and salt. */ SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 256); SecretKey tmp = factory.generateSecret(spec); SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES"); /* Encrypt the SampleSetID. */ Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secret); AlgorithmParameters params = cipher.getParameters(); iv = params.getParameterSpec(IvParameterSpec.class).getIV(); ciphertext = cipher.doFinal(ByteBuffer.allocate(4).putInt(integer).array()); } catch (Exception ex) { throw new ServletException(ex); } /* Store the encrypted SampleSetID in a cookie */ Encoder encoder = Base64.getEncoder(); String encryptedStr = encoder.encodeToString(ciphertext) + "|" + encoder.encodeToString(iv) + "|" + encoder.encodeToString(salt); return encryptedStr; }
Example 3
Source File: JavaImgConverter.java From easyCV with Apache License 2.0 | 5 votes |
/** * bufferedImage转base64 * @param format -格式(jpg,png等等) * @return * @throws IOException */ public static String bufferedImage2Base64(BufferedImage image, String format) throws IOException { Encoder encoder = Base64.getEncoder(); ByteArrayOutputStream baos = new ByteArrayOutputStream();// 字节流 // baos.reset(); ImageIO.write(image, format, baos);// 写出到字节流 byte[] bytes=baos.toByteArray(); // 编码成base64 String jpg_base64 = encoder.encodeToString(bytes); return jpg_base64; }
Example 4
Source File: AESCipher.java From AESCipher-Java with MIT License | 5 votes |
public static String aesEncryptString(String content, String key) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { byte[] contentBytes = content.getBytes(charset); byte[] keyBytes = key.getBytes(charset); byte[] encryptedBytes = aesEncryptBytes(contentBytes, keyBytes); Encoder encoder = Base64.getEncoder(); return encoder.encodeToString(encryptedBytes); }
Example 5
Source File: CommonUtil.java From FHIR with Apache License 2.0 | 5 votes |
/** * Generate a random 32 byte value encoded as a Base64 string (44 characters). * * @return */ public static String getRandomKey() { byte[] buffer = new byte[32]; random.nextBytes(buffer); Encoder enc = Base64.getEncoder(); return enc.encodeToString(buffer); }
Example 6
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 7
Source File: GcpDiskEncryptionService.java From cloudbreak with Apache License 2.0 | 4 votes |
private String encode(byte[] data) { Encoder encoder = Base64.getEncoder(); return encoder.encodeToString(data); }
Example 8
Source File: ContentCryptographer.java From keywhiz with Apache License 2.0 | 4 votes |
static Crypted of(String info, byte[] content, byte[] iv) { Encoder encoder = getEncoder(); String contentBase64 = encoder.encodeToString(content); String ivBase64 = encoder.encodeToString(iv); return new AutoValue_ContentCryptographer_Crypted(info, contentBase64, ivBase64); }
Example 9
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 10
Source File: TestBase64Golden.java From openjdk-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); } } }
Example 11
Source File: TestBase64Golden.java From openjdk-8-source 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 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 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 14
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 15
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 16
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 17
Source File: TestBase64Golden.java From native-obfuscator with GNU General Public License v3.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 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); } } }
Example 19
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 20
Source File: TokenSecurityContextFilter.java From openscoring with GNU Affero General Public License v3.0 | 3 votes |
static private String generateRandomToken(int length){ byte[] bytes = new byte[length]; TokenSecurityContextFilter.random.nextBytes(bytes); Encoder encoder = (Base64.getUrlEncoder()).withoutPadding(); return encoder.encodeToString(bytes); }