public void testDecodeFromString() throws Exception { for (String str : new String[] {"abcdefgh", "1", "12", "123", "1234", "12345"}) { byte[] b64Encoded = B64Code.encode(str.getBytes("UTF-8")); byte[] decoded = B64Code.decode(b64Encoded); byte[] decodedFromString = B64Code.decode(new String(b64Encoded, "UTF-8")); assertEquals(STRING.deser(decoded), STRING.deser(decodedFromString)); assertEquals(str, STRING.deser(decoded)); } }
static void testBuffer(String str, String prefix, LinkedBuffer tail, int nextBufferSize) throws IOException { byte[] data = str.getBytes(); WriteSession session = new WriteSession(tail, nextBufferSize); session.size += (tail.offset - tail.start); tail = B64Code.encode(data, 0, data.length, session, tail); byte[] result = session.toByteArray(); // System.err.println(result.length - prefix.length()); // System.err.println(new String(result, prefix.length(), result.length - prefix.length())); byte[] decoded = B64Code.decode(result, prefix.length(), result.length - prefix.length()); String strd = new String(decoded); assertEquals(str, strd); }
public void testDecodeTo() throws Exception { for (String str : new String[] {"abcdefgh", "1", "12", "123", "1234", "12345"}) { byte[] b64Encoded = B64Code.encode(str.getBytes("UTF-8")); byte[] decoded = new byte[16]; int decodedLen = B64Code.decodeTo(decoded, 0, b64Encoded, 0, b64Encoded.length); byte[] decodedFromString = B64Code.decode(new String(b64Encoded, "UTF-8")); assertEquals(STRING.deser(decoded, 0, decodedLen), STRING.deser(decodedFromString)); assertEquals(str, STRING.deser(decoded, 0, decodedLen)); } }
@Override public int read() throws IOException { if (_buffer == null || _pos >= _buffer.length) { // Any CR and LF will be consumed by the readLine() call. // We need to put them back into the bytes returned from this // method because the parsing of the multipart content uses them // as markers to determine when we've reached the end of a part. _line = _in.readLine(); if (_line == null) return -1; // nothing left if (_line.startsWith("--")) _buffer = (_line + "\r\n").getBytes(); // boundary marking end of part else if (_line.length() == 0) _buffer = "\r\n".getBytes(); // blank line else { ByteArrayOutputStream baos = new ByteArrayOutputStream((4 * _line.length() / 3) + 2); B64Code.decode(_line, baos); baos.write(13); baos.write(10); _buffer = baos.toByteArray(); } _pos = 0; } return _buffer[_pos++]; }
@Override public int read() throws IOException { if (_buffer == null || _pos >= _buffer.length) { _line = _in.readLine(); if (_line == null) return -1; if (_line.startsWith("--")) _buffer = (_line + "\r\n").getBytes(); else if (_line.length() == 0) _buffer = "\r\n".getBytes(); else _buffer = B64Code.decode(_line); _pos = 0; } return _buffer[_pos++]; }
static void testStream(String str, String prefix, LinkedBuffer tail) throws IOException { byte[] data = str.getBytes(); ByteArrayOutputStream out = new ByteArrayOutputStream(); WriteSession session = new WriteSession(tail, out); session.size += (tail.offset - tail.start); tail = B64Code.encode(data, 0, data.length, session, out, tail); assertTrue(tail == session.head); LinkedBuffer.writeTo(out, tail); byte[] result = out.toByteArray(); // System.err.println(new String(result, prefix.length(), result.length - prefix.length())); byte[] decoded = B64Code.decode(result, prefix.length(), result.length - prefix.length()); String strd = new String(decoded); assertEquals(str, strd); // System.err.println(gg.length + " == " + decoded.length + " | " + // str.equals(strd) + " | " + str + " == " + strd); }