/** * Creates a Base32OutputStream such that all data written is either Base32-encoded or Base32-decoded to the * original provided OutputStream. * * @param out * OutputStream to wrap. * @param doEncode * true if we should encode all data written to us, false if we should decode. */ public Base32OutputStream(final OutputStream out, final boolean doEncode) { super(out, new Base32(false), doEncode); }
/** * Creates a Base32InputStream such that all data read is either Base32-encoded or Base32-decoded from the original * provided InputStream. * * @param in * InputStream to wrap. * @param doEncode * true if we should encode all data read from us, false if we should decode. */ public Base32InputStream(final InputStream in, final boolean doEncode) { super(in, new Base32(false), doEncode); }
/** * Creates a Base32OutputStream such that all data written is either Base32-encoded or Base32-decoded to the * original provided OutputStream. * * @param out * OutputStream to wrap. * @param doEncode * true if we should encode all data written to us, false if we should decode. * @param lineLength * If doEncode is true, each line of encoded data will contain lineLength characters (rounded down to * nearest multiple of 4). If lineLength <= 0, the encoded data is not divided into lines. If doEncode * is false, lineLength is ignored. * @param lineSeparator * If doEncode is true, each line of encoded data will be terminated with this byte sequence (e.g. \r\n). * If lineLength <= 0, the lineSeparator is not used. If doEncode is false lineSeparator is ignored. */ public Base32OutputStream(final OutputStream out, final boolean doEncode, final int lineLength, final byte[] lineSeparator) { super(out, new Base32(lineLength, lineSeparator), doEncode); }
/** * Creates a Base32InputStream such that all data read is either Base32-encoded or Base32-decoded from the original * provided InputStream. * * @param in * InputStream to wrap. * @param doEncode * true if we should encode all data read from us, false if we should decode. * @param lineLength * If doEncode is true, each line of encoded data will contain lineLength characters (rounded down to * nearest multiple of 4). If lineLength <= 0, the encoded data is not divided into lines. If doEncode * is false, lineLength is ignored. * @param lineSeparator * If doEncode is true, each line of encoded data will be terminated with this byte sequence (e.g. \r\n). * If lineLength <= 0, the lineSeparator is not used. If doEncode is false lineSeparator is ignored. */ public Base32InputStream(final InputStream in, final boolean doEncode, final int lineLength, final byte[] lineSeparator) { super(in, new Base32(lineLength, lineSeparator), doEncode); }
private void testBase64InBuffer(final int startPasSize, final int endPadSize) { final Base32 codec = new Base32(); for (final String[] element : BASE32_TEST_CASES) { final byte[] bytes = element[0].getBytes(CHARSET_UTF8); byte[] buffer = ArrayUtils.addAll(bytes, new byte[endPadSize]); buffer = ArrayUtils.addAll(new byte[startPasSize], buffer); assertEquals(element[1], StringUtils.newStringUtf8(codec.encode(buffer, startPasSize, bytes.length))); } }
@Test public void testCodec200() { final Base32 codec = new Base32(true, (byte)'W'); // should be allowed assertNotNull(codec); }
@Test public void testBase32HexSamplesReverseLowercase() throws Exception { final Base32 codec = new Base32(true); for (final String[] element : BASE32HEX_TEST_CASES) { assertEquals(element[0], new String(codec.decode(element[1].toLowerCase()), CHARSET_UTF8)); } }
@Test public void testBase32HexSamples() throws Exception { final Base32 codec = new Base32(true); for (final String[] element : BASE32HEX_TEST_CASES) { assertEquals(element[1], codec.encodeAsString(element[0].getBytes(CHARSET_UTF8))); } }
@Test public void testBase32HexSamplesReverse() throws Exception { final Base32 codec = new Base32(true); for (final String[] element : BASE32HEX_TEST_CASES) { assertEquals(element[0], new String(codec.decode(element[1]), CHARSET_UTF8)); } }
@Test public void testBase32SamplesNonDefaultPadding() throws Exception { final Base32 codec = new Base32((byte)0x25); // '%' <=> 0x25 for (final String[] element : BASE32_PAD_TEST_CASES) { assertEquals(element[1], codec.encodeAsString(element[0].getBytes(CHARSET_UTF8))); } }
@Test public void testBase32Samples() throws Exception { final Base32 codec = new Base32(); for (final String[] element : BASE32_TEST_CASES) { assertEquals(element[1], codec.encodeAsString(element[0].getBytes(CHARSET_UTF8))); } }
@Test public void testBase32Chunked () throws Exception { final Base32 codec = new Base32(20); for (final String[] element : BASE32_TEST_CASES_CHUNKED) { assertEquals(element[1], codec.encodeAsString(element[0].getBytes(CHARSET_UTF8))); } }
@Test public void testBase32BinarySamples() throws Exception { final Base32 codec = new Base32(); for (final Object[] element : BASE32_BINARY_TEST_CASES) { String expected; if(element.length > 2) { expected = (String)element[2]; } else { expected = (String)element[1]; } assertEquals(expected.toUpperCase(), codec.encodeAsString((byte[])element[0])); } }
@Test public void testBase32BinarySamplesReverse() throws Exception { final Base32 codec = new Base32(); for (final Object[] element : BASE32_BINARY_TEST_CASES) { assertArrayEquals((byte[])element[0], codec.decode((String)element[1])); } }
@Test public void testSingleCharEncoding() { for (int i = 0; i < 20; i++) { Base32 codec = new Base32(); final BaseNCodec.Context context = new BaseNCodec.Context(); final byte unencoded[] = new byte[i]; final byte allInOne[] = codec.encode(unencoded); codec = new Base32(); for (int j=0; j< unencoded.length; j++) { codec.encode(unencoded, j, 1, context); } codec.encode(unencoded, 0, -1, context); final byte singly[] = new byte[allInOne.length]; codec.readResults(singly, 0, 100, context); if (!Arrays.equals(allInOne, singly)){ fail(); } } }
@Test public void testRandomBytes() { for (int i = 0; i < 20; i++) { final Base32 codec = new Base32(); final byte[][] b = Base32TestData.randomData(codec, i); assertEquals(""+i+" "+codec.lineLength,b[1].length,codec.getEncodedLength(b[0])); //assertEquals(b[0],codec.decode(b[1])); } }
@Test public void testRandomBytesChunked() { for (int i = 0; i < 20; i++) { final Base32 codec = new Base32(10); final byte[][] b = Base32TestData.randomData(codec, i); assertEquals(""+i+" "+codec.lineLength,b[1].length,codec.getEncodedLength(b[0])); //assertEquals(b[0],codec.decode(b[1])); } }
@Test public void testRandomBytesHex() { for (int i = 0; i < 20; i++) { final Base32 codec = new Base32(true); final byte[][] b = Base32TestData.randomData(codec, i); assertEquals(""+i+" "+codec.lineLength,b[1].length,codec.getEncodedLength(b[0])); //assertEquals(b[0],codec.decode(b[1])); } }
final BaseNCodec codec = new Base32(); for (int i = 0; i <= 150; i++) { final byte[][] randomData = Base32TestData.randomData(codec, i);
/** * Tests the Base32InputStream implementation. * * @throws Exception * for some failure scenarios. */ @Test public void testBase32InputStreamByteByByte() throws Exception { // Hello World test. byte[] encoded = StringUtils.getBytesUtf8(Base32TestData.BASE32_FIXTURE); byte[] decoded = StringUtils.getBytesUtf8(Base32TestData.STRING_FIXTURE); testByteByByte(encoded, decoded, BaseNCodec.MIME_CHUNK_SIZE, CRLF); // Single Byte test. encoded = StringUtils.getBytesUtf8("AA======\r\n"); decoded = new byte[] { (byte) 0 }; testByteByByte(encoded, decoded, BaseNCodec.MIME_CHUNK_SIZE, CRLF); // // Single Line test. // String singleLine = Base32TestData.ENCODED_32_CHARS_PER_LINE.replaceAll("\n", ""); // encoded = StringUtils.getBytesUtf8(singleLine); // decoded = Base32TestData.DECODED; // testByteByByte(encoded, decoded, 0, LF); // test random data of sizes 0 thru 150 final BaseNCodec codec = new Base32(); for (int i = 0; i <= 150; i++) { final byte[][] randomData = Base32TestData.randomData(codec, i); encoded = randomData[1]; decoded = randomData[0]; testByteByByte(encoded, decoded, 0, LF); } }