/** * Serializes the {@code message} into an {@link OutputStream} using the given schema. * * @return the size of the message */ public static <T> int writeTo(OutputStream out, T message, Schema<T> schema, LinkedBuffer buffer) throws IOException { if (buffer.start != buffer.offset) throw new IllegalArgumentException("Buffer previously used and had not been reset."); final ProtobufOutput output = new ProtobufOutput(buffer); schema.writeTo(output, message); return LinkedBuffer.writeTo(out, buffer); }
/** * Serializes the {@code message} into an {@link OutputStream} using the given schema. * * @return the size of the message */ public static <T> int writeTo(final OutputStream out, final T message, final Schema<T> schema, final LinkedBuffer buffer) throws IOException { if (buffer.start != buffer.offset) throw new IllegalArgumentException("Buffer previously used and had not been reset."); final ProtostuffOutput output = new ProtostuffOutput(buffer, out); schema.writeTo(output, message); LinkedBuffer.writeTo(out, buffer); return output.size; }
/** * Serializes the {@code message}, prefixed with its length, into an {@link OutputStream}. * * @return the size of the message */ public static <T> int writeDelimitedTo(final OutputStream out, final T message, final Schema<T> schema, final LinkedBuffer buffer) throws IOException { if (buffer.start != buffer.offset) throw new IllegalArgumentException("Buffer previously used and had not been reset."); final ProtostuffOutput output = new ProtostuffOutput(buffer); schema.writeTo(output, message); ProtobufOutput.writeRawVarInt32Bytes(out, output.size); LinkedBuffer.writeTo(out, buffer); return output.size; }
/** * Serializes the {@code message} into an {@link OutputStream} using the given schema. * * @return the size of the message */ public static <T> int writeTo(final OutputStream out, final T message, final Schema<T> schema, final LinkedBuffer buffer) throws IOException { if (buffer.start != buffer.offset) throw new IllegalArgumentException("Buffer previously used and had not been reset."); final ProtostuffOutput output = new ProtostuffOutput(buffer, out); final GraphProtostuffOutput graphOutput = new GraphProtostuffOutput(output); schema.writeTo(graphOutput, message); LinkedBuffer.writeTo(out, buffer); return output.size; }
/** * Serializes the {@code message}, prefixed with its length, into an {@link OutputStream}. * * @return the size of the message */ public static <T> int writeDelimitedTo(final OutputStream out, final T message, final Schema<T> schema, final LinkedBuffer buffer) throws IOException { if (buffer.start != buffer.offset) throw new IllegalArgumentException("Buffer previously used and had not been reset."); final ProtostuffOutput output = new ProtostuffOutput(buffer); final GraphProtostuffOutput graphOutput = new GraphProtostuffOutput(output); schema.writeTo(graphOutput, message); ProtobufOutput.writeRawVarInt32Bytes(out, output.size); LinkedBuffer.writeTo(out, buffer); return output.size; }
/** * Serializes the {@code message}, prefixed with its length, into an {@link OutputStream}. * * @return the size of the message */ public static <T> int writeDelimitedTo(OutputStream out, T message, Schema<T> schema, LinkedBuffer buffer) throws IOException { if (buffer.start != buffer.offset) throw new IllegalArgumentException("Buffer previously used and had not been reset."); final ProtobufOutput output = new ProtobufOutput(buffer); schema.writeTo(output, message); final int size = output.getSize(); ProtobufOutput.writeRawVarInt32Bytes(out, size); final int msgSize = LinkedBuffer.writeTo(out, buffer); assert size == msgSize; return size; }
/** * Used by the code generated messages that implement {@link java.io.Externalizable}. Writes to the * {@link DataOutput} . * * @return the size of the message. */ public static <T> int writeDelimitedTo(DataOutput out, T message, Schema<T> schema) throws IOException { final LinkedBuffer buffer = new LinkedBuffer(LinkedBuffer.MIN_BUFFER_SIZE); final ProtostuffOutput output = new ProtostuffOutput(buffer); schema.writeTo(output, message); ProtobufOutput.writeRawVarInt32Bytes(out, output.size); LinkedBuffer.writeTo(out, buffer); return output.size; }
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); }
/** * Serializes the {@code message} into an {@link OutputStream} via {@link JsonXOutput} using the given * {@code schema}. */ public static <T> void writeTo(OutputStream out, T message, Schema<T> schema, boolean numeric, LinkedBuffer buffer) throws IOException { if (buffer.start != buffer.offset) throw new IllegalArgumentException("Buffer previously used and had not been reset."); final JsonXOutput output = new JsonXOutput(buffer, out, numeric, schema); output.writeStartObject(); schema.writeTo(output, message); if (output.isLastRepeated()) output.writeEndArray(); output.writeEndObject(); LinkedBuffer.writeTo(out, buffer); }
@Override protected <T> byte[] toByteArray(T message, Schema<T> schema) { ByteArrayOutputStream out = new ByteArrayOutputStream(); LinkedBuffer buffer = buf(); final KvpOutput output = new KvpOutput(buffer, out, schema, numeric); try { schema.writeTo(output, message); LinkedBuffer.writeTo(out, buffer); } catch (IOException e) { throw new RuntimeException("Serializing to a byte array threw an IOException " + "(should never happen).", e); } byte[] data = out.toByteArray(); // System.err.println(data.length + " | " + getClass().getSimpleName()); return data; }
/** * Used by the code generated messages that implement {@link java.io.Externalizable}. Writes to the * {@link DataOutput} . * * @return the size of the message. */ public static <T> int writeDelimitedTo(DataOutput out, T message, Schema<T> schema) throws IOException { final LinkedBuffer buffer = new LinkedBuffer(LinkedBuffer.MIN_BUFFER_SIZE); final ProtostuffOutput output = new ProtostuffOutput(buffer); final GraphProtostuffOutput graphOutput = new GraphProtostuffOutput(output); schema.writeTo(graphOutput, message); ProtobufOutput.writeRawVarInt32Bytes(out, output.size); LinkedBuffer.writeTo(out, buffer); return output.size; }
/** * Used by the code generated messages that implement {@link java.io.Externalizable}. Writes to the * {@link DataOutput} . * * @return the size of the message. */ public static <T> int writeDelimitedTo(DataOutput out, T message, Schema<T> schema) throws IOException { final LinkedBuffer buffer = new LinkedBuffer(LinkedBuffer.MIN_BUFFER_SIZE); final ProtobufOutput output = new ProtobufOutput(buffer); schema.writeTo(output, message); final int size = output.getSize(); ProtobufOutput.writeRawVarInt32Bytes(out, size); final int msgSize = LinkedBuffer.writeTo(out, buffer); assert size == msgSize; return size; }
public void testInt() throws Exception { for (int i : int_targets) { ByteArrayOutputStream out = new ByteArrayOutputStream(); LinkedBuffer lb = new LinkedBuffer(BUF_SIZE); WriteSession session = new WriteSession(lb, out); StreamedStringSerializer.writeInt(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.writeInt(i, session2, lb2); LinkedBuffer.writeTo(out2, lb2); byte[] buffered = out.toByteArray(); byte[] buffered_needed_to_flush = out2.toByteArray(); byte[] builtin = STRING.ser(Integer.toString(i)); assertEquals(builtin, buffered); assertEquals(builtin, buffered_needed_to_flush); } }
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); }
public void testDouble() throws Exception { for (double i : double_targets) { ByteArrayOutputStream out = new ByteArrayOutputStream(); LinkedBuffer lb = new LinkedBuffer(BUF_SIZE); WriteSession session = new WriteSession(lb, out); StreamedStringSerializer.writeDouble(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.writeDouble(i, session2, lb2); LinkedBuffer.writeTo(out2, lb2); byte[] buffered = out.toByteArray(); byte[] buffered_needed_to_flush = out2.toByteArray(); byte[] builtin = STRING.ser(Double.toString(i)); assertEquals(builtin, buffered); assertEquals(builtin, buffered_needed_to_flush); } }
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); } }
static void checkVarDelimited(CharSequence str, int size, int stringLen) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); LinkedBuffer lb = new LinkedBuffer(BUF_SIZE); WriteSession session = new WriteSession(lb, out); StreamedStringSerializer.writeUTF8VarDelimited(str, session, lb); LinkedBuffer.writeTo(out, lb); byte[] buf = out.toByteArray(); assertTrue(buf.length == stringLen + size); int len = readRawVarint32(buf, 0); assertTrue(len == stringLen); print("total len: " + buf.length); }
static void check(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.writeUTF8(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, "UTF-8"); String strBuffered = new String(buffered, "UTF-8"); assertEquals(strBuiltin, strBuffered); assertTrue(Arrays.equals(builtin, buffered)); assertEquals(STRING.deser(builtin), STRING.deser(buffered)); print(strBuiltin); print("len: " + builtin.length); }
static void testStream(String str, String prefix, LinkedBuffer tail) throws IOException { byte[] copy = new byte[tail.offset - tail.start]; System.arraycopy(tail.buffer, tail.start, copy, 0, tail.offset - tail.start); byte[] data = str.getBytes(); ByteArrayOutputStream out = new ByteArrayOutputStream(); WriteSession session = new WriteSession(tail, out); session.size += (tail.offset - tail.start); tail = B64Code.sencode(data, 0, data.length, session, tail); assertTrue(tail == session.head); LinkedBuffer.writeTo(out, tail); byte[] dataFromStream = out.toByteArray(); verifyB64(str, prefix, dataFromStream); WriteSession bws = new WriteSession(new LinkedBuffer(copy, 0)); bws.tail.offset += copy.length; bws.size += copy.length; bws.tail = B64Code.encode(data, 0, data.length, bws, bws.tail); assertTrue(Arrays.equals(dataFromStream, bws.toByteArray())); // System.err.println(gg.length + " == " + decoded.length + " | " + // str.equals(strd) + " | " + str + " == " + strd); }
public void testBarList() throws Exception { Bar barCompare = bar; ArrayList<Bar> list = new ArrayList<Bar>(); list.add(bar); list.add(negativeBar); ByteArrayOutputStream out = new ByteArrayOutputStream(); int total = writeListTo(out, list, barCompare.cachedSchema()); ByteArrayOutputStream out2 = new ByteArrayOutputStream(); LinkedBuffer buffer = new LinkedBuffer(512); int total2 = writeListTo(buffer, list, barCompare.cachedSchema()); LinkedBuffer.writeTo(out2, buffer); byte[] data = out.toByteArray(); byte[] data2 = out2.toByteArray(); assertTrue(data.length == total); assertTrue(data2.length == total2); assertTrue(total == total2); String text = STRING.deser(data); String text2 = STRING.deser(data2); assertEquals(text, text2); print(text); }