Refine search
@Override protected LinkedBuffer initialValue() { return LinkedBuffer.allocate(DEFAULT_BUF_SIZE); } };
/** * The buffer will be cleared (tail will point to the head) and the size will be reset to zero. */ public WriteSession clear() { tail = head.clear(); size = 0; return this; }
/** * 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); }
public LinkedBuffer writeStartObject() { LinkedBuffer objectHeader; if (tail.buffer.length - tail.offset < MAX_MAPHEADER_SIZE) { tail = new LinkedBuffer(this.nextBufferSize, tail); } objectHeader = tail; tail = LinkedBuffer.wrap(tail.buffer, tail.offset + MAX_MAPHEADER_SIZE, 0); objectHeader.next = tail; return objectHeader; }
/** * 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 <T> byte[] toByteArrayStreamedProtostuff(T message, Schema<T> schema) { /* * final ByteArrayOutputStream out = new ByteArrayOutputStream(); try { ProtostuffIOUtil.writeTo(out, message, * schema, new LinkedBuffer(BUF_SIZE)); } catch (IOException e) { throw new * RuntimeException("Serializing to a byte array threw an IOException " + "(should never happen).", e); } return * out.toByteArray(); */ final ByteArrayOutputStream out = new ByteArrayOutputStream(); final LinkedBuffer buffer = new LinkedBuffer(BUF_SIZE); final ProtostuffOutput output = new ProtostuffOutput(buffer, out); 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); } return out.toByteArray(); }
@Override public <T extends Message<T>> byte[] serialize(T message) { try { ByteArrayOutputStream out = new ByteArrayOutputStream(); JsonXIOUtil.writeTo(out, message, true, buffer); return out.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); } finally { buffer.clear(); } }
@Test public void testSerializeDeserializeNumericEnum() throws Exception { RuntimeSchema<A> schema = RuntimeSchema.createFrom(A.class); A source = new A(TaggedEnum.TEN); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); LinkedBuffer buffer = LinkedBuffer.allocate(); ProtostuffIOUtil.writeTo(outputStream, source, schema, buffer); byte[] bytes = outputStream.toByteArray(); A newMessage = schema.newMessage(); ProtostuffIOUtil.mergeFrom(bytes, newMessage, schema); Assert.assertEquals(source, newMessage); }
private ByteBuffer testObj(Message msg, Schema sch) throws java.io.IOException ByteArrayOutputStream controlStream = new ByteArrayOutputStream(); LinkedBuffer linkedBuffer = LinkedBuffer.allocate(512); // meh ProtostuffIOUtil.writeTo(controlStream, msg, sch, linkedBuffer); byte[] controlData = controlStream.toByteArray(); sch.writeTo(lcpo, msg); List<ByteBuffer> testDatas = lcpo.buffer.finish();
@Test public void forceUseSunReflectionFactory() throws Exception { System.setProperty("protostuff.runtime.always_use_sun_reflection_factory", "true"); Schema<MyClass> schema = RuntimeSchema.getSchema(MyClass.class); ByteArrayOutputStream output = new ByteArrayOutputStream(); MyClass myClass = new MyClass(); // constructor initializes list with one element ProtostuffIOUtil.writeTo(output, myClass, schema, LinkedBuffer.allocate()); byte[] bytes = output.toByteArray(); Assert.assertEquals(1, myClass.getList().size()); MyClass myClassNew = schema.newMessage(); // default constructor should not be used ProtostuffIOUtil.mergeFrom(bytes, myClassNew, schema); Assert.assertEquals(1, myClassNew.getList().size()); }
public void testMultipleLargeStringsExceedingBufferSize() throws Exception { LinkedBuffer buffer = LinkedBuffer.allocate(256); ByteArrayOutputStream out = new ByteArrayOutputStream(); WriteSession session = new WriteSession(buffer, out); String utf8OneByte = repeatChar('a', 1024); String utf8TwoBytes = repeatChar((char) 0x7ff, 1024 / 2); String utf8ThreeBytes = repeatChar((char) 0x800, 1024 / 3); writeToSession(utf8OneByte, utf8TwoBytes, utf8ThreeBytes, session, false); assertTrue(session.tail == session.head); // flush remaining LinkedBuffer.writeTo(out, buffer); // clear buffer.clear(); byte[] data = out.toByteArray(); LinkedBuffer buffer2 = LinkedBuffer.allocate(256); WriteSession session2 = new WriteSession(buffer2); writeToSession(utf8OneByte, utf8TwoBytes, utf8ThreeBytes, session2, false); byte[] data2 = session2.toByteArray(); assertEquals(STRING.deser(data), STRING.deser(data2)); }
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(); }
/** * Serializes the {@code message} into an {@link OutputStream} with the supplied buffer. * * @return the total bytes written to the output. */ 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 YamlOutput output = new YamlOutput(buffer, out, schema); output.tail = YamlOutput.writeTag( schema.messageName(), false, output.sink, output, output.sink.writeByteArray( START_DIRECTIVE, output, buffer)); schema.writeTo(output, message); LinkedBuffer.writeTo(out, buffer); return output.getSize(); }
static <T> byte[] toByteArrayBufferedProtostuff(T message, Schema<T> schema) { final ProtostuffOutput output = new ProtostuffOutput(new LinkedBuffer(BUF_SIZE)); try { schema.writeTo(output, message); } catch (IOException e) { throw new RuntimeException("Serializing to a byte array threw an IOException " + "(should never happen).", e); } return output.toByteArray(); }
/** * Wraps the byte array buffer as a read-only buffer. */ public static LinkedBuffer wrap(byte[] array, int offset, int length) { return new LinkedBuffer(array, offset, offset + length); }
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); }
@Override public <T extends Message<T>> byte[] serialize(T message) { try { ByteArrayOutputStream out = new ByteArrayOutputStream(); JsonXIOUtil.writeTo(out, message, false, buffer); return out.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); } finally { buffer.clear(); } }
public void testProtobuf() throws Exception { Schema<Entity> schema = RuntimeSchema.getSchema(Entity.class); Entity p = filledEntity(); byte[] data = ProtobufIOUtil.toByteArray(p, schema, LinkedBuffer.allocate(512)); Entity p2 = new Entity(); ProtostuffIOUtil.mergeFrom(data, p2, schema); assertEquals(p, p2); List<Entity> list = new ArrayList<Entity>(); list.add(p); list.add(p2); ByteArrayOutputStream out = new ByteArrayOutputStream(); ProtobufIOUtil.writeListTo(out, list, schema, buf()); byte[] listData = out.toByteArray(); ByteArrayInputStream in = new ByteArrayInputStream(listData); List<Entity> parsedList = ProtobufIOUtil.parseListFrom(in, schema); assertEquals(list, parsedList); }
private ByteBuffer testObj(Message msg, Schema sch) throws java.io.IOException ByteArrayOutputStream controlStream = new ByteArrayOutputStream(); LinkedBuffer linkedBuffer = LinkedBuffer.allocate(512); // meh ProtobufIOUtil.writeTo(controlStream, msg, sch, linkedBuffer); byte[] controlData = controlStream.toByteArray(); sch.writeTo(lcpo, msg); List<ByteBuffer> testDatas = lcpo.buffer.finish();
public void checkVarDelimitedBoundry(int initialGap, int secondWriteSize) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); int bufferSize = BUF_SIZE; final LinkedBuffer lb = new LinkedBuffer(bufferSize); WriteSession session = new WriteSession(lb, out, null, bufferSize); // Should fill up the buffer with initialGap byte(s) left StreamedStringSerializer.writeUTF8(repeatChar('a', bufferSize - initialGap), session, lb); // Write a string of length secondWriteSize that should be larger // than the next buffer size assertTrue(secondWriteSize > bufferSize); StreamedStringSerializer.writeUTF8VarDelimited(repeatChar('a', secondWriteSize), session, lb); }