Java Code Examples for io.protostuff.LinkedBuffer#clear()

The following examples show how to use io.protostuff.LinkedBuffer#clear() . 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: ProtoStuffSerialize.java    From Voovan with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] serialize(Object obj) {
    //字节数组直接返回
    if(obj instanceof byte[]) {
        return (byte[])obj;
    }

    byte[] buf = null;
    buf = TByte.toBytes(obj);
    if(buf==null) {
        Schema schema = getSchema(obj.getClass());
        LinkedBuffer buffer = threadBufferPool.get(()->LinkedBuffer.allocate(512));
        try {
            buf = ProtostuffIOUtil.toByteArray(obj, schema, buffer);
        } finally {
            buffer.clear();
        }
    }

    byte[] type = TByte.getBytes(TSerialize.getHashByClass(obj.getClass()));
    buf = TByte.byteArrayConcat(type, type.length, buf, buf.length);

    return buf;
}
 
Example 2
Source File: ProtostuffSerializer.java    From kafka_book_demo with Apache License 2.0 6 votes vote down vote up
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 jim-framework with Apache License 2.0 6 votes vote down vote up
public static <T> byte[] serialize(T obj) {
    if (obj == null) {
        throw new RpcException("ProtoStuffSerialize.serialize: obj is null");
    }
    if(obj instanceof List){
        return serializeList((List)obj);
    }

    Schema<T> schema = (Schema<T>) RuntimeSchema.getSchema(obj.getClass());

    LinkedBuffer buffer = LinkedBuffer.allocate();
    try {
        return ProtostuffIOUtil.toByteArray(obj, schema, buffer);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        buffer.clear();
    }
}
 
Example 4
Source File: ProtostuffRowSerializationSchema.java    From alchemy with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] serialize(Row row) {
    if(this.schema == null){
        this.schema = RuntimeSchema.getSchema(clazz);
    }
    LinkedBuffer buf = THREAD_LOCAL.get();
    try {
        Object object = schema.newMessage();
        ConvertRowUtil.convertFromRow(object, ((RowTypeInfo)typeInfo).getFieldNames(), row);
        return ProtostuffIOUtil.toByteArray(object, schema, buf);
    } catch (Throwable t) {
        throw new RuntimeException(
            "Could not serialize row '" + row + "'. " + "Make sure that the schema matches the input.", t);
    } finally {
        buf.clear();
    }
}
 
Example 5
Source File: ProtoStuffSerializer.java    From bitchat with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public byte[] serialize(Object object) {
    LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
    byte[] bytes = null;
    try {
        Class clazz = object.getClass();
        Schema schema = getSchema(clazz);
        bytes = ProtostuffIOUtil.toByteArray(object, schema, buffer);
    } catch (Exception e) {
        throw new IllegalStateException(e.getMessage(), e);
    } finally {
        buffer.clear();
    }
    return bytes;
}
 
Example 6
Source File: ProtostuffSerializer.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@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 7
Source File: PurchaseInfoSerializer.java    From ChengFeng1.5 with MIT License 6 votes vote down vote up
@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 8
Source File: MessageSerializer.java    From protect with MIT License 5 votes vote down vote up
/**
 * 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 9
Source File: ProtostuffSerializer.java    From tx-lcn with Apache License 2.0 5 votes vote down vote up
@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();
    }
}
 
Example 10
Source File: ProtostuffSerializer.java    From tx-lcn with Apache License 2.0 5 votes vote down vote up
@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 11
Source File: MessageCrypto.java    From jeesupport with MIT License 5 votes vote down vote up
/**
 * 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 12
Source File: MessageSerializer.java    From protect with MIT License 5 votes vote down vote up
/**
 * 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 13
Source File: MessageSerializer.java    From protect with MIT License 5 votes vote down vote up
/**
 * 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 14
Source File: MessageSerializer.java    From protect with MIT License 5 votes vote down vote up
/**
 * Serializes an Relayed Message into a byte string using Java serialization
 * 
 * @param object
 * @return
 */
public static byte[] serializeSignedMessage(final SignedMessage signedMessage) {

	// 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(signedMessage, SIGNED_MESSAGE_SCHEMA, buffer);
	} finally {
		// Release buffer
		buffer.clear();
	}

}
 
Example 15
Source File: ProtoStuffSerializer.java    From Thunder with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> byte[] serialize(T object) {
    Schema<T> schema = getSchema((Class<T>) object.getClass());

    LinkedBuffer buffer = BUFFERS.get();
    try {
        return ProtostuffIOUtil.toByteArray(object, schema, buffer);
    } finally {
        buffer.clear();
    }
}
 
Example 16
Source File: RecordCopier.java    From DataLink with Apache License 2.0 5 votes vote down vote up
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 17
Source File: SerializationUtil.java    From springboot-learn with MIT License 5 votes vote down vote up
public static <T> byte[] serialize(T obj) {
    Class<T> aClass = (Class<T>) obj.getClass();
    LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
    try {
        Schema<T> schema = getSchema(aClass);
        return ProtobufIOUtil.toByteArray(obj, schema, buffer);
    } catch (Exception e) {
        throw new IllegalStateException(e.getMessage(), e);
    } finally {
        buffer.clear();
    }
}
 
Example 18
Source File: LinkedBuffers.java    From Jupiter with Apache License 2.0 4 votes vote down vote up
public static void resetBuf(LinkedBuffer buf) {
    buf.clear();
}
 
Example 19
Source File: CollectionTest.java    From protostuff with Apache License 2.0 4 votes vote down vote up
public void testAA() throws IOException
{
    AA aa = new AA();
    aa.map = new HashMap<String, BB[]>();
    aa.map.put("o1", new BB[]{ new BB(1, 2), new BB(3, 4), new BB(5, 6) });
    aa.map.put("o2", new BB[]{ new BB(10, 11) });

    aa.map2 = new HashMap<BB[], String>();
    aa.map2.put(new BB[]{ new BB(1, 2), new BB(3, 4), new BB(5, 6) }, "o1");

    aa.list = new ArrayList<BB[]>();
    aa.list.add(new BB[]{ new BB(1, 2), new BB(3, 4), new BB(5, 6) });

    LinkedBuffer buffer = LinkedBuffer.allocate(4096);
    Schema<AA> schema = RuntimeSchema.getSchema(AA.class);
    byte[] protostuff = null;
    try
    {
        protostuff = ProtostuffIOUtil.toByteArray(aa, schema, buffer);
    }
    finally
    {
        buffer.clear();
    }

    AA result = schema.newMessage();
    ProtostuffIOUtil.mergeFrom(protostuff, result, schema);

    assertEquals(aa.map.size(), result.map.size());
    assertEquals(aa.map.get("o1").length, result.map.get("o1").length);
    assertEquals(aa.map.get("o2").length, result.map.get("o2").length);
    Iterator<Map.Entry<String, BB[]>> it1 = aa.map.entrySet().iterator();
    Iterator<Map.Entry<String, BB[]>> it2 = result.map.entrySet().iterator();
    for (int i = 0, len = aa.map.size(); i < len; i++)
    {
        Map.Entry<String, BB[]> e1 = it1.next(), e2 = it2.next();
        assertEquals(e1.getKey(), e2.getKey());
        assertArrayEquals(e1.getValue(), e2.getValue());
    }

    assertEquals(aa.map2.size(), result.map2.size());
    Iterator<Map.Entry<BB[], String>> it21 = aa.map2.entrySet().iterator();
    Iterator<Map.Entry<BB[], String>> it22 = result.map2.entrySet().iterator();
    for (int i = 0, len = aa.map2.size(); i < len; i++)
    {
        Map.Entry<BB[], String> e1 = it21.next(), e2 = it22.next();
        assertArrayEquals(e1.getKey(), e2.getKey());
        assertEquals(e1.getValue(), e2.getValue());
    }

    assertEquals(aa.list.size(), result.list.size());
    for (int i = 0, len = aa.list.size(); i < len; i++)
        assertArrayEquals(aa.list.get(i), result.list.get(i));
}
 
Example 20
Source File: LinkedBuffers.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
public static void resetBuf(final LinkedBuffer buf) {
    buf.clear();
}