io.protostuff.LinkedBuffer Java Examples
The following examples show how to use
io.protostuff.LinkedBuffer.
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: 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 #2
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 #3
Source File: CollectionTest.java From protostuff with Apache License 2.0 | 6 votes |
@Test public void testITask() throws Exception { // Because we mapped ITask to Task, this is ok. Schema<ITask> schema = RuntimeSchema.getSchema(ITask.class); ITask p = filledTask(); byte[] data = ProtostuffIOUtil.toByteArray(p, schema, LinkedBuffer.allocate(512)); ITask p2 = new Task(); ProtostuffIOUtil.mergeFrom(data, p2, schema); // System.err.println(p2); assertEquals(p, p2); }
Example #4
Source File: ProtoStuffSerialize.java From Voovan with Apache License 2.0 | 6 votes |
@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 #5
Source File: ObjectSchemaTest.java From protostuff with Apache License 2.0 | 6 votes |
public void testGraph() { System.err.println(RuntimeEnv.COLLECTION_SCHEMA_ON_REPEATED_FIELDS); Bean bean = fill(new Bean()); verify(bean); Schema<Bean> schema = RuntimeSchema.getSchema(Bean.class); // print(schema); byte[] bytes = GraphIOUtil.toByteArray(bean, schema, LinkedBuffer.allocate(256)); Bean deBean = new Bean(); GraphIOUtil.mergeFrom(bytes, deBean, schema); verify(deBean); }
Example #6
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 #7
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 #8
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 #9
Source File: EnumSetAndMapTest.java From protostuff with Apache License 2.0 | 6 votes |
static void doBean(IdStrategy strategy) { Bean bean = fill(new Bean()); verify(bean); Schema<Bean> schema = RuntimeSchema.getSchema(Bean.class, strategy); // print(schema); byte[] bytes = ProtostuffIOUtil.toByteArray(bean, schema, LinkedBuffer.allocate(256)); Bean deBean = new Bean(); ProtostuffIOUtil.mergeFrom(bytes, deBean, schema); verify(deBean); }
Example #10
Source File: EnumSetAndMapTest.java From protostuff with Apache License 2.0 | 6 votes |
static void doBeanCyclic(IdStrategy strategy) { boolean csorf = 0 != (strategy.flags & (IdStrategy.COLLECTION_SCHEMA_ON_REPEATED_FIELDS)); Bean bean = fillCyclic(new Bean()); verifyCyclic(bean, csorf); Schema<Bean> schema = RuntimeSchema.getSchema(Bean.class, strategy); // print(schema); byte[] bytes = GraphIOUtil.toByteArray(bean, schema, LinkedBuffer.allocate(256)); Bean deBean = new Bean(); GraphIOUtil.mergeFrom(bytes, deBean, schema); verifyCyclic(deBean, csorf); }
Example #11
Source File: ProtostuffRowSerializationSchema.java From alchemy with Apache License 2.0 | 6 votes |
@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 #12
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 #13
Source File: ProtoStuffSerializer.java From bitchat with Apache License 2.0 | 6 votes |
@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 #14
Source File: DateTest.java From protostuff with Apache License 2.0 | 6 votes |
public void testProtostuff() throws Exception { Schema<Entity> schema = RuntimeSchema.getSchema(Entity.class); Entity p = filledEntity(); byte[] data = ProtostuffIOUtil.toByteArray(p, schema, LinkedBuffer.allocate(512)); Entity p2 = new Entity(); ProtostuffIOUtil.mergeFrom(data, 0, data.length, p2, schema); assertEquals(p, p2); List<Entity> list = new ArrayList<Entity>(); 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<Entity> parsedList = ProtostuffIOUtil.parseListFrom(in, schema); assertEquals(list, parsedList); }
Example #15
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 #16
Source File: ProtoStuffSerializer.java From Jupiter with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") @Override public <T> byte[] writeObject(T obj) { Schema<T> schema = RuntimeSchema.getSchema((Class<T>) obj.getClass()); LinkedBuffer buf = LinkedBuffers.getLinkedBuffer(); Output output = Outputs.getOutput(buf); try { schema.writeTo(output, obj); return Outputs.toByteArray(output); } catch (IOException e) { ThrowUtil.throwException(e); } finally { LinkedBuffers.resetBuf(buf); // for reuse } return null; // never get here }
Example #17
Source File: PBTest.java From blog with BSD 2-Clause "Simplified" License | 6 votes |
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 #18
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 #19
Source File: CatalogServiceImpl.java From dremio-oss with Apache License 2.0 | 6 votes |
private void communicateChange(SourceConfig config, RpcType rpcType) { final Set<NodeEndpoint> endpoints = new HashSet<>(); endpoints.add(context.get().getEndpoint()); List<RpcFuture<Ack>> futures = new ArrayList<>(); SourceWrapper wrapper = SourceWrapper.newBuilder().setBytes(ByteString.copyFrom(ProtobufIOUtil.toByteArray(config, SourceConfig.getSchema(), LinkedBuffer.allocate()))).build(); for(NodeEndpoint e : Iterables.concat(this.context.get().getCoordinators(), this.context.get().getExecutors())) { if(!endpoints.add(e)) { continue; } SendSource send = new SendSource(wrapper, rpcType); tunnelFactory.getCommandRunner(e.getAddress(), e.getFabricPort()).runCommand(send); logger.trace("Sending [{}] to {}:{}", config.getName(), e.getAddress(), e.getUserPort()); futures.add(send.getFuture()); } try { Futures.successfulAsList(futures).get(CHANGE_COMMUNICATION_WAIT, TimeUnit.MILLISECONDS); } catch (InterruptedException | ExecutionException | TimeoutException e1) { logger.warn("Failure while communicating source change [{}].", config.getName(), e1); } }
Example #20
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 #21
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 #22
Source File: ProtoStuffSerializeUtil.java From jim-framework with Apache License 2.0 | 6 votes |
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 #23
Source File: SerializerTest.java From jim-framework with Apache License 2.0 | 6 votes |
public static <T> byte[] serialize(T obj) { if (obj == null) { throw new RuntimeException("JdkSerialize.serialize: obj is null"); } @SuppressWarnings("unchecked") Schema<T> schema = (Schema<T>) RuntimeSchema.getSchema(obj.getClass()); LinkedBuffer buffer = LinkedBuffer.allocate(); byte[] protostuff = null; try { protostuff = ProtostuffIOUtil.toByteArray(obj, schema, buffer); } catch (Exception e) { throw e; } finally { buffer.clear(); } return protostuff; }
Example #24
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 #25
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 #26
Source File: RuntimeGraphUnknownFieldTest.java From protostuff with Apache License 2.0 | 5 votes |
private static <T> byte[] serializeGraph(T object) { @SuppressWarnings("unchecked") Class<T> clazz = (Class<T>) object.getClass(); Schema<T> schema = RuntimeSchema.getSchema(clazz); return GraphIOUtil.toByteArray(object, schema, LinkedBuffer.allocate()); }
Example #27
Source File: CollectionTest.java From protostuff with Apache License 2.0 | 5 votes |
@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); }
Example #28
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 #29
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 #30
Source File: ProtostuffSerializer.java From dremio-oss with Apache License 2.0 | 5 votes |
@Override public byte[] convert(T t) { final LinkedBuffer buffer = tl.get(); try { return ProtostuffIOUtil.toByteArray(t, schema, buffer); } finally { buffer.clear(); } }