public <T> byte[] serialize(final T source) { VO<T> vo = new VO<T>(source); final LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE); try { final Schema<VO> schema = getSchema(VO.class); return serializeInternal(vo, schema, buffer); } catch (final Exception e) { throw new IllegalStateException(e.getMessage(), e); } finally { buffer.clear(); } }
public CASAndIndexCollectOutput clear() { if(tail != head) { // overflow size = -1; } // what super.clear() does, except the size is not reset tail = head.clear(); // unset for gc root = null; cas = null; return this; }
protected LinkedBuffer initialValue() { return allocate( bufferSize ); } };
@SuppressWarnings({ "unchecked", "rawtypes" }) private <T> void encode(OutputStream out, T object) throws IOException { LinkedBuffer buffer = LinkedBuffer.allocate(4096); Schema schema = null; if (null == object) { schema = SchemaCache.getSchema(Object.class); } else { schema = SchemaCache.getSchema(object.getClass()); } // write the length header int length = writeObject(buffer, object, schema); IOUtils.writeInt(out, length); // write content 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(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); }
/** * 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; }
@Override public void serialize(T t) throws IOException { LinkedBuffer buff = threadLocalBuffer.get(); ProtobufIOUtil.writeDelimitedTo(out, t, t, buff); buff.clear(); }
@Override protected LinkedBuffer initialValue() { return LinkedBuffer.allocate(500); } };
/** * 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; }
/** * 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; }
@Override public void serialize(final OutputStream output, final Object object) { Class cls = object.getClass(); LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE); try { Schema schema = getSchema(cls); ProtostuffIOUtil.writeTo(output, object, schema, buffer); } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } finally { buffer.clear(); } } }
@Override public void serialize(T t) throws IOException { LinkedBuffer buff = threadLocalBuffer.get(); ProtobufIOUtil.writeDelimitedTo(out, t, t, buff); buff.clear(); }
@Override protected LinkedBuffer initialValue() { return LinkedBuffer.allocate(500); } };
/** * 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; }
/** * 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; }
@Override public byte[] serialize(Object obj) throws SerializerException { Class cls = obj.getClass(); LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE); try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()){ Schema schema = getSchema(cls); ProtostuffIOUtil.writeTo(outputStream, obj, schema, buffer); return outputStream.toByteArray(); } catch (Exception e) { throw new SerializerException(e.getMessage(), e); } finally { buffer.clear(); } }
public JniStream start(JniOp op) { if (reservedSize != 0) return start(op, reservedSize); //super.clear(); tail = head.clear(); size = 0; // reset previousOffset = headerOffset = head.start; // clear System.arraycopy(CLEAR, 0, head.buffer, OFFSET_RC, 8); this.op = op; return this; }
/** * 获取LinkedBuffer实例。 * @return LinkedBuffer实例。 */ public static LinkedBuffer getCurrentLinkedBuffer(int bufferSize) { LinkedBuffer linkedBuffer = currentLinkedBuffer.get(); if (linkedBuffer == null) { linkedBuffer = LinkedBuffer.allocate(bufferSize); currentLinkedBuffer.set(linkedBuffer); } return linkedBuffer; } }
/** * 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; }
@Override public void serialize(Object obj, OutputStream outputStream) throws SerializerException { Class cls = obj.getClass(); LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE); try { Schema schema = getSchema(cls); ProtostuffIOUtil.writeTo(outputStream, obj, schema, buffer); } catch (Exception e) { throw new SerializerException(e.getMessage(), e); }finally { buffer.clear(); } }