下面列出了com.fasterxml.jackson.core.json.UTF8JsonGenerator#io.protostuff.ProtostuffIOUtil 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testEmployee() throws Exception
{
Schema<Employee> schema = RuntimeSchema.getSchema(Employee.class);
Employee p = filledEmployee();
byte[] data = ProtostuffIOUtil.toByteArray(p, schema,
LinkedBuffer.allocate(512));
Employee p2 = new Employee();
ProtostuffIOUtil.mergeFrom(data, p2, schema);
// System.err.println(p2);
assertEquals(p, p2);
}
@Override
@Nullable
public Object decodeResult(ByteBuf data, @Nullable Class<?> targetClass) throws EncodingException {
if (data.readableBytes() > 0 && targetClass != null) {
try {
if (GeneratedMessageV3.class.isAssignableFrom(targetClass)) {
Method method = parseFromMethodStore.get(targetClass);
if (method != null) {
return method.invoke(null, data.nioBuffer());
}
} else if (ktProtoBuf && KotlinSerializerSupport.isKotlinSerializable(targetClass)) {
byte[] bytes = new byte[data.readableBytes()];
data.readBytes(bytes);
return KotlinSerializerSupport.decodeFromProtobuf(bytes, targetClass);
} else {
Schema schema = RuntimeSchema.getSchema(targetClass);
Object object = schema.newMessage();
ProtostuffIOUtil.mergeFrom(new ByteBufInputStream(data), object, schema);
return object;
}
} catch (Exception e) {
throw new EncodingException(RsocketErrorCode.message("RST-700501", "bytebuf", targetClass.getName()), e);
}
}
return null;
}
@Test
public void testSimpleTask() throws Exception
{
Schema<Task> schema = RuntimeSchema.getSchema(Task.class);
Task p = filledTask();
byte[] data = ProtostuffIOUtil.toByteArray(p, schema,
LinkedBuffer.allocate(512));
Task p2 = new Task();
ProtostuffIOUtil.mergeFrom(data, p2, schema);
// System.err.println(p2);
assertEquals(p, p2);
}
public void testProtobuf() throws Exception
{
Schema<Entity> schema = RuntimeSchema.getSchema(Entity.class);
Entity p = filledEntity();
byte[] data = ProtobufIOUtil.toByteArray(p, schema,
LinkedBuffer.allocate(512));
Entity p2 = new Entity();
ProtostuffIOUtil.mergeFrom(data, p2, schema);
assertEquals(p, p2);
List<Entity> list = new ArrayList<Entity>();
list.add(p);
list.add(p2);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ProtobufIOUtil.writeListTo(out, list, schema, buf());
byte[] listData = out.toByteArray();
ByteArrayInputStream in = new ByteArrayInputStream(listData);
List<Entity> parsedList = ProtobufIOUtil.parseListFrom(in, schema);
assertEquals(list, parsedList);
}
public void testBar() throws Exception
{
Schema<Bar> schema = RuntimeSchema.getSchema(Bar.class);
for (Bar barCompare : new Bar[] { bar, negativeBar })
{
Bar dbar = new Bar();
int expectedSize = ComputedSizeOutput.getSize(barCompare, schema);
byte[] deferred = toByteArray(barCompare, schema);
assertTrue(deferred.length == expectedSize);
ProtostuffIOUtil.mergeFrom(deferred, dbar, schema);
SerializableObjects.assertEquals(barCompare, dbar);
// System.err.println(dbar.getSomeInt());
// System.err.println(dbar.getSomeLong());
// System.err.println(dbar.getSomeFloat());
// System.err.println(dbar.getSomeDouble());
// System.err.println(dbar.getSomeBytes());
// System.err.println(dbar.getSomeString());
// System.err.println(dbar.getSomeEnum());
// System.err.println(dbar.getSomeBoolean());
}
}
/**
* HasHasBar wraps an object without a schema. That object will have to be serialized via the default java
* serialization and it will be delimited.
* <p>
* HasBar wraps a message {@link Bar}.
*/
public void testJavaSerializable() throws Exception
{
Schema<HasHasBar> schema = RuntimeSchema.getSchema(HasHasBar.class);
HasHasBar hhbCompare = new HasHasBar("hhb", new HasBar(12345, "hb",
SerializableObjects.bar));
HasHasBar dhhb = new HasHasBar();
int expectedSize = ComputedSizeOutput.getSize(hhbCompare, schema);
byte[] deferred = toByteArray(hhbCompare, schema);
assertTrue(deferred.length == expectedSize);
ProtostuffIOUtil.mergeFrom(deferred, dhhb, schema);
assertEquals(hhbCompare, dhhb);
}
/**
* Description: <br>
*
* @author 王伟<br>
* @taskId <br>
* @param clazz
* @param data
* @param <T> T
* @return T
* @throws UtilException <br>
*/
@SuppressWarnings("unchecked")
public static <T> T unserial(final Class<T> clazz, final byte[] data) throws UtilException {
T result = null;
if (data != null && data.length > 0) {
try {
if (Map.class.isAssignableFrom(clazz)) {
result = (T) jdkUnserial(data);
}
else {
Schema<T> schema = RuntimeSchema.getSchema(clazz);
result = clazz.newInstance();
ProtostuffIOUtil.mergeFrom(data, result, schema);
}
}
catch (Exception e) {
throw new UtilException(e, ErrorCodeDef.UNSERIALIZE_ERROR, DataUtil.byte2HexStr(data));
}
}
return result;
}
/**
* Deserialized object
*
* @param data
* Binary arrays requiring deserialization
* @param clazz
* Deserialized object class
* @param <T>
* Object types after deserialization
* @return Deserialized object set
*/
public static <T> T deserialize(byte[] data, Class<T> clazz) {
notNullOf(clazz, "objectClass");
if (isNull(data))
return null;
try {
// Simple type conversion
T bean = simpleConversion(data, clazz);
if (bean == null) {
if (!warpperSet.contains(clazz) && !clazz.isArray()) {
bean = objenesis.newInstance(clazz); // java原生实例化必须调用constructor故使用objenesis
ProtostuffIOUtil.mergeFrom(data, bean, getSchema(clazz));
return bean;
} else {
SerializableWrapper<T> wrapper = new SerializableWrapper<>();
ProtostuffIOUtil.mergeFrom(data, wrapper, warpperSchema);
return wrapper.getData();
}
}
return bean;
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
public static <T> T getModel(byte[] value, Class<T> clazz) {
if (value == null) {
return null;
}
try {
ModelWrapper model = new ModelWrapper();
ProtostuffIOUtil.mergeFrom(value, model, MODEL_WRAPPER_SCHEMA);
if (clazz != null && model.getT() != null) {
return clazz.cast(model.getT());
}
return (T) model.getT();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public void testPojoWithArrayAndSet() throws Exception
{
PojoWithArrayAndSet pojoCompare = filledPojoWithArrayAndSet();
Schema<PojoWithArrayAndSet> schema = RuntimeSchema
.getSchema(PojoWithArrayAndSet.class);
PojoWithArrayAndSet dpojo = new PojoWithArrayAndSet();
int expectedSize = ComputedSizeOutput
.getSize(pojoCompare, schema, true);
byte[] deferred = toByteArray(pojoCompare, schema);
assertTrue(deferred.length == expectedSize);
ProtostuffIOUtil.mergeFrom(deferred, dpojo, schema);
assertEquals(pojoCompare, dpojo);
// System.err.println(dpojo.getSomeEnumAsSet());
// System.err.println(dpojo.getSomeFloatAsSet());
}
@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();
}
}
}
public static <T> T getModel(String area, byte[] key, Class<T> clazz) {
if (!baseCheckArea(area)) {
return null;
}
if (key == null) {
return null;
}
try {
DB db = AREAS.get(area);
byte[] bytes = db.get(key);
if (bytes == null) {
return null;
}
RuntimeSchema schema = SCHEMA_MAP.get(ModelWrapper.class);
ModelWrapper model = new ModelWrapper();
ProtostuffIOUtil.mergeFrom(bytes, model, schema);
if (clazz != null && model.getT() != null) {
return clazz.cast(model.getT());
}
return (T) model.getT();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
static void compareFoo()
{
io.protostuff.Foo foo1 = io.protostuff.SerializableObjects.foo;
Schema<io.protostuff.Foo> schema1 = io.protostuff.Foo
.getSchema();
Foo foo2 = SerializableObjects.foo;
Schema<Foo> schema2 = RuntimeSchema.getSchema(Foo.class);
Schema<io.protostuff.Foo> schema3 = RuntimeSchema
.getSchema(io.protostuff.Foo.class);
byte[] byte1 = ProtostuffIOUtil.toByteArray(foo1, schema1, buf());
byte[] byte2 = ProtostuffIOUtil.toByteArray(foo2, schema2, buf());
byte[] byte3 = ProtostuffIOUtil.toByteArray(foo1, schema3, buf());
assertArrayEquals(byte1, byte2);
assertArrayEquals(byte1, byte3);
}
public Seckill getSeckill(long seckillId) {
//redis操作逻辑
try {
Jedis jedis = jedisPool.getResource();
try {
String key = RedisKeyPrefix.SECKILL_GOODS + seckillId;
byte[] bytes = jedis.get(key.getBytes());
//缓存中获取到bytes
if (bytes != null) {
//空对象
Seckill seckill = schema.newMessage();
ProtostuffIOUtil.mergeFrom(bytes, seckill, schema);
//seckill 被反序列化
return seckill;
}
} finally {
jedis.close();
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return null;
}
public String putSeckill(Seckill seckill) {
// set Object(Seckill) -> 序列化 -> byte[]
try {
Jedis jedis = jedisPool.getResource();
try {
String key = RedisKeyPrefix.SECKILL_GOODS + seckill.getSeckillId();
byte[] bytes = ProtostuffIOUtil.toByteArray(seckill, schema,
LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
String result = jedis.set(key.getBytes(), bytes);
return result;
} finally {
jedis.close();
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return null;
}
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完毕!");
}
/**
* 预热秒杀数据到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缓存数据初始化完毕!");
}
private static Optional<ClusterIdentity> getClusterIdentityFromStore(ConfigurationStore store, LegacyKVStoreProvider provider) {
final ConfigurationEntry entry = store.get(SupportService.CLUSTER_ID);
if (entry == null) {
Optional<ClusterIdentity> upgradedClusterIdentity = upgradeToNewSupportStore(provider);
return upgradedClusterIdentity;
}
try {
ClusterIdentity identity = ClusterIdentity.getSchema().newMessage();
ProtostuffIOUtil.mergeFrom(entry.getValue().toByteArray(), identity, ClusterIdentity.getSchema());
return Optional.ofNullable(identity);
} catch (Exception e) {
logger.info("failed to get cluster identity", e);
return Optional.empty();
}
}
@Override
public PurchaseInfoDto deserialize(String topic, byte[] data) {
if(data==null) {
return null;
}
Schema<PurchaseInfoDto> schema = RuntimeSchema.getSchema(PurchaseInfoDto.class);
PurchaseInfoDto purchaseInfoDto=new PurchaseInfoDto();
ProtostuffIOUtil.mergeFrom(data, purchaseInfoDto, schema);
return purchaseInfoDto;
}
public void testImmutablePojo() throws Exception
{
Schema<ImmutablePojo> schema = RuntimeSchema
.getSchema(ImmutablePojo.class);
ImmutablePojo p = new ImmutablePojo(3, "ip");
byte[] data = ProtostuffIOUtil.toByteArray(p, schema, buf());
ImmutablePojo p2 = schema.newMessage();
ProtostuffIOUtil.mergeFrom(data, 0, data.length, p2, schema);
assertEquals(p, p2);
List<ImmutablePojo> list = new ArrayList<ImmutablePojo>();
list.add(p);
list.add(p2);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ProtostuffIOUtil.writeListTo(out, list, schema, buf());
byte[] listData = out.toByteArray();
ByteArrayInputStream in = new ByteArrayInputStream(listData);
List<ImmutablePojo> parsedList = ProtostuffIOUtil.parseListFrom(in,
schema);
assertEquals(list, parsedList);
}
public void testProtostuff() throws Exception
{
Schema<Payment> schema = RuntimeSchema.getSchema(Payment.class);
Payment p = filledPayment();
byte[] data = ProtostuffIOUtil.toByteArray(p, schema, buf());
Payment p2 = new Payment();
ProtostuffIOUtil.mergeFrom(data, 0, data.length, p2, schema);
/*
* System.err.println(p2.getId()); System.err.println(p2.getBd()); System.err.println(p2.getBi());
* System.err.println(p2.getBdList()); System.err.println(p2.getBiList());
*/
assertEquals(p, p2);
List<Payment> list = new ArrayList<Payment>();
list.add(p);
list.add(p2);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ProtostuffIOUtil.writeListTo(out, list, schema, buf());
byte[] listData = out.toByteArray();
ByteArrayInputStream in = new ByteArrayInputStream(listData);
List<Payment> parsedList = ProtostuffIOUtil.parseListFrom(in, schema);
assertEquals(list, parsedList);
}
@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);
}
/**
* 这里只解析使用serrializeforcommon方法所序列化的对象,不支持这个工具类中的其他方法序列化的字节流
* @param bs
* @return
* @throws InstantiationException
* @throws IllegalAccessException
*/
public static Object unSerializeForCommon(byte[] bs) throws InstantiationException, IllegalAccessException
{
Schema<ObjectWrapper> schema = RuntimeSchema.getSchema(ObjectWrapper.class);
ObjectWrapper objectWrapper = new ObjectWrapper();
ProtostuffIOUtil.mergeFrom(bs, objectWrapper, schema);
return objectWrapper.getWrappedStuff();
}
@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();
}
}
@Test
public void testIEmployee() throws Exception
{
// Because we mapped IEmployee to Employee, this is ok.
Schema<AbstractEmployee> schema = RuntimeSchema
.getSchema(AbstractEmployee.class);
Collection<String> departments = new ArrayList<String>();
departments.add("Engineering");
departments.add("IT");
Collection<ITask> tasks = new ArrayList<ITask>();
tasks.add(filledTask());
AbstractEmployee p = new Employee();
p.setId(1);
p.setDepartments(departments);
p.setTasks(tasks);
byte[] data = ProtostuffIOUtil.toByteArray(p, schema,
LinkedBuffer.allocate(512));
AbstractEmployee p2 = new Employee();
ProtostuffIOUtil.mergeFrom(data, p2, schema);
// System.err.println(p2);
assertEquals(p, p2);
}
@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));
}
/**
* 序列化方法,把指定对象序列化成字节数组
*
* @param obj
* @param <T>
* @return
*/
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;
}
@Test
public void testSerializeDeserializeNumericEnum() throws Exception
{
RuntimeSchema<A> schema = RuntimeSchema.createFrom(A.class);
A source = new A(TaggedEnum.TEN);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
LinkedBuffer buffer = LinkedBuffer.allocate();
ProtostuffIOUtil.writeTo(outputStream, source, schema, buffer);
byte[] bytes = outputStream.toByteArray();
A newMessage = schema.newMessage();
ProtostuffIOUtil.mergeFrom(bytes, newMessage, schema);
Assert.assertEquals(source, newMessage);
}
public void testPojoWithEnumMap() throws Exception
{
Schema<PojoWithEnumMap> schema = RuntimeSchema
.getSchema(PojoWithEnumMap.class);
PojoWithEnumMap p = new PojoWithEnumMap().fill();
byte[] data = ProtostuffIOUtil.toByteArray(p, schema, buf());
PojoWithEnumMap p2 = new PojoWithEnumMap();
ProtostuffIOUtil.mergeFrom(data, 0, data.length, p2, schema);
assertEquals(p, p2);
List<PojoWithEnumMap> list = new ArrayList<PojoWithEnumMap>();
list.add(p);
list.add(p2);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ProtostuffIOUtil.writeListTo(out, list, schema, buf());
byte[] listData = out.toByteArray();
ByteArrayInputStream in = new ByteArrayInputStream(listData);
List<PojoWithEnumMap> parsedList = ProtostuffIOUtil.parseListFrom(in,
schema);
assertEquals(list, parsedList);
}
/**
* Serializes an SignedRelayedMessage into a byte string using Java
* serialization
*
* @param object
* @return
*/
public static byte[] serializeSignedRelayedMessage(final SignedRelayedMessage signedRelayedMessage) {
// 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(signedRelayedMessage, SIGNED_RELAYED_MESSAGE_SCHEMA, buffer);
} finally {
// Release buffer
buffer.clear();
}
}