@Override public void run() { try { while (true) { MetaData metaData = sendQueue.take(); Output output = getOutput(metaData.getDestRank()); operand.send(output, metaData); } } catch (Exception e) { try { exception(e); } catch (Mp4jException e1) { LOG.error("Mp4jException", e); } } }
@Override public void write(final Kryo kryo, final Output output, final RegisteredService service) { kryo.writeObject(output, service.getServiceId()); kryo.writeObject(output, StringUtils.defaultIfEmpty(service.getName(), "")); kryo.writeObject(output, StringUtils.defaultIfEmpty(service.getDescription(), "")); kryo.writeObject(output, service.getId()); kryo.writeObject(output, service.getEvaluationOrder()); kryo.writeObject(output, ObjectUtils.defaultIfNull(service.getLogo(), getEmptyUrl())); kryo.writeObject(output, service.getLogoutType()); kryo.writeObject(output, ObjectUtils.defaultIfNull(service.getLogoutUrl(), getEmptyUrl())); kryo.writeObject(output, ImmutableSet.copyOf(service.getRequiredHandlers())); kryo.writeObject(output, StringUtils.defaultIfEmpty(service.getTheme(), "")); writeObjectByReflection(kryo, output, ObjectUtils.defaultIfNull(service.getPublicKey(), new RegisteredServicePublicKeyImpl())); writeObjectByReflection(kryo, output, ObjectUtils.defaultIfNull(service.getProxyPolicy(), new RefuseRegisteredServiceProxyPolicy())); writeObjectByReflection(kryo, output, ObjectUtils.defaultIfNull(service.getAttributeReleasePolicy(), new ReturnAllowedAttributeReleasePolicy())); writeObjectByReflection(kryo, output, ObjectUtils.defaultIfNull(service.getUsernameAttributeProvider(), new DefaultRegisteredServiceUsernameProvider())); writeObjectByReflection(kryo, output, ObjectUtils.defaultIfNull(service.getAccessStrategy(), new DefaultRegisteredServiceAccessStrategy())); }
@Override public ByteBuf encode(Object in) throws IOException { Kryo kryo = null; ByteBuf out = ByteBufAllocator.DEFAULT.buffer(); try { ByteBufOutputStream baos = new ByteBufOutputStream(out); Output output = new Output(baos); kryo = kryoPool.get(); kryo.writeClassAndObject(output, in); output.close(); return baos.buffer(); } catch (Exception e) { out.release(); if (e instanceof RuntimeException) { throw (RuntimeException) e; } throw new RedissonKryoCodecException(e); } finally { if (kryo != null) { kryoPool.yield(kryo); } } }
public void write(Kryo kryo, Output output, ArrayMetaData<long[]> object) { try { long[] arrData = arrayMetaData.getArrData(); arrayMetaData.send(output); int arrSegNum = arrayMetaData.getSegNum(); for (int i = 0; i < arrSegNum; i++) { int from = arrayMetaData.getFrom(i); int to = arrayMetaData.getTo(i); for (int j = from; j < to; j++) { output.writeLong(arrData[j]); } } } catch (IOException e) { LOG.error("double array write exception", e); System.exit(1); } }
@Override public byte[] serialize(Object obj) throws SerializerException { byte[] bytes; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { //获取kryo对象 Kryo kryo = new Kryo(); Output output = new Output(outputStream); kryo.writeObject(output, obj); bytes = output.toBytes(); output.flush(); } catch (Exception ex) { throw new SerializerException("kryo serialize error" + ex.getMessage()); } finally { try { outputStream.flush(); outputStream.close(); } catch (IOException e) { } } return bytes; }
public void write(Kryo kryo, Output output, ArrayMetaData<short[]> object) { try { short[] arrData = arrayMetaData.getArrData(); arrayMetaData.send(output); int arrSegNum = arrayMetaData.getSegNum(); for (int i = 0; i < arrSegNum; i++) { int from = arrayMetaData.getFrom(i); int to = arrayMetaData.getTo(i); for (int j = from; j < to; j++) { output.writeShort(arrData[j]); } } } catch (IOException e) { LOG.error("double array write exception", e); System.exit(1); } }
public void write(Kryo kryo, Output output, ArrayMetaData<byte[]> object) { try { byte[] arrData = arrayMetaData.getArrData(); arrayMetaData.send(output); int arrSegNum = arrayMetaData.getSegNum(); for (int i = 0; i < arrSegNum; i++) { int from = arrayMetaData.getFrom(i); int to = arrayMetaData.getTo(i); for (int j = from; j < to; j++) { output.writeByte(arrData[j]); } } } catch (IOException e) { LOG.error("double array write exception", e); System.exit(1); } }
/** * 序列化 * * @param obj 需要序更列化的对象 * @return 序列化后的byte 数组 * @throws TccException */ @Override public byte[] serialize(Object obj) throws TccException { byte[] bytes; try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { //获取kryo对象 Kryo kryo = new Kryo(); Output output = new Output(outputStream); kryo.writeObject(output, obj); bytes = output.toBytes(); output.flush(); } catch (Exception ex) { throw new TccException("kryo serialize error" + ex.getMessage()); } return bytes; }
@Override public byte[] apply(Object value) { try { Kryo kryo = kryoThreadLocal.get(); ByteArrayOutputStream bos = new ByteArrayOutputStream(256); Output output = new Output(bos); output.writeInt(IDENTITY_NUMBER); kryo.writeClassAndObject(output, value); output.close(); return bos.toByteArray(); } catch (Exception e) { StringBuilder sb = new StringBuilder("Kryo Encode error. "); sb.append("msg=").append(e.getMessage()); throw new CacheEncodeException(sb.toString(), e); } }
protected void encode(ChannelHandlerContext channelHandlerContext, Object o, ByteBuf byteBuf) throws Exception { if(clazz.isInstance(o)){ Kryo kryo = null; try{ kryo = pool.borrow(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Output output = new Output(baos); kryo.writeObject(output, o); output.flush(); output.close(); byte[] data = baos.toByteArray(); byteBuf.writeInt(data.length); byteBuf.writeBytes(data); baos.close(); }catch(Exception e){ LOG.warn("MessageEncoder happen exception.", e); }finally{ if(kryo != null){ pool.release(kryo); } } } }
public void write (Kryo kryo, Output output, LongMap map) { int length = map.size; output.writeVarInt(length, true); output.writeBoolean(false); // whether type is written (in case future version of LongMap supports type awareness) Serializer valueSerializer = null; if (valueGenericType != null) { if (valueSerializer == null) valueSerializer = kryo.getSerializer(valueGenericType); valueGenericType = null; } for (Iterator iter = map.iterator(); iter.hasNext();) { LongMap.Entry entry = (LongMap.Entry)iter.next(); output.writeLong(entry.key); if (valueSerializer != null) { kryo.writeObjectOrNull(output, entry.value, valueSerializer); } else kryo.writeClassAndObject(output, entry.value); } }
private <E> void writeTable(String key, RakTable<E> paperTable, File originalFile, File backupFile) { try { FileOutputStream file = new FileOutputStream(originalFile); final Output kryoOutput = new Output(file); getKryo().writeObject(kryoOutput, paperTable); kryoOutput.flush(); file.flush(); sync(file); kryoOutput.close(); backupFile.delete(); } catch (IOException e) { if (originalFile.exists()) { if (!originalFile.delete()) { throw new RuntimeException("cant deleted"); } } throw new RuntimeException("cant save to table " + key, e); } }
public void write(Kryo kryo, Output output, ArrayMetaData<String[]> object) { try { String[] arrData = arrayMetaData.getArrData(); arrayMetaData.send(output); int arrSegNum = arrayMetaData.getSegNum(); for (int i = 0; i < arrSegNum; i++) { int from = arrayMetaData.getFrom(i); int to = arrayMetaData.getTo(i); for (int j = from; j < to; j++) { output.writeString(arrData[j]); } } } catch (IOException e) { LOG.error("double array write exception", e); System.exit(1); } }
public void write(Kryo kryo, Output output, MapMetaData<Float> object) { try { List<Map<String, Float>> mapDataList = mapMetaData.getMapDataList(); mapMetaData.send(output); int mapSegNum = mapMetaData.getSegNum(); for (int i = 0; i < mapSegNum; i++) { Map<String, Float> mapData = mapDataList.get(i); for (Map.Entry<String, Float> entry : mapData.entrySet()) { output.writeString(entry.getKey()); output.writeFloat(entry.getValue()); } if (mapMetaData.getCollective() == Collective.GATHER || mapMetaData.getCollective() == Collective.SCATTER || mapMetaData.getCollective() == Collective.REDUCE_SCATTER) { mapData.clear(); } } } catch (IOException e) { LOG.error("double array write exception", e); System.exit(1); } }
/** * 序列化 * * @param obj 需要序更列化的对象 * @return 序列化后的byte 数组 * @throws MythException 异常 */ @Override public byte[] serialize(Object obj) throws MythException { byte[] bytes; try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { //获取kryo对象 Kryo kryo = new Kryo(); Output output = new Output(outputStream); kryo.writeObject(output, obj); bytes = output.toBytes(); output.flush(); } catch (Exception ex) { throw new MythException("kryo serialize error" + ex.getMessage()); } return bytes; }
public void write(Kryo kryo, Output output, ArrayMetaData<T[]> object) { try { T []arrData = arrayMetaData.getArrData(); arrayMetaData.send(output); int arrSegNum = arrayMetaData.getSegNum(); for (int i = 0; i < arrSegNum; i++) { int from = arrayMetaData.getFrom(i); int to = arrayMetaData.getTo(i); for (int j = from; j < to; j++) { serializer.write(kryo, output, arrData[j]); } } } catch (IOException e) { LOG.error("double array write exception", e); System.exit(1); } }
public byte[] serialize(Object object) { Kryo kryo = new Kryo(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Output output = new Output(baos); kryo.writeObject(output, object); output.flush(); output.close(); byte[] bytes = baos.toByteArray(); try { baos.flush(); baos.close(); } catch (IOException e) { e.printStackTrace(); } return bytes; }
private <T extends Serializable> byte[] serializationObject(T obj) { Kryo kryo = new Kryo(); kryo.setReferences(false); kryo.register(obj.getClass(), new JavaSerializer()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Output output = new Output(baos); kryo.writeClassAndObject(output, obj); output.flush(); output.close(); byte[] b = baos.toByteArray(); try { baos.flush(); baos.close(); } catch (IOException e) { e.printStackTrace(); } return b; }
public void write(Kryo kryo, Output output, ArrayMetaData<int[]> object) { try { int[] arrData = arrayMetaData.getArrData(); arrayMetaData.send(output); int arrSegNum = arrayMetaData.getSegNum(); for (int i = 0; i < arrSegNum; i++) { int from = arrayMetaData.getFrom(i); int to = arrayMetaData.getTo(i); for (int j = from; j < to; j++) { output.writeInt(arrData[j]); } } } catch (IOException e) { LOG.error("double array write exception", e); System.exit(1); } }
public void write (Kryo kryo, Output output, ObjectIntMap map) { int length = map.size; output.writeVarInt(length, true); output.writeBoolean(false); // whether type is written (in case future version of ObjectIntMap supports type awareness) Serializer keySerializer = null; if (keyGenericType != null) { if (keySerializer == null) keySerializer = kryo.getSerializer(keyGenericType); keyGenericType = null; } for (Iterator iter = map.iterator(); iter.hasNext();) { ObjectIntMap.Entry entry = (ObjectIntMap.Entry)iter.next(); if (keySerializer != null) { kryo.writeObject(output, entry.key, keySerializer); } else kryo.writeClassAndObject(output, entry.key); output.writeInt(entry.value); } }
@Override public void write(Kryo kryo, Output output, SortedIntList list) { output.writeVarInt(list.size(), true); Serializer serializer = null; if (genericType != null) { if (serializer == null) serializer = kryo.getSerializer(genericType); genericType = null; } for (Iterator<SortedIntList.Node> iter = list.iterator(); iter.hasNext();){ SortedIntList.Node node = iter.next(); output.writeInt(node.index); if (serializer != null) { kryo.writeObjectOrNull(output, node.value, serializer); } else { kryo.writeClassAndObject(output, node.value); } } }
public void write(Kryo kryo, Output output, ArrayMetaData<double[]> object) { try { double[] arrData = arrayMetaData.getArrData(); arrayMetaData.send(output); int arrSegNum = arrayMetaData.getSegNum(); for (int i = 0; i < arrSegNum; i++) { int from = arrayMetaData.getFrom(i); int to = arrayMetaData.getTo(i); for (int j = from; j < to; j++) { output.writeDouble(arrData[j]); } } } catch (IOException e) { LOG.error("double array write exception", e); System.exit(1); } }
@Test public void treeNode() { final Result<DecisionTreeRuleSet> result = (new CommisionRuleSetSupplier()).get(); EhSupport.ensure(result.isSuccess(), "Could not create decision tree"); final DecisionTreeRuleSet ruleSet = result.getData(); final TreeNode node = DecisionTreeFactory.constructDecisionTree(ruleSet, DecisionTreeType.SINGLE); final Kryo kryo = new Kryo(); // no default no-arg constructors kryo.setInstantiatorStrategy(new StdInstantiatorStrategy()); final InstantiatorStrategy defaultInstantiatorStrategy = new Kryo.DefaultInstantiatorStrategy(); kryo.getRegistration(ArrayList.class) .setInstantiator(defaultInstantiatorStrategy.newInstantiatorOf(ArrayList.class)); kryo.getRegistration(HashSet.class) .setInstantiator(defaultInstantiatorStrategy.newInstantiatorOf(HashSet.class)); UnmodifiableCollectionsSerializer.registerSerializers(kryo); final ByteArrayOutputStream out = new ByteArrayOutputStream(); final Output output = new Output(out); kryo.writeObject(output, node); output.flush(); output.close(); final ByteArrayInputStream inputStream = new ByteArrayInputStream(out.toByteArray()); final Input kryoInput = new Input(inputStream); final TreeNode tree = kryo.readObject(kryoInput, BaseTreeNode.class); final SingleDecisionTreeFactoryTest test = new SingleDecisionTreeFactoryTest(); test.checkTreeNode(tree, ruleSet); assertEquals(node, tree); }
public byte[] convertToBytes(Object dataArray) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024); Output output = new Output(byteArrayOutputStream); T[] array = (T[]) dataArray; Kryo kryo = KryoUtils.getKryo(); for (int i = 0; i < array.length; i++) { serializer.write(kryo, output, array[i]); } output.close(); return byteArrayOutputStream.toByteArray(); }
@Override public byte[] convert(Doughnut v) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try(Output output = new Output(baos)){ kryo.writeObject(output, v); } return baos.toByteArray(); }
/** * {@inheritDoc} * * @see marshalsec.MarshallerBase#marshal(java.lang.Object) */ @Override public byte[] marshal ( Object o ) throws Exception { com.esotericsoftware.kryo.Kryo k = makeKryo(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); try ( Output output = new Output(bos) ) { k.writeClassAndObject(output, o); } return bos.toByteArray(); }
@Override public void write(Kryo kryo, Output output, Locale locale) { if (locale != null) { this.stringSerializer.write(kryo, output, locale.getLanguage()); this.stringSerializer.write(kryo, output, locale.getCountry()); this.stringSerializer.write(kryo, output, locale.getVariant()); } else { this.stringSerializer.write(kryo, output, null); this.stringSerializer.write(kryo, output, null); this.stringSerializer.write(kryo, output, null); } }
@Override public CachedData encode(final Object obj) { final ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); try (Output output = new Output(byteStream)) { this.kryo.writeClassAndObject(output, obj); output.flush(); final byte[] bytes = byteStream.toByteArray(); return new CachedData(0, bytes, bytes.length); } }
@Override public void write(Kryo kryo, Output output, List object) { Preconditions.checkArgument(object instanceof ImmutableNullableList); output.writeInt(object.size()); final Iterator iterator = object.iterator(); while (iterator.hasNext()) { kryo.writeClassAndObject(output, iterator.next()); } }
public static float[][][] getInputData(String folder) throws IOException { if (cachedInputs != null) { return cachedInputs; } final float[][][] inputs; final Kryo kryo = new Kryo(); kryo.register(float[][][].class); final File dataBinFile = new File(getDataPath() + File.separator + "data-small.bin"); if (dataBinFile.exists()) { Logger.i("begin reading input data bin: %s", dataBinFile.getAbsolutePath()); Input input = new Input(new FileInputStream(dataBinFile)); inputs = kryo.readObject(input, float[][][].class); input.close(); Logger.i("begin reading input data bin: %s", dataBinFile.getAbsolutePath()); } else { Logger.i("begin parsing input data"); String inputFilePath = folder + File.separator + "test_data" + File.separator + "sensor"; inputs = parseInputData(inputFilePath); Logger.i("end parsing input data"); new Thread(new Runnable() { @Override public void run() { try { Output output = new Output(new FileOutputStream(dataBinFile)); kryo.writeObject(output, inputs); output.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } }).start(); } cachedInputs = inputs; return cachedInputs; }
@Override public void write(Kryo kryo, Output output, PortNumber object) { output.writeBoolean(object.hasName()); output.writeLong(object.toLong()); if (object.hasName()) { output.writeString(object.name()); } }
/** * @param topic topic associated with data * @param statAggregate typed data * @return serialized bytes */ @Override public byte[] serialize(String topic, StatAggregate statAggregate) { return pool.run(kryo -> { ByteArrayOutputStream stream = new ByteArrayOutputStream(); Output output = new Output(stream); kryo.writeClassAndObject(output, statAggregate); output.close(); return stream.toByteArray(); }); }
public byte[] serialize(TagValueFilterTree tagValueFilterTree) { return pool.run(kryo -> { ByteArrayOutputStream stream = new ByteArrayOutputStream(); Output output = new Output(stream); kryo.writeClassAndObject(output, tagValueFilterTree); output.close(); return stream.toByteArray(); }); }
@Override public CachedData encode(final Object obj) { final ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); final Output output = new Output(byteStream); kryo.writeClassAndObject(output, obj); output.flush(); IOUtils.closeQuietly(output); final byte[] bytes = byteStream.toByteArray(); return new CachedData(0, bytes, bytes.length); }
@Override public void write(Kryo kryo, Output output, IntArray array) { output.writeVarInt(array.size, true); output.writeBoolean(array.ordered); for (int i = 0; i < array.size; i++) { output.writeInt(array.get(i)); } }
public byte[] toBinary(Object value, Object meta, boolean insertMeta) { return toBinary(value, meta, insertMeta, (input) -> { ByteArrayOutputStream buffer = new ByteArrayOutputStream(1024); Output out = new Output(buffer); kryo.writeClassAndObject(out, input); out.flush(); return buffer.toByteArray(); }); }
@Override public CachedData encode(final Object obj) { final ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); try (final Output output = new Output(byteStream)) { kryo.writeClassAndObject(output, obj); output.flush(); final byte[] bytes = byteStream.toByteArray(); return new CachedData(0, bytes, bytes.length); } }
@Override public void write(final Kryo kryo, final Output output, final T object) { try { object.write(new DataOutputStream(output)); } catch (final IOException e) { throw new RuntimeException("unable to serialize Writable object", e); } }
private <T> T clone(T obj){ Kryo kryo = kryoFactory.getKryo(); try { Output output = new Output(1024, 1024 * 500); kryo.writeClassAndObject(output, obj); output.flush(); Input input = new Input(output.toBytes()); T t = (T) kryo.readClassAndObject(input); return t; } finally { kryoFactory.returnKryo(kryo);; } }
@Override public void write(Kryo kryo, Output output, Ray ray) { Vector3 origin = ray.origin; output.writeFloat(origin.x); output.writeFloat(origin.y); output.writeFloat(origin.z); Vector3 direction = ray.direction; output.writeFloat(direction.x); output.writeFloat(direction.y); output.writeFloat(direction.z); }