@Override public JsonXOutput clear() { super.clear(); return this; }
@Benchmark public byte[] bufferedSerializer() { try { final WriteSession session = new WriteSession(sharedBuffer); StringSerializer.writeUTF8(s, session, sharedBuffer); return session.toByteArray(); } finally { sharedBuffer.clear(); } }
private static void flushAndReset(LinkedBuffer node, final WriteSession session) throws IOException { int len; do { if ((len = node.offset - node.start) > 0) node.offset = session.flush(node, node.buffer, node.start, len); } while ((node = node.next) != null); }
@Benchmark public byte[] bufferedRecycledSerializer() { final WriteSession session = this.sharedSession; try { StringSerializer.writeUTF8(s, session, session.head); return session.toByteArray(); } finally { session.clear(); } }
public void testParseInt() throws Exception { assertTrue(0 == NumberParser.parseInt(new byte[] { '0' }, 0, 1, 10)); assertTrue(1 == NumberParser.parseInt(new byte[] { '1' }, 0, 1, 10)); assertTrue(-1 == NumberParser.parseInt(new byte[] { '-', '1' }, 0, 2, 10)); final LinkedBuffer lb = LinkedBuffer.allocate(256); final WriteSession session = new WriteSession(lb); assertTrue(lb == StringSerializer.writeInt( Integer.MAX_VALUE, session, session.tail)); assertTrue(Integer.MAX_VALUE == NumberParser.parseInt( session.toByteArray(), 0, session.size, 10)); session.clear(); assertTrue(lb == StringSerializer.writeInt( Integer.MAX_VALUE - 1, session, session.tail)); assertTrue(Integer.MAX_VALUE - 1 == NumberParser.parseInt( session.toByteArray(), 0, session.size, 10)); session.clear(); assertTrue(lb == StringSerializer.writeInt( Integer.MIN_VALUE, session, session.tail)); assertTrue(Integer.MIN_VALUE == NumberParser.parseInt( session.toByteArray(), 0, session.size, 10)); session.clear(); assertTrue(lb == StringSerializer.writeInt( Integer.MIN_VALUE + 1, session, session.tail)); assertTrue(Integer.MIN_VALUE + 1 == NumberParser.parseInt( session.toByteArray(), 0, session.size, 10)); }
public static byte[] toByteArray(Output output) { if (output instanceof WriteSession) { return ((WriteSession) output).toByteArray(); } throw new IllegalArgumentException("Output [" + Reflects.simpleClassName(output) + "] must be a WriteSession's implementation"); }
@Setup public void prepare() throws IOException { sharedBuffer = LinkedBuffer.allocate(512); sharedSession = new WriteSession(sharedBuffer); StringBuilder sb = new StringBuilder(); for (int i = 0; i < stringLength; i++) { sb.append('.'); } s = sb.toString(); }
public void testParseLong() throws IOException { assertTrue(0l == NumberParser.parseLong(new byte[] { '0' }, 0, 1, 10)); assertTrue(1l == NumberParser.parseLong(new byte[] { '1' }, 0, 1, 10)); assertTrue(-1l == NumberParser.parseLong(new byte[] { '-', '1' }, 0, 2, 10)); final LinkedBuffer lb = LinkedBuffer.allocate(256); final WriteSession session = new WriteSession(lb); assertTrue(lb == StringSerializer.writeLong( Long.MAX_VALUE, session, session.tail)); assertTrue(Long.MAX_VALUE == NumberParser.parseLong( session.toByteArray(), 0, session.size, 10)); session.clear(); assertTrue(lb == StringSerializer.writeLong( Long.MAX_VALUE - 1, session, session.tail)); assertTrue(Long.MAX_VALUE - 1 == NumberParser.parseLong( session.toByteArray(), 0, session.size, 10)); session.clear(); assertTrue(lb == StringSerializer.writeLong( Long.MIN_VALUE, session, session.tail)); assertTrue(Long.MIN_VALUE == NumberParser.parseLong( session.toByteArray(), 0, session.size, 10)); session.clear(); assertTrue(lb == StringSerializer.writeLong( Long.MIN_VALUE + 1, session, session.tail)); assertTrue(Long.MIN_VALUE + 1 == NumberParser.parseLong( session.toByteArray(), 0, session.size, 10)); }
public static byte[] toByteArray(Output output) { if (output instanceof WriteSession) { return ((WriteSession) output).toByteArray(); } throw new IllegalArgumentException("Output [" + Reflects.simpleClassName(output) + "] must be a WriteSession's implementation"); }
static void checkFixedDelimited(CharSequence str) throws Exception { ByteArrayOutputStream bout = new ByteArrayOutputStream(); OutputStreamWriter writer = new OutputStreamWriter(bout, "UTF-8"); bout.write(getShortStringLengthInBytes(str)); writer.write(str.toString(), 0, str.length()); writer.close(); ByteArrayOutputStream out = new ByteArrayOutputStream(); LinkedBuffer lb = new LinkedBuffer(BUF_SIZE); WriteSession session = new WriteSession(lb, out); StreamedStringSerializer.writeUTF8FixedDelimited(str, session, lb); LinkedBuffer.writeTo(out, lb); byte[] b1 = bout.toByteArray(); byte[] b2 = out.toByteArray(); assertEquals(b1, b2); }
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(); verifyB64(str, prefix, result); }
@Override public XmlXOutput clear() { super.clear(); return this; }
public static byte[] toByteArray(Output output) { if (output instanceof WriteSession) { return ((WriteSession) output).toByteArray(); } throw new IllegalArgumentException("Output [" + Reflects.simpleClassName(output) + "] must be a WriteSession's implementation"); }
static void checkAscii(CharSequence str) throws Exception { byte[] builtin = str.toString().getBytes("UTF-8"); ByteArrayOutputStream out = new ByteArrayOutputStream(); LinkedBuffer lb = new LinkedBuffer(BUF_SIZE); WriteSession session = new WriteSession(lb, out); StreamedStringSerializer.writeAscii(str, session, lb); LinkedBuffer.writeTo(out, lb); assertTrue(builtin.length == session.size); byte[] buffered = out.toByteArray(); assertTrue(builtin.length == buffered.length); String strBuiltin = new String(builtin, "ASCII"); String strBuffered = new String(buffered, "ASCII"); assertEquals(strBuiltin, strBuffered); print(strBuiltin); print("len: " + builtin.length); }
@Override public LinkedBuffer drain(final WriteSession session, final LinkedBuffer lb) throws IOException { // flush and reset lb.offset = session.flush(lb.buffer, lb.start, lb.offset - lb.start); return lb; }
public void testPartialSurrogatePair() throws Exception { // Make sure that we don't overflow or get out of bounds, // since pairs require 2 characters. String partial = "\uD83C"; // 3 bytes can't hold a 4-byte encoding, but we // don't expect it to use the 4-byte encoding path, // since it's not a pair LinkedBuffer lb = new LinkedBuffer(3); ByteArrayOutputStream out = new ByteArrayOutputStream(); WriteSession session = new WriteSession(lb, out); StreamedStringSerializer.writeUTF8(partial, session, lb); byte[] buffered = session.toByteArray(); }
/** * Resets this output for re-use. */ @Override public ProtostuffOutput clear() { super.clear(); return this; }
public static byte[] toByteArray(Output output) { if (output instanceof WriteSession) { return ((WriteSession) output).toByteArray(); } throw new IllegalArgumentException("Output [" + Reflects.simpleClassName(output) + "] must be a WriteSession's implementation"); }
public void testLong() throws Exception { for (long i : long_targets) { ByteArrayOutputStream out = new ByteArrayOutputStream(); LinkedBuffer lb = new LinkedBuffer(BUF_SIZE); WriteSession session = new WriteSession(lb, out); StreamedStringSerializer.writeLong(i, session, lb); LinkedBuffer.writeTo(out, lb); ByteArrayOutputStream out2 = new ByteArrayOutputStream(); LinkedBuffer lb2 = new LinkedBuffer(NUM_BUF_SIZE); WriteSession session2 = new WriteSession(lb2, out2); StreamedStringSerializer.writeLong(i, session2, lb2); LinkedBuffer.writeTo(out2, lb2); byte[] buffered = out.toByteArray(); byte[] buffered_needed_to_flush = out2.toByteArray(); byte[] builtin = STRING.ser(Long.toString(i)); assertEquals(builtin, buffered); assertEquals(builtin, buffered_needed_to_flush); } }
@Override public LinkedBuffer writeByte(final byte value, final WriteSession session, LinkedBuffer lb) throws IOException { session.size++; if (lb.offset == lb.buffer.length) { // flush and reset lb.offset = session.flush(lb.buffer, lb.start, lb.offset - lb.start); } lb.buffer[lb.offset++] = value; return lb; }