예제 #1
0
  /**
   * Test the Base64OutputStream implementation
   *
   * @throws Exception for some failure scenarios.
   */
  @Test
  public void testBase64OutputStreamByChunk() throws Exception {
    // Hello World test.
    byte[] encoded = StringUtils.getBytesUtf8("SGVsbG8gV29ybGQ=\r\n");
    byte[] decoded = StringUtils.getBytesUtf8(STRING_FIXTURE);
    testByChunk(encoded, decoded, BaseNCodec.MIME_CHUNK_SIZE, CRLF);

    // Single Byte test.
    encoded = StringUtils.getBytesUtf8("AA==\r\n");
    decoded = new byte[] {(byte) 0};
    testByChunk(encoded, decoded, BaseNCodec.MIME_CHUNK_SIZE, CRLF);

    // OpenSSL interop test.
    encoded = StringUtils.getBytesUtf8(Base64TestData.ENCODED_64_CHARS_PER_LINE);
    decoded = Base64TestData.DECODED;
    testByChunk(encoded, decoded, BaseNCodec.PEM_CHUNK_SIZE, LF);

    // Single Line test.
    String singleLine = Base64TestData.ENCODED_64_CHARS_PER_LINE.replaceAll("\n", "");
    encoded = StringUtils.getBytesUtf8(singleLine);
    decoded = Base64TestData.DECODED;
    testByChunk(encoded, decoded, 0, LF);

    // test random data of sizes 0 thru 150
    for (int i = 0; i <= 150; i++) {
      byte[][] randomData = Base64TestData.randomData(i, false);
      encoded = randomData[1];
      decoded = randomData[0];
      testByChunk(encoded, decoded, 0, LF);
    }
  }
예제 #2
0
  /**
   * Test the Base64OutputStream implementation against the special NPE inducing input identified in
   * the CODEC-98 bug.
   *
   * @throws Exception for some failure scenarios.
   */
  @Test
  public void testCodec98NPE() throws Exception {
    byte[] codec98 = StringUtils.getBytesUtf8(Base64TestData.CODEC_98_NPE);
    byte[] codec98_1024 = new byte[1024];
    System.arraycopy(codec98, 0, codec98_1024, 0, codec98.length);
    ByteArrayOutputStream data = new ByteArrayOutputStream(1024);
    Base64OutputStream stream = new Base64OutputStream(data, false);
    stream.write(codec98_1024, 0, 1024);
    stream.close();

    byte[] decodedBytes = data.toByteArray();
    String decoded = StringUtils.newStringUtf8(decodedBytes);
    assertEquals("codec-98 NPE Base64OutputStream", Base64TestData.CODEC_98_NPE_DECODED, decoded);
  }
예제 #3
0
 /**
  * Tests a given String to see if it contains only valid characters within the Base64 alphabet.
  * Currently the method treats whitespace as valid.
  *
  * @param base64 String to test
  * @return <code>true</code> if all characters in the String are valid characters in the Base64
  *     alphabet or if the String is empty; <code>false</code>, otherwise
  * @since 1.5
  */
 public static boolean isBase64(String base64) {
   return isBase64(StringUtils.getBytesUtf8(base64));
 }
예제 #4
0
 /**
  * Tests a given String to see if it contains only valid characters within the alphabet. The
  * method treats whitespace and PAD as valid.
  *
  * @param basen String to test
  * @return {@code true} if all characters in the String are valid characters in the alphabet or if
  *     the String is empty; {@code false}, otherwise
  * @see #isInAlphabet(byte[], boolean)
  */
 public boolean isInAlphabet(final String basen) {
   return isInAlphabet(StringUtils.getBytesUtf8(basen), true);
 }
예제 #5
0
 /**
  * Decodes a String containing characters in the Base-N alphabet.
  *
  * @param pArray A String containing Base-N character data
  * @return a byte array containing binary data
  */
 public byte[] decode(final String pArray) {
   return decode(StringUtils.getBytesUtf8(pArray));
 }
예제 #6
0
 public boolean isInAlphabet(String s) {
   return isInAlphabet(StringUtils.getBytesUtf8(s), true);
 }
예제 #7
0
 public byte[] decode(String s) {
   return decode(StringUtils.getBytesUtf8(s));
 }
예제 #8
0
 public static boolean isBase64(String paramString) {
   return isBase64(StringUtils.getBytesUtf8(paramString));
 }
예제 #9
0
 /**
  * Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>.
  *
  * @param data Data to digest
  * @return MD5 digest
  */
 public static byte[] md5(final String data) {
   return md5(StringUtils.getBytesUtf8(data));
 }