Java Code Examples for io.protostuff.ProtostuffIOUtil#toByteArray()

The following examples show how to use io.protostuff.ProtostuffIOUtil#toByteArray() . 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 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 2
Source File: CompatTest.java    From protostuff with Apache License 2.0 6 votes vote down vote up
@Test
public void testByteArrayCompat()
{
    PojoWithByteArray pwba = new PojoWithByteArray();
    PojoWithByteString pwbs = new PojoWithByteString();
    fill(pwba);
    fill(pwbs);

    byte[] b1 = ProtostuffIOUtil.toByteArray(pwba,
            RuntimeSchema.getSchema(PojoWithByteArray.class), buf());

    byte[] b2 = ProtostuffIOUtil.toByteArray(pwbs,
            RuntimeSchema.getSchema(PojoWithByteString.class), buf());

    assertTrue(Arrays.equals(b1, b2));
}
 
Example 3
Source File: InitTask.java    From jseckill with Apache License 2.0 6 votes vote down vote up
/**
 * 预热秒杀数据到Redis
 */
private void initRedis() {
    Jedis jedis = jedisPool.getResource();
    //清空Redis缓存
    jedis.flushDB();

    List<Seckill> seckillList = seckillDAO.queryAll(0, 10);
    if (seckillList == null || seckillList.size()< 1) {
        logger.info("--FatalError!!! seckill_list_data is empty");
        return;
    }

    for (Seckill seckill : seckillList) {
        jedis.sadd(RedisKey.SECKILL_ID_SET, seckill.getSeckillId() + "");

        String inventoryKey = RedisKeyPrefix.SECKILL_INVENTORY + seckill.getSeckillId();
        jedis.set(inventoryKey, String.valueOf(seckill.getInventory()));

        String seckillGoodsKey = RedisKeyPrefix.SECKILL_GOODS + seckill.getSeckillId();
        byte[] goodsBytes = ProtostuffIOUtil.toByteArray(seckill, MyRuntimeSchema.getInstance().getGoodsRuntimeSchema(),
                LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
        jedis.set(seckillGoodsKey.getBytes(), goodsBytes);
    }
    jedis.close();
    logger.info("Redis缓存数据初始化完毕!");
}
 
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: 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 6
Source File: PBTest.java    From blog with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static void main(String[] args) {
	Bean1 bean1 = new Bean1("haha1", new BigDecimal("1.00"));
	Bean2 bean2 = new Bean2("haha2", new BigDecimal("2.00"));

	LinkedBuffer buffer1 = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
	Schema schema1 = RuntimeSchema.createFrom(bean1.getClass());
	byte[] bytes1 = ProtostuffIOUtil.toByteArray(bean1, schema1, buffer1);

	Bean1 bean11 = new Bean1();
	ProtostuffIOUtil.mergeFrom(bytes1, bean11, schema1);
	System.out.println(bean11.toString());

	LinkedBuffer buffer2 = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
	Schema schema2 = RuntimeSchema.createFrom(bean2.getClass());
	byte[] bytes2 = ProtostuffIOUtil.toByteArray(bean2, schema2, buffer2);

	Bean2 bean22 = new Bean2();
	ProtostuffIOUtil.mergeFrom(bytes2, bean22, schema2);
	System.out.println(bean22.toString());

}
 
Example 7
Source File: RedisDAO.java    From jseckill with Apache License 2.0 6 votes vote down vote up
public void setAllGoods(List<Seckill> list) {
    Jedis jedis = jedisPool.getResource();
    if (list == null || list.size()< 1) {
        logger.info("--FatalError!!! seckill_list_data is empty");
        return;
    }

    jedis.del(RedisKey.SECKILL_ID_SET);

    for (Seckill seckill : list) {
        jedis.sadd(RedisKey.SECKILL_ID_SET, seckill.getSeckillId() + "");

        String seckillGoodsKey = RedisKeyPrefix.SECKILL_GOODS + seckill.getSeckillId();
        byte[] goodsBytes = ProtostuffIOUtil.toByteArray(seckill, MyRuntimeSchema.getInstance().getGoodsRuntimeSchema(),
                LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
        jedis.set(seckillGoodsKey.getBytes(), goodsBytes);
    }
    jedis.close();
    logger.info("数据库Goods数据同步到Redis完毕!");
}
 
Example 8
Source File: ProtobufUtils.java    From spring-boot-starter-grpc with MIT License 5 votes vote down vote up
/**
 * 序列化方法,将对象序列化为字节数组(对象 ---> 字节数组)
 */
@SuppressWarnings("unchecked")
public static <T> byte[] serialize(T obj) {
    Class<T> clazz = (Class<T>) obj.getClass();
    RuntimeSchema<T> schema = getSchema(clazz);
    LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
    return ProtostuffIOUtil.toByteArray(obj, schema, buffer);
}
 
Example 9
Source File: StoragePluginId.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@JsonIgnore
public SourceConfig getClonedConfig() {
  byte[] bytes = ProtostuffIOUtil.toByteArray(config, SourceConfig.getSchema(), LinkedBuffer.allocate());
  SourceConfig newConfig = new SourceConfig();
  ProtostuffIOUtil.mergeFrom(bytes, newConfig, SourceConfig.getSchema());
  return newConfig;

}
 
Example 10
Source File: PojoWithArrayAndSet.java    From protostuff with Apache License 2.0 5 votes vote down vote up
private void writeObject(ObjectOutputStream out) throws IOException
{
    byte[] data = ProtostuffIOUtil.toByteArray(this,
            RuntimeSchema.getSchema(PojoWithArrayAndSet.class),
            LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
    out.writeInt(data.length);
    out.write(data);
    out.close();
}
 
Example 11
Source File: ProtostuffNullArrayElementInObjectArrayTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
protected <T> void roundTrip(T message, Schema<T> schema,
        Pipe.Schema<T> pipeSchema) throws IOException
{
    byte[] protobuf = ProtobufIOUtil.toByteArray(message, schema, buf());

    ByteArrayInputStream protobufStream = new ByteArrayInputStream(protobuf);

    byte[] protostuff = ProtostuffIOUtil.toByteArray(
            ProtobufIOUtil.newPipe(protobuf, 0, protobuf.length),
            pipeSchema, buf());

    byte[] protostuffFromStream = ProtostuffIOUtil.toByteArray(
            ProtobufIOUtil.newPipe(protobufStream), pipeSchema, buf());

    assertTrue(protostuff.length == protostuffFromStream.length);
    assertEquals(STRING.deser(protostuff),
            STRING.deser(protostuffFromStream));

    ByteArrayInputStream protostuffStream = new ByteArrayInputStream(
            protostuff);

    byte[] protobufRoundTrip = ProtobufIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuff, 0, protostuff.length),
            pipeSchema, buf());

    byte[] protobufRoundTripFromStream = ProtobufIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuffStream), pipeSchema, buf());

    assertTrue(protobufRoundTrip.length == protobufRoundTripFromStream.length);

    String strProtobufRoundTrip = STRING.deser(protobufRoundTrip);

    assertEquals(strProtobufRoundTrip,
            STRING.deser(protobufRoundTripFromStream));

    assertTrue(protobufRoundTrip.length == protobuf.length);

    assertEquals(strProtobufRoundTrip, STRING.deser(protobuf));
}
 
Example 12
Source File: ProtostuffRuntimeMapTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
protected <T> void roundTrip(T message, Schema<T> schema,
        Pipe.Schema<T> pipeSchema) throws Exception
{
    byte[] protobuf = ProtobufIOUtil.toByteArray(message, schema, buf());

    ByteArrayInputStream protobufStream = new ByteArrayInputStream(protobuf);

    byte[] protostuff = ProtostuffIOUtil.toByteArray(
            ProtobufIOUtil.newPipe(protobuf, 0, protobuf.length),
            pipeSchema, buf());

    byte[] protostuffFromStream = ProtostuffIOUtil.toByteArray(
            ProtobufIOUtil.newPipe(protobufStream), pipeSchema, buf());

    assertTrue(Arrays.equals(protostuff, protostuffFromStream));

    T parsedMessage = schema.newMessage();
    ProtostuffIOUtil.mergeFrom(protostuff, parsedMessage, schema);
    SerializableObjects.assertEquals(message, parsedMessage);

    ByteArrayInputStream protostuffStream = new ByteArrayInputStream(
            protostuff);

    byte[] protobufRoundTrip = ProtobufIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuff, 0, protostuff.length),
            pipeSchema, buf());

    byte[] protobufRoundTripFromStream = ProtobufIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuffStream), pipeSchema, buf());

    assertTrue(Arrays.equals(protobufRoundTrip, protobufRoundTripFromStream));

    assertTrue(Arrays.equals(protobufRoundTrip, protobuf));
}
 
Example 13
Source File: ProtobufNullArrayElementInObjectArrayTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Override
protected <T> void roundTrip(T message, Schema<T> schema,
        Pipe.Schema<T> pipeSchema) throws IOException
{
    byte[] protobuf = ProtobufIOUtil.toByteArray(message, schema, buf());

    ByteArrayInputStream protobufStream = new ByteArrayInputStream(protobuf);

    byte[] protostuff = ProtostuffIOUtil.toByteArray(
            ProtobufIOUtil.newPipe(protobuf, 0, protobuf.length),
            pipeSchema, buf());

    byte[] protostuffFromStream = ProtostuffIOUtil.toByteArray(
            ProtobufIOUtil.newPipe(protobufStream), pipeSchema, buf());

    assertTrue(protostuff.length == protostuffFromStream.length);
    assertEquals(STRING.deser(protostuff),
            STRING.deser(protostuffFromStream));

    ByteArrayInputStream protostuffStream = new ByteArrayInputStream(
            protostuff);

    byte[] protobufRoundTrip = ProtobufIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuff, 0, protostuff.length),
            pipeSchema, buf());

    byte[] protobufRoundTripFromStream = ProtobufIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuffStream), pipeSchema, buf());

    assertTrue(protobufRoundTrip.length == protobufRoundTripFromStream.length);

    String strProtobufRoundTrip = STRING.deser(protobufRoundTrip);

    assertEquals(strProtobufRoundTrip,
            STRING.deser(protobufRoundTripFromStream));

    assertTrue(protobufRoundTrip.length == protobuf.length);

    assertEquals(strProtobufRoundTrip, STRING.deser(protobuf));
}
 
Example 14
Source File: ProtoStuffSerializer.java    From litchi with Apache License 2.0 5 votes vote down vote up
@Override
public <T> byte[] encode(T obj) throws IOException {

	Class<?> clazz = obj.getClass();
	LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
	try {
		@SuppressWarnings("unchecked")
		Schema<T> schema = (Schema<T>) getSchema(clazz);
		return ProtostuffIOUtil.toByteArray(obj, schema, buffer);
	} catch (Exception e) {
		throw new IllegalStateException(e.getMessage(), e);
	} finally {
		buffer.clear();
	}
}
 
Example 15
Source File: CacheObjectSerializer.java    From nuls with MIT License 5 votes vote down vote up
@Override
    public ByteBuffer serialize(T o) throws SerializerException {
//        System.out.println("serialize+++" + o);
        byte[] bytes = ProtostuffIOUtil.toByteArray(o, schema, LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
//        System.out.println("hahaha===========" + bytes.length);
        return ByteBuffer.wrap(bytes);
    }
 
Example 16
Source File: ProtostuffUtil.java    From blade-tool with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 序列化方法,把指定对象序列化成字节数组
 *
 * @param obj obj
 * @param <T> T
 * @return byte[]
 */
@SuppressWarnings("unchecked")
public static <T> byte[] serialize(T obj) {
	Class<T> clazz = (Class<T>) obj.getClass();
	Schema<T> schema = getSchema(clazz);
	byte[] data;
	try {
		data = ProtostuffIOUtil.toByteArray(obj, schema, buffer);
	} finally {
		buffer.clear();
	}
	return data;
}
 
Example 17
Source File: RuntimeViewTest.java    From protostuff with Apache License 2.0 4 votes vote down vote up
static byte[] ser(Schema<Baz> schema)
{
    return ProtostuffIOUtil.toByteArray(BAZ, schema, buf());
}
 
Example 18
Source File: ProtostuffRuntimeObjectSchemaTest.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> byte[] toByteArray(T message, Schema<T> schema)
{
    return ProtostuffIOUtil.toByteArray(message, schema, buf());
}
 
Example 19
Source File: ProtostuffNullArrayElementInObjectArrayTest.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> byte[] toByteArray(T message, Schema<T> schema)
{
    return ProtostuffIOUtil.toByteArray(message, schema, buf());
}
 
Example 20
Source File: ProtobufNullArrayElementTest.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> void roundTrip(T message, Schema<T> schema,
        Pipe.Schema<T> pipeSchema) throws IOException
{
    byte[] protobuf = ProtobufIOUtil.toByteArray(message, schema, buf());

    ByteArrayInputStream protobufStream = new ByteArrayInputStream(protobuf);

    byte[] protostuff = ProtostuffIOUtil.toByteArray(
            ProtobufIOUtil.newPipe(protobuf, 0, protobuf.length),
            pipeSchema, buf());

    byte[] protostuffFromStream = ProtostuffIOUtil.toByteArray(
            ProtobufIOUtil.newPipe(protobufStream), pipeSchema, buf());

    assertTrue(protostuff.length == protostuffFromStream.length);
    assertEquals(STRING.deser(protostuff),
            STRING.deser(protostuffFromStream));

    T parsedMessage = schema.newMessage();
    ProtostuffIOUtil.mergeFrom(protostuff, parsedMessage, schema);
    SerializableObjects.assertEquals(message, parsedMessage);

    ByteArrayInputStream protostuffStream = new ByteArrayInputStream(
            protostuff);

    byte[] protobufRoundTrip = ProtobufIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuff, 0, protostuff.length),
            pipeSchema, buf());

    byte[] protobufRoundTripFromStream = ProtobufIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuffStream), pipeSchema, buf());

    assertTrue(protobufRoundTrip.length == protobufRoundTripFromStream.length);

    String strProtobufRoundTrip = STRING.deser(protobufRoundTrip);

    assertEquals(strProtobufRoundTrip,
            STRING.deser(protobufRoundTripFromStream));

    assertTrue(protobufRoundTrip.length == protobuf.length);

    assertEquals(strProtobufRoundTrip, STRING.deser(protobuf));
}