/** * 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); }
/** * Allocates a new buffer with default size. */ public static LinkedBuffer allocate() { return new LinkedBuffer(DEFAULT_BUFFER_SIZE); }
@Override public LinkedBuffer drain(final WriteSession session, final LinkedBuffer lb) throws IOException { // grow return new LinkedBuffer(session.nextBufferSize, lb); }
@Override public LinkedBuffer writeByte(final byte value, final WriteSession session, LinkedBuffer lb) throws IOException { session.size++; if (lb.offset == lb.buffer.length) { // grow lb = new LinkedBuffer(session.nextBufferSize, lb); } lb.buffer[lb.offset++] = value; return lb; }
/** * Uses the existing byte array as the internal buffer. */ public static LinkedBuffer use(byte[] buffer, int start) { assert start >= 0; if (buffer.length - start < MIN_BUFFER_SIZE) throw new IllegalArgumentException(MIN_BUFFER_SIZE + " is the minimum buffer size."); return new LinkedBuffer(buffer, start, start); }
/** * Allocates a new buffer with the specified size and appends it to the previous buffer. */ public static LinkedBuffer allocate(int size, LinkedBuffer previous) { if (size < MIN_BUFFER_SIZE) throw new IllegalArgumentException(MIN_BUFFER_SIZE + " is the minimum buffer size."); return new LinkedBuffer(size, previous); }
/** * Allocates a new buffer with the specified size. */ public static LinkedBuffer allocate(int size) { if (size < MIN_BUFFER_SIZE) throw new IllegalArgumentException(MIN_BUFFER_SIZE + " is the minimum buffer size."); return new LinkedBuffer(size); }
private void writeStartArray() { if (tail.buffer.length - tail.offset < MAX_ARRAYHEADER_SIZE) { tail = new LinkedBuffer(this.nextBufferSize, tail); } arrayHeader = tail; tail = LinkedBuffer.wrap(tail.buffer, tail.offset + MAX_ARRAYHEADER_SIZE, 0); arrayHeader.next = tail; arraySize = 1; }
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; }
static LinkedBuffer newBuffer(int size, char c, int loops) { LinkedBuffer lb = new LinkedBuffer(size); while (loops-- > 0) { lb.buffer[lb.offset++] = (byte) c; } return lb; }
@Override public LinkedBuffer writeInt16(final int value, final WriteSession session, LinkedBuffer lb) throws IOException { session.size += 2; if (lb.offset + 2 > lb.buffer.length) { // grow lb = new LinkedBuffer(session.nextBufferSize, lb); } IntSerializer.writeInt16(value, lb.buffer, lb.offset); lb.offset += 2; return lb; }
@Override public LinkedBuffer writeInt32(final int value, final WriteSession session, LinkedBuffer lb) throws IOException { session.size += 4; if (lb.offset + 4 > lb.buffer.length) { // grow lb = new LinkedBuffer(session.nextBufferSize, lb); } IntSerializer.writeInt32(value, lb.buffer, lb.offset); lb.offset += 4; return lb; }
public <T> int writeListTo(OutputStream out, List<T> messages, Schema<T> schema) throws IOException { return ProtostuffIOUtil.writeListTo(out, messages, schema, new LinkedBuffer(LinkedBuffer.DEFAULT_BUFFER_SIZE)); }
static <T> byte[] toByteArrayBufferedProtobuf(T message, Schema<T> schema) { final ProtobufOutput output = new ProtobufOutput(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(); }
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(); }
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(); }
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); }
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); }
public void testStream() throws Exception { // everything will fit testStream("1234567", new LinkedBuffer(12)); // need to flush once testStream("1234567", str('a', 4), newBuffer(12, 'a', 4)); // 1 write chunk and encode remaining testStream("1234567", new LinkedBuffer(4)); // no write chunks and encode remaining testStream("1234567", str('a', 1), newBuffer(4, 'a', 1)); // 1 write chunk and encode remaining testStream("1234567", str('a', 4), newBuffer(8, 'a', 4)); }
static void checkVarDelimited(CharSequence str, int size, int stringLen) throws Exception { LinkedBuffer lb = new LinkedBuffer(512); WriteSession session = new WriteSession(lb); StringSerializer.writeUTF8VarDelimited(str, session, lb); byte[] buf = session.toByteArray(); assertTrue(buf.length == stringLen + size); int len = readRawVarint32(lb.buffer, 0); assertTrue(len == stringLen); print("total len: " + buf.length); }