static byte[] streamToBytes(final InputStream in, byte[] buf) throws IOException { try { int[] status = fill(buf, 0, in); int size = status[SIZE_KEY]; int lastRead = status[LAST_READ_KEY]; while (lastRead != -1) { buf = resizeArray(buf); status = fill(buf, size, in); size = status[SIZE_KEY]; lastRead = status[LAST_READ_KEY]; } if (buf.length != size) { final byte[] smallerBuf = new byte[size]; System.arraycopy(buf, 0, smallerBuf, 0, size); buf = smallerBuf; } } finally { in.close(); } return buf; }
static byte[] streamToBytes(final InputStream in) throws IOException { // new byte[7] is obviously quite slow, but helps exercise the code. return streamToBytes(in, new byte[7]); }
@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])); } }
byte[] output = Base32TestData.streamToBytes(in); output = Base32TestData.streamToBytes(in); in = new Base32InputStream(in, false); output = Base32TestData.streamToBytes(in);
final byte[][] randomData = Base32TestData.randomData(codec, i); encoded = randomData[1]; decoded = randomData[0];
/** * Tests the problem reported in CODEC-130. Missing / wrong implementation of skip. */ @Test public void testCodec130() throws IOException { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final Base32OutputStream base32os = new Base32OutputStream(bos); base32os.write(StringUtils.getBytesUtf8(STRING_FIXTURE)); base32os.close(); final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); final Base32InputStream ins = new Base32InputStream(bis); // we skip the first character read from the reader ins.skip(1); final byte[] decodedBytes = Base32TestData.streamToBytes(ins, new byte[64]); final String str = StringUtils.newStringUtf8(decodedBytes); assertEquals(STRING_FIXTURE.substring(1), str); }
final byte[][] randomData = Base32TestData.randomData(codec, i); encoded = randomData[1]; decoded = randomData[0];
final byte[][] randomData = Base32TestData.randomData(codec, i); encoded = randomData[1]; decoded = randomData[0];
/** * 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); } }