test/java/util/Base64/Base64GetEncoderTest.java

Print this page




  34 
  35 /**
  36  * @test
  37  * @bug 8007799
  38  * @summary test Encoder with linemax == 0, line separator should not appear in encoded data
  39  */
  40 
  41 public class Base64GetEncoderTest {
  42 
  43     public static void main(String args[]) throws Throwable {
  44         final Base64.Encoder encoder = Base64.getMimeEncoder(0, "$$$".getBytes(US_ASCII));
  45 
  46         testEncodeToString(encoder);
  47 
  48         testWrapEncode1(encoder);
  49 
  50         testEncodeToStringWithLongInputData(encoder);
  51 
  52         testWrapEncode2(encoder);
  53 
  54         testEncodeWithByteBuffer(encoder);
  55 
  56     }
  57 
  58     private static void testEncodeWithByteBuffer(final Base64.Encoder encoder) {
  59         System.err.println("\n\nEncoder.encode with ByteBuffer test  ");
  60         final byte[] secondTestBuffer =
  61                 "api/java_util/Base64/index.html#GetEncoderMimeCustom[noLineSeparatorInEncodedString]"
  62                 .getBytes(US_ASCII);
  63         String base64EncodedString;
  64         ByteBuffer srcData = ByteBuffer.wrap(secondTestBuffer);
  65         ByteBuffer dstData = ByteBuffer.allocate(secondTestBuffer.length * 2);
  66 
  67         encoder.encode(srcData, dstData, 0);
  68         dstData.flip();
  69         if (dstData.hasArray()) {
  70             System.err.println("\nByteBuffer test dstData is Base64 encoding = "
  71                     + new String(dstData.array(), US_ASCII) + "\n");
  72         }
  73 
  74         base64EncodedString = new String(dstData.array(), US_ASCII);
  75         if (base64EncodedString.contains("$$$")) {
  76             throw new RuntimeException("Base64 encoding contains line separator after Encoder.encode ByteBuffer ... \n");
  77         }
  78     }
  79 
  80     private static void testWrapEncode2(final Base64.Encoder encoder)
  81             throws IOException {
  82         System.err.println("\nEncoder.wrap test II ");
  83         final byte[] secondTestBuffer =
  84                 "api/java_util/Base64/index.html#GetEncoderMimeCustom[noLineSeparatorInEncodedString]"
  85                 .getBytes(US_ASCII);
  86         String base64EncodedString;
  87         ByteArrayOutputStream secondEncodingStream = new ByteArrayOutputStream();
  88         OutputStream base64EncodingStream = encoder.wrap(secondEncodingStream);
  89         base64EncodingStream.write(secondTestBuffer);
  90         base64EncodingStream.close();
  91 
  92         final byte[] encodedByteArray = secondEncodingStream.toByteArray();
  93 
  94         System.err.print("result = " + new String(encodedByteArray, US_ASCII)
  95                 + "  after wrap Base64 encoding of string");
  96 
  97         base64EncodedString = new String(encodedByteArray, US_ASCII);




  34 
  35 /**
  36  * @test
  37  * @bug 8007799
  38  * @summary test Encoder with linemax == 0, line separator should not appear in encoded data
  39  */
  40 
  41 public class Base64GetEncoderTest {
  42 
  43     public static void main(String args[]) throws Throwable {
  44         final Base64.Encoder encoder = Base64.getMimeEncoder(0, "$$$".getBytes(US_ASCII));
  45 
  46         testEncodeToString(encoder);
  47 
  48         testWrapEncode1(encoder);
  49 
  50         testEncodeToStringWithLongInputData(encoder);
  51 
  52         testWrapEncode2(encoder);
  53 
























  54     }
  55 
  56     private static void testWrapEncode2(final Base64.Encoder encoder)
  57             throws IOException {
  58         System.err.println("\nEncoder.wrap test II ");
  59         final byte[] secondTestBuffer =
  60                 "api/java_util/Base64/index.html#GetEncoderMimeCustom[noLineSeparatorInEncodedString]"
  61                 .getBytes(US_ASCII);
  62         String base64EncodedString;
  63         ByteArrayOutputStream secondEncodingStream = new ByteArrayOutputStream();
  64         OutputStream base64EncodingStream = encoder.wrap(secondEncodingStream);
  65         base64EncodingStream.write(secondTestBuffer);
  66         base64EncodingStream.close();
  67 
  68         final byte[] encodedByteArray = secondEncodingStream.toByteArray();
  69 
  70         System.err.print("result = " + new String(encodedByteArray, US_ASCII)
  71                 + "  after wrap Base64 encoding of string");
  72 
  73         base64EncodedString = new String(encodedByteArray, US_ASCII);