Java Code Examples for io.protostuff.LinkedBuffer#allocate()
The following examples show how to use
io.protostuff.LinkedBuffer#allocate() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProtostuffSerializer.java From sofa-rpc with Apache License 2.0 | 6 votes |
@Override public AbstractByteBuf encode(Object object, Map<String, String> context) throws SofaRpcException { if (object == null) { throw buildSerializeError("Unsupported null message!"); } else if (object instanceof SofaRequest) { return encodeSofaRequest((SofaRequest) object, context); } else if (object instanceof SofaResponse) { return encodeSofaResponse((SofaResponse) object, context); } else { Class clazz = object.getClass(); Schema schema = RuntimeSchema.getSchema(clazz); // Re-use (manage) this buffer to avoid allocating on every serialization LinkedBuffer buffer = LinkedBuffer.allocate(512); // ser try { return new ByteArrayWrapperByteBuf(ProtostuffIOUtil.toByteArray(object, schema, buffer)); } finally { buffer.clear(); } } }
Example 2
Source File: ProtostuffSerializer.java From kafka_book_demo with Apache License 2.0 | 6 votes |
public byte[] serialize(String topic, Company data) { if (data == null) { return null; } Schema schema = (Schema) RuntimeSchema.getSchema(data.getClass()); LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE); byte[] protostuff = null; try { protostuff = ProtostuffIOUtil.toByteArray(data, schema, buffer); } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } finally { buffer.clear(); } return protostuff; }
Example 3
Source File: ProtoStuffSerializeUtil.java From ns4_frame with Apache License 2.0 | 6 votes |
@Deprecated public static <T> byte[] serialize(List<T> lst,Class<T> cl) throws IOException { if (lst == null) { return new byte[0]; } Schema<T> schema = RuntimeSchema.getSchema(cl); ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); LinkedBuffer linkedBuffer = LinkedBuffer.allocate(1024); ProtostuffIOUtil.writeListTo(arrayOutputStream, lst, schema, linkedBuffer); byte[] bs = arrayOutputStream.toByteArray(); arrayOutputStream.close(); return bs; }
Example 4
Source File: SerializationUtil.java From framework with Apache License 2.0 | 6 votes |
/** * Description: <br> * * @author 王伟<br> * @taskId <br> * @param obj * @param <T> T * @return T * @throws UtilException <br> */ @SuppressWarnings("unchecked") public static <T> byte[] serial(final T obj) throws UtilException { if (obj != null && !(obj instanceof Void)) { try { if (obj instanceof Map) { return jdkSerial(obj); } else { Schema<T> schema = RuntimeSchema.getSchema((Class<T>) obj.getClass()); LinkedBuffer buffer = LinkedBuffer.allocate(INIT_SIZE); return ProtostuffIOUtil.toByteArray(obj, schema, buffer); } } catch (Exception e) { throw new UtilException(e, ErrorCodeDef.SERIALIZE_ERROR, obj); } } return null; }
Example 5
Source File: PurchaseInfoSerializer.java From ChengFeng1.5 with MIT License | 6 votes |
@Override public byte[] serialize(String topic, PurchaseInfoDto data) { if(data==null) { return null; } Schema schema = RuntimeSchema.getSchema(data.getClass()); LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE); byte[] protostuff=null; try { protostuff= ProtostuffIOUtil.toByteArray(data, schema, buffer); }catch (Exception e) { log.error(e.getMessage()); }finally { buffer.clear(); } return protostuff; }
Example 6
Source File: ProtostuffSerializer.java From BigData-In-Practice with Apache License 2.0 | 6 votes |
@Override public byte[] serialize(String topic, Company data) { if (data == null) { return null; } Schema schema = RuntimeSchema.getSchema(data.getClass()); LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE); byte[] protostuff = null; try { protostuff = ProtobufIOUtil.toByteArray(data, schema, buffer); } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } finally { buffer.clear(); } return protostuff; }
Example 7
Source File: JobsProtoUtil.java From dremio-oss with Apache License 2.0 | 5 votes |
/** * Generic method to convert Protostuff to Protobuf. Uses LegacyProtobufSerializer because deserializing with the * regular protobuf serializer does not handle repeated fields correctly. * @param protobufParser Parser for protobuf object * @param protostuff Protostuff object to convert * @param <M> Type of Protobuf * @param <T> Type of Protobuff * @return Converted object as Protobuf */ private static <M extends GeneratedMessageV3, T extends Message<T> & Schema<T>> M toBuf(Parser<M> protobufParser, T protostuff) { try { LinkedBuffer buffer = LinkedBuffer.allocate(); byte[] bytes = ProtobufIOUtil.toByteArray(protostuff, protostuff.cachedSchema(), buffer); // LegacyProtobufSerializer is necessary as it deals with stuff/buf grouping differences return LegacyProtobufSerializer.parseFrom(protobufParser, bytes); } catch (InvalidProtocolBufferException e) { throw new IllegalArgumentException("Cannot convert from protostuff to protobuf"); } }
Example 8
Source File: ProtostuffSerializer.java From tx-lcn with Apache License 2.0 | 5 votes |
@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(); } }
Example 9
Source File: MessageSerializer.java From protect with MIT License | 5 votes |
/** * Serializes a Payload into a byte array using Java serialization * * @param object * @return */ public static byte[] serializePayload(final Payload payload) { // Re-use (manage) this buffer to avoid allocating on every serialization final LinkedBuffer buffer = LinkedBuffer.allocate(MAX_MESSAGE_SIZE); try { // Perform serialization return ProtostuffIOUtil.toByteArray(payload, PAYLOAD_SCHEMA, buffer); } finally { // Release buffer buffer.clear(); } }
Example 10
Source File: MessageSerializer.java From protect with MIT License | 5 votes |
/** * Serializes a Message into a byte string using Java serialization * * @param object * @return */ public static byte[] serializeMessage(final Message message) { // Re-use (manage) this buffer to avoid allocating on every serialization final LinkedBuffer buffer = LinkedBuffer.allocate(MAX_MESSAGE_SIZE); try { // Perform serialization return ProtostuffIOUtil.toByteArray(message, MESSAGE_SCHEMA, buffer); } finally { // Release buffer buffer.clear(); } }
Example 11
Source File: MessageSerializer.java From protect with MIT License | 5 votes |
/** * Serializes an RelayedMessage into a byte string using Java serialization * * @param object * @return */ public static byte[] serializeRelayedMessage(final RelayedMessage relayedMessage) { // Re-use (manage) this buffer to avoid allocating on every serialization final LinkedBuffer buffer = LinkedBuffer.allocate(MAX_MESSAGE_SIZE); try { // Perform serialization return ProtostuffIOUtil.toByteArray(relayedMessage, RELAYED_MESSAGE_SCHEMA, buffer); } finally { // Release buffer buffer.clear(); } }
Example 12
Source File: EmptyMessageIT.java From protostuff with Apache License 2.0 | 5 votes |
@Test public void testSerializeDeserialize_EmptyMessage() throws Exception { EmptyMessage source = new EmptyMessage(); LinkedBuffer buffer = LinkedBuffer.allocate(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ProtostuffIOUtil.writeTo(outputStream, source, EMPTY_MESSAGE_SCHEMA, buffer); byte[] bytes = outputStream.toByteArray(); EmptyMessage newMessage = EMPTY_MESSAGE_SCHEMA.newMessage(); ProtostuffIOUtil.mergeFrom(bytes, newMessage, EMPTY_MESSAGE_SCHEMA); Assert.assertEquals(0, bytes.length); }
Example 13
Source File: RecordCopier.java From DataLink with Apache License 2.0 | 5 votes |
private static byte[] serialize(Object obj) { Schema schema = (Schema) RuntimeSchema.getSchema(obj.getClass()); LinkedBuffer buffer = LinkedBuffer.allocate(1024); byte[] bytes = null; try { bytes = ProtostuffIOUtil.toByteArray(obj, schema, buffer); } finally { buffer.clear(); } return bytes; }
Example 14
Source File: BusProtoStuffMessageConverter.java From hawkbit with Eclipse Public License 1.0 | 5 votes |
private static byte[] writeClassHeader(final Class<?> clazz) { final EventType clazzEventType = EventType.from(clazz); if (clazzEventType == null) { LOG.error("There is no mapping to EventType for the given class {}", clazz); throw new MessageConversionException("Missing EventType for given class : " + clazz); } @SuppressWarnings("unchecked") final Schema<Object> schema = (Schema<Object>) RuntimeSchema .getSchema((Class<?>) EventType.class); final LinkedBuffer buffer = LinkedBuffer.allocate(); return ProtobufIOUtil.toByteArray(clazzEventType, schema, buffer); }
Example 15
Source File: BusProtoStuffMessageConverter.java From hawkbit with Eclipse Public License 1.0 | 5 votes |
private static byte[] writeContent(final Object payload) { final Class<?> serializeClass = payload.getClass(); @SuppressWarnings("unchecked") final Schema<Object> schema = (Schema<Object>) RuntimeSchema.getSchema(serializeClass); final LinkedBuffer buffer = LinkedBuffer.allocate(); return ProtobufIOUtil.toByteArray(payload, schema, buffer); }
Example 16
Source File: MessageCrypto.java From jeesupport with MIT License | 5 votes |
/** * S2C * 将对象序列化为byte[] * * @param _obj 序列化对象 * @return 序列化后的byte[]值 */ public static < T > byte[] serializer ( T _obj ) { @SuppressWarnings( "unchecked" ) Class< T > cls = ( Class< T > ) _obj.getClass(); LinkedBuffer buf = LinkedBuffer.allocate( LinkedBuffer.DEFAULT_BUFFER_SIZE ); try { Schema< T > schema = _get_schema( cls ); return ProtostuffIOUtil.toByteArray( _obj , schema , buf ); } catch ( Exception e ) { throw new IllegalStateException( e.getMessage() , e ); } finally { buf.clear(); } }
Example 17
Source File: ProtostuffSerializer.java From dremio-oss with Apache License 2.0 | 4 votes |
@Override protected LinkedBuffer initialValue() { return LinkedBuffer.allocate(256); }
Example 18
Source File: RuntimeSchemaBenchmark.java From protostuff with Apache License 2.0 | 4 votes |
@Setup public void prepare() throws IOException { int1RuntimeSchema = RuntimeSchema.createFrom(Int1.class); int10RuntimeSchema = RuntimeSchema.createFrom(Int10.class); sparseInt1RuntimeSchema = RuntimeSchema.createFrom(SparseInt1.class); sparseInt10RuntimeSchema = RuntimeSchema.createFrom(SparseInt10.class); generatedInt1Schema = GeneratedInt1.getSchema(); generatedInt10Schema = GeneratedInt10.getSchema(); int1 = new Int1(); int1.a0 = 1; int10 = new Int10(); int10.a0 = 1; int10.a1 = 2; int10.a2 = 3; int10.a3 = 4; int10.a4 = 5; int10.a5 = 6; int10.a6 = 7; int10.a7 = 8; int10.a8 = 9; int10.a9 = 10; sparseInt1 = new SparseInt1(); sparseInt1.a0 = 1; sparseInt10 = new SparseInt10(); sparseInt10.a0 = 1; sparseInt10.a1 = 2; sparseInt10.a2 = 3; sparseInt10.a3 = 4; sparseInt10.a4 = 5; sparseInt10.a5 = 6; sparseInt10.a6 = 7; sparseInt10.a7 = 8; sparseInt10.a8 = 9; sparseInt10.a9 = 10; generatedInt1 = new GeneratedInt1(); generatedInt1.setA0(1); generatedInt10 = new GeneratedInt10(); generatedInt10.setA0(1); generatedInt10.setA1(2); generatedInt10.setA2(3); generatedInt10.setA3(4); generatedInt10.setA4(5); generatedInt10.setA5(6); generatedInt10.setA6(7); generatedInt10.setA7(8); generatedInt10.setA8(9); generatedInt10.setA9(10); buffer = LinkedBuffer.allocate(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ProtobufIOUtil.writeTo(outputStream, int1, int1RuntimeSchema, buffer); data_1_int = outputStream.toByteArray(); outputStream.reset(); buffer.clear(); ProtobufIOUtil.writeTo(outputStream, int10, int10RuntimeSchema, buffer); data_10_int = outputStream.toByteArray(); outputStream.reset(); buffer.clear(); }
Example 19
Source File: ProtoStuffSerializeUtil.java From ns4_frame with Apache License 2.0 | 3 votes |
@Deprecated public static <T> byte[] serialize(T t,Class<T> cl) { Schema<T> schema = RuntimeSchema.getSchema(cl); LinkedBuffer linkedBuffer = LinkedBuffer.allocate(1024); return ProtostuffIOUtil.toByteArray(t, schema, linkedBuffer); }
Example 20
Source File: ProtostuffUtils.java From dog with GNU Lesser General Public License v3.0 | 2 votes |
public LinkedBuffer initialValue() { return LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE); }