io.protostuff.ProtostuffIOUtil Java Examples
The following examples show how to use
io.protostuff.ProtostuffIOUtil.
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: LevelDBManager.java From nuls with MIT License | 6 votes |
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; } }
Example #2
Source File: ObjectEncodingHandlerProtobufImpl.java From alibaba-rsocket-broker with Apache License 2.0 | 6 votes |
@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; }
Example #3
Source File: CollectionTest.java From protostuff with Apache License 2.0 | 6 votes |
@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); }
Example #4
Source File: RedisDAO.java From jseckill with Apache License 2.0 | 6 votes |
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; }
Example #5
Source File: CompatTest.java From protostuff with Apache License 2.0 | 6 votes |
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); }
Example #6
Source File: CollectionTest.java From protostuff with Apache License 2.0 | 6 votes |
@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); }
Example #7
Source File: DateTest.java From protostuff with Apache License 2.0 | 6 votes |
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); }
Example #8
Source File: RedisDAO.java From jseckill with Apache License 2.0 | 6 votes |
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; }
Example #9
Source File: RedisDAO.java From jseckill with Apache License 2.0 | 6 votes |
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 #10
Source File: InitTask.java From jseckill with Apache License 2.0 | 6 votes |
/** * 预热秒杀数据到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 #11
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 #12
Source File: SerDeserTest.java From protostuff with Apache License 2.0 | 6 votes |
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()); } }
Example #13
Source File: SerDeserTest.java From protostuff with Apache License 2.0 | 6 votes |
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()); }
Example #14
Source File: BasicSupportService.java From dremio-oss with Apache License 2.0 | 6 votes |
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(); } }
Example #15
Source File: SerDeserTest.java From protostuff with Apache License 2.0 | 6 votes |
/** * 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); }
Example #16
Source File: SerializationUtil.java From framework with Apache License 2.0 | 6 votes |
/** * 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; }
Example #17
Source File: ContractDBUtil.java From nuls-v2 with MIT License | 6 votes |
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; } }
Example #18
Source File: ProtostuffUtils.java From super-cloudops with Apache License 2.0 | 6 votes |
/** * 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); } }
Example #19
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 #20
Source File: ProtostuffSerializer.java From tx-lcn with Apache License 2.0 | 5 votes |
@Override public <T> T deSerialize(byte[] param, Class<T> cls) throws SerializerException { T object; try (ByteArrayInputStream inputStream = new ByteArrayInputStream(param)) { object = OBJENESIS.newInstance(cls); Schema schema = getSchema(cls); ProtostuffIOUtil.mergeFrom(inputStream, object, schema); return object; } catch (Exception e) { throw new SerializerException(e.getMessage(), e); } }
Example #21
Source File: ProtoStuffSerialize.java From Voovan with Apache License 2.0 | 5 votes |
@Override public <T> T unserialize(byte[] bytes) { try { Integer hashcode = null; if(bytes.length >= 4) { hashcode = TByte.getInt(bytes); } Class innerClazz = hashcode==null ? null : TSerialize.getClassByHash(hashcode); //如果没有明确的类指示,则直接返回字节数组 if(innerClazz != null) { byte[] valueBytes = Arrays.copyOfRange(bytes, 4, bytes.length); Object obj = TByte.toObject(valueBytes, innerClazz); if (obj == null) { Schema schema = getSchema(innerClazz); obj = TReflect.newInstance(innerClazz); ProtostuffIOUtil.mergeFrom(valueBytes, 0, valueBytes.length, obj, schema); } return (T) obj; } else { return (T) bytes; } } catch (Exception e) { Logger.error(e); } return null; }
Example #22
Source File: EnumSetAndMapTest.java From protostuff with Apache License 2.0 | 5 votes |
public void testPojoWithEnumSet() throws Exception { Schema<PojoWithEnumSet> schema = RuntimeSchema .getSchema(PojoWithEnumSet.class); PojoWithEnumSet p = new PojoWithEnumSet().fill(); byte[] data = ProtostuffIOUtil.toByteArray(p, schema, buf()); PojoWithEnumSet p2 = new PojoWithEnumSet(); ProtostuffIOUtil.mergeFrom(data, 0, data.length, p2, schema); assertEquals(p, p2); List<PojoWithEnumSet> list = new ArrayList<PojoWithEnumSet>(); 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<PojoWithEnumSet> parsedList = ProtostuffIOUtil.parseListFrom(in, schema); assertEquals(list, parsedList); }
Example #23
Source File: PojoWithArrayAndSet.java From protostuff with Apache License 2.0 | 5 votes |
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 #24
Source File: PurchaseInfoDeSerializer.java From ChengFeng1.5 with MIT License | 5 votes |
@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; }
Example #25
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 #26
Source File: ProtoStuffSerializer.java From Thunder with Apache License 2.0 | 5 votes |
public static <T> T deserialize(byte[] bytes, Class<T> clazz) { Schema<T> schema = getSchema(clazz); T object = OBJENESIS.newInstance(clazz); ProtostuffIOUtil.mergeFrom(bytes, object, schema); return object; }
Example #27
Source File: ConnectionReaderImpl.java From dremio-oss with Apache License 2.0 | 5 votes |
/** * Returns the given source config as a string, without secret fields. Useful in error messages and debug logs. * * @param sourceConfig source config * @return source config as string, without secret fields */ @Override public String toStringWithoutSecrets(SourceConfig sourceConfig) { try { final byte[] bytes = ProtostuffIOUtil.toByteArray(sourceConfig, SourceConfig.getSchema(), LinkedBuffer.allocate()); final SourceConfig clone = new SourceConfig(); ProtostuffIOUtil.mergeFrom(bytes, clone, SourceConfig.getSchema()); final ConnectionConf<?, ?> conf = getConnectionConf(clone); conf.clearSecrets(); clone.setConfig(null); final StringBuilder sb = new StringBuilder(); sb.append("[source: ") .append(clone.toString()) .append(", connection: "); try { sb.append(mapper.writeValueAsString(conf)); } catch (JsonProcessingException ignored) { sb.append("<serialization_error>"); } sb.append("]"); return sb.toString(); } catch (Exception e) { return "failed to serialize: " + e.getMessage(); } }
Example #28
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 #29
Source File: ProtoSerializers.java From dremio-oss with Apache License 2.0 | 5 votes |
@Override public X deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { X msg = schema.newMessage(); ProtostuffIOUtil.mergeFrom(p.getBinaryValue(), msg, schema); // p.nextToken(); // JsonIOUtil.mergeFrom(p, msg, schema, false); return msg; }
Example #30
Source File: StoragePluginId.java From dremio-oss with Apache License 2.0 | 5 votes |
@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; }