Java Code Examples for com.esotericsoftware.kryo.Kryo
The following examples show how to use
com.esotericsoftware.kryo.Kryo.
These examples are extracted from open source projects.
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 Project: gatk Author: broadinstitute File: PSTreeUnitTest.java License: BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void testSerialize() throws Exception { final PSTree tree = new PSTree(1); tree.addNode(2, "n2", 1, 0, "none"); tree.addNode(3, "n3", 1, 0, "none"); tree.addNode(4, "n4", 2, 0, "none"); try { final File tempFile = createTempFile("test", ".dat"); final Kryo kryo = new Kryo(); kryo.setReferences(false); Output output = new Output(new FileOutputStream(tempFile)); kryo.writeObject(output, tree); output.close(); final Input input = new Input(new FileInputStream(tempFile)); final PSTree treeIn = kryo.readObject(input, PSTree.class); Assert.assertEquals(treeIn, tree); } catch (FileNotFoundException e) { throw new IOException("Error with Kryo IO", e); } }
Example #2
Source Project: journalkeeper Author: chubaostream File: KryoSerializer.java License: Apache License 2.0 | 6 votes |
@Override public byte[] serialize(Object entry) { if (entry == null) { return ArrayUtils.EMPTY_BYTE_ARRAY; } Kryo kryo = kryoPool.borrow(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(BUFFER_SIZE); Output output = new Output(outputStream); if (type == null) { kryo.writeClassAndObject(output, entry); } else { kryo.writeObject(output, entry); } kryoPool.release(kryo); output.flush(); byte[] result = outputStream.toByteArray(); output.close(); return result; }
Example #3
Source Project: Lottor Author: keets2012 File: KryoSerializer.java License: MIT License | 6 votes |
/** * 序列化 * * @param obj 需要序更列化的对象 * @return 序列化后的byte 数组 * @throws TransactionException */ @Override public byte[] serialize(Object obj) throws TransactionException { 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 TransactionException("kryo serialize error" + ex.getMessage()); } finally { try { outputStream.flush(); outputStream.close(); } catch (IOException e) { } } return bytes; }
Example #4
Source Project: hgraphdb Author: rayokota File: EdgeValueWritable.java License: Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") @Override public void readFields(final DataInput input) throws IOException { try { Kryo kryo = new Kryo(); kryo.register(HBaseEdge.class, new HBaseEdgeSerializer()); final ByteArrayInputStream bais = new ByteArrayInputStream(WritableUtils.readCompressedByteArray(input)); this.edge = kryo.readObject(new Input(bais), HBaseEdge.class); Class<? extends Writable> cls = Class.forName(Text.readString(input)).asSubclass(Writable.class); Writable writable; if (cls.equals(NullWritable.class)) { writable = NullWritable.get(); } else { writable = cls.newInstance(); } writable.readFields(input); this.value = writable != NullWritable.get() ? (V) writable : null; } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) { throw new IOException("Failed writable init", e); } }
Example #5
Source Project: attic-apex-malhar Author: apache File: AbstractFileInputOperatorTest.java License: Apache License 2.0 | 6 votes |
/** * This method checkpoints the given operator. * @param oper The operator to checkpoint. * @param bos The ByteArrayOutputStream which saves the checkpoint data temporarily. * @return new operator. */ public static <T> T checkpoint(T oper, ByteArrayOutputStream bos) throws Exception { Kryo kryo = new Kryo(); Output loutput = new Output(bos); kryo.writeObject(loutput, oper); loutput.close(); Input lInput = new Input(bos.toByteArray()); @SuppressWarnings("unchecked") T checkPointedOper = kryo.readObject(lInput, (Class<T>)oper.getClass()); lInput.close(); return checkPointedOper; }
Example #6
Source Project: ytk-mp4j Author: yuantiku File: ByteOperand.java License: MIT License | 6 votes |
@Override public MapMetaData<Byte> read(Kryo kryo, Input input, Class<MapMetaData<Byte>> type) { try { thatMapMetaData = mapMetaData.recv(input); int thatMapSegNum = thatMapMetaData.getSegNum(); List<Map<String, Byte>> mapDataList = new ArrayList<>(thatMapSegNum); thatMapMetaData.setMapDataList(mapDataList); for (int i = 0; i < thatMapSegNum; i++) { int dataNum = thatMapMetaData.getDataNum(i); Map<String, Byte> mapData = new HashMap<>(dataNum); mapDataList.add(mapData); for (int j = 0; j < dataNum; j++) { String key = input.readString(); Byte val = input.readByte(); mapData.put(key, val); } } } catch (IOException e) { LOG.error("double array read exception", e); System.exit(1); } return thatMapMetaData; }
Example #7
Source Project: flink Author: flink-tpc-ds File: AvroKryoClassloadingTest.java License: Apache License 2.0 | 6 votes |
@Test public void testKryoInChildClasspath() throws Exception { final Class<?> avroClass = AvroKryoSerializerUtils.class; final URL avroLocation = avroClass.getProtectionDomain().getCodeSource().getLocation(); final URL kryoLocation = Kryo.class.getProtectionDomain().getCodeSource().getLocation(); final ClassLoader parentClassLoader = new FilteredClassLoader( avroClass.getClassLoader(), AvroKryoSerializerUtils.class.getName()); final ClassLoader userAppClassLoader = FlinkUserCodeClassLoaders.childFirst( new URL[] { avroLocation, kryoLocation }, parentClassLoader, CoreOptions.ALWAYS_PARENT_FIRST_LOADER_PATTERNS.defaultValue().split(";")); final Class<?> userLoadedAvroClass = Class.forName(avroClass.getName(), false, userAppClassLoader); assertNotEquals(avroClass, userLoadedAvroClass); // call the 'addAvroGenericDataArrayRegistration(...)' method final Method m = userLoadedAvroClass.getMethod("addAvroGenericDataArrayRegistration", LinkedHashMap.class); final LinkedHashMap<String, ?> map = new LinkedHashMap<>(); m.invoke(userLoadedAvroClass.newInstance(), map); assertEquals(1, map.size()); }
Example #8
Source Project: ytk-mp4j Author: yuantiku File: ShortOperand.java License: MIT License | 6 votes |
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); } }
Example #9
Source Project: spliceengine Author: splicemachine File: BulkWriteResult.java License: GNU Affero General Public License v3.0 | 6 votes |
@Override public BulkWriteResult read(Kryo kryo, Input input, Class<BulkWriteResult> type) { WriteResult globalStatus = kryo.readObject(input,WriteResult.class); int notRunSize = input.readInt(); IntHashSet notRunRows = new IntHashSet(notRunSize); for(int i=0;i<notRunSize;i++){ notRunRows.add(input.readInt()); } int failedSize = input.readInt(); IntObjectHashMap<WriteResult> failedRows = new IntObjectHashMap<>(failedSize,0.9f); for(int i=0;i<failedSize;i++){ int k = input.readInt(); WriteResult result = kryo.readObject(input,WriteResult.class); failedRows.put(k,result); } return new BulkWriteResult(globalStatus,notRunRows,failedRows); }
Example #10
Source Project: jea Author: yiyongfei File: AbstractSerializer.java License: Apache License 2.0 | 6 votes |
/** * 根据所注册的Class,初始化Kryo实例 * * @return */ protected Kryo initKryo() { Kryo kryo = new Kryo(); kryo.setReferences(true); kryo.setRegistrationRequired(true); kryo.setInstantiatorStrategy(new StdInstantiatorStrategy()); Set<Class<?>> keys = propertyMap.keySet(); for(Class<?> key : keys){ if(propertyMap.get(key) != null){ kryo.register(key, propertyMap.get(key)); } else { kryo.register(key); } } return kryo; }
Example #11
Source Project: pragmatic-java-engineer Author: superhj1987 File: KryoExample.java License: GNU General Public License v3.0 | 6 votes |
public static void main(String[] args) { Kryo kryo = new Kryo(); // 序列化 ByteArrayOutputStream os = new ByteArrayOutputStream(); Output output = new Output(os); TestUser user = new TestUser(); kryo.writeObject(output, user); output.close(); byte[] bytes = os.toByteArray(); // 反序列化 Input input = new Input(new ByteArrayInputStream(bytes)); user = kryo.readObject(input, TestUser.class); input.close(); }
Example #12
Source Project: springJredisCache Author: chenleijava File: KryoPoolSer.java License: Apache License 2.0 | 6 votes |
KryoHolder(Kryo kryo) { this.kryo = kryo; // register this.kryo.register(Arrays.asList("").getClass(), new ArraysAsListSerializer()); this.kryo.register(Collections.EMPTY_LIST.getClass(), new CollectionsEmptyListSerializer()); this.kryo.register(Collections.EMPTY_MAP.getClass(), new CollectionsEmptyMapSerializer()); this.kryo.register(Collections.EMPTY_SET.getClass(), new CollectionsEmptySetSerializer()); this.kryo.register(Collections.singletonList("").getClass(), new CollectionsSingletonListSerializer()); this.kryo.register(Collections.singleton("").getClass(), new CollectionsSingletonSetSerializer()); this.kryo.register(Collections.singletonMap("", "").getClass(), new CollectionsSingletonMapSerializer()); this.kryo.register(GregorianCalendar.class, new GregorianCalendarSerializer()); this.kryo.register(InvocationHandler.class, new JdkProxySerializer()); // register CGLibProxySerializer, works in combination with the appropriate action in handleUnregisteredClass (see below) this.kryo.register(CGLibProxySerializer.CGLibProxyMarker.class, new CGLibProxySerializer()); UnmodifiableCollectionsSerializer.registerSerializers(this.kryo); SynchronizedCollectionsSerializer.registerSerializers(this.kryo); //其他第三方 // // joda datetime // kryo.register(DateTime.class, new JodaDateTimeSerializer()); // // wicket // kryo.register(MiniMap.class, new MiniMapSerializer()); // // guava ImmutableList // ImmutableListSerializer.registerSerializers(kryo); }
Example #13
Source Project: metron Author: apache File: ProfilePeriodTest.java License: Apache License 2.0 | 6 votes |
/** * Ensure that the ProfilePeriod can undergo Kryo serialization which * occurs when the Profiler is running in Storm. */ @Test public void testKryoSerialization() { ProfilePeriod expected = ProfilePeriod.fromTimestamp(AUG2016, 1, TimeUnit.HOURS); Kryo kryo = new Kryo(); // serialize ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); Output output = new Output(byteStream); kryo.writeObject(output, expected); // validate serialization byte[] bits = output.toBytes(); assertNotNull(bits); // deserialize Input input = new Input(new ByteArrayInputStream(bits)); ProfilePeriod actual = kryo.readObject(input, ProfilePeriod.class); // validate deserialization assertNotNull(actual); assertEquals(expected, actual); }
Example #14
Source Project: gatk Author: broadinstitute File: ReadMetadata.java License: BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Reads a read-metadata from a file or resource. * @param whereFrom the file or resource containing the read-metadata. * @throws IllegalArgumentException if {@code whereFrom} is {@code null}. * @throws UserException if any problem occurred where deserializing. * @return never {@code null}. */ public static ReadMetadata readStandalone(final String whereFrom) { try (final InputStream inputStream = BucketUtils.openFile(whereFrom); final Input input = new Input(inputStream)) { final Kryo kryo = new Kryo(); final Serializer serializer = new Serializer(); final String magicString = input.readString(); if (!Objects.equals(MAGIC_STRING, magicString)) { throw new UserException.BadInput("Bad file format in " + whereFrom + "; it does not seem to be a valid read-metadata serialization"); } final String versionString = input.readString(); if (!Objects.equals(VERSION_STRING, versionString)) { throw new UserException.BadInput("Bad file format in " + whereFrom + "; it contains an incompatible version " + versionString + "(expected: " + VERSION_STRING + ")"); } final ReadMetadata result = serializer.read(kryo, input, ReadMetadata.class); if (result == null) { throw new UserException.BadInput("Missing read-metadata in " + whereFrom); } return result; } catch (final Exception ex) { throw new UserException.CouldNotCreateOutputFile(whereFrom, ex); } }
Example #15
Source Project: flink Author: apache File: AvroKryoSerializerRegistrationsTest.java License: Apache License 2.0 | 6 votes |
/** * Creates a Kryo serializer and writes the default registrations out to a * comma separated file with one entry per line: * * <pre> * id,class * </pre> * * <p>The produced file is used to check that the registered IDs don't change * in future Flink versions. * * <p>This method is not used in the tests, but documents how the test file * has been created and can be used to re-create it if needed. * * @param filePath File path to write registrations to */ private void writeDefaultKryoRegistrations(String filePath) throws IOException { final File file = new File(filePath); if (file.exists()) { assertTrue(file.delete()); } final Kryo kryo = new KryoSerializer<>(Integer.class, new ExecutionConfig()).getKryo(); final int nextId = kryo.getNextRegistrationId(); try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) { for (int i = 0; i < nextId; i++) { Registration registration = kryo.getRegistration(i); String str = registration.getId() + "," + registration.getType().getName(); writer.write(str, 0, str.length()); writer.newLine(); } System.out.println("Created file with registrations at " + file.getAbsolutePath()); } }
Example #16
Source Project: artemis-odb-orion Author: FEOMedia File: OrionKryoSerialization.java License: Apache License 2.0 | 5 votes |
@Override public Interpolation.BounceIn copy(Kryo kryo, Interpolation.BounceIn original) { try { float[] widths = (float[]) fieldW.get(original); return new Interpolation.BounceIn(widths.length); } catch (IllegalAccessException e) { throw new RuntimeException(e); } }
Example #17
Source Project: kryonet Author: EsotericSoftware File: Network.java License: BSD 3-Clause "New" or "Revised" License | 5 votes |
static public void register (EndPoint endPoint) { Kryo kryo = endPoint.getKryo(); kryo.register(Login.class); kryo.register(RegistrationRequired.class); kryo.register(Register.class); kryo.register(AddCharacter.class); kryo.register(UpdateCharacter.class); kryo.register(RemoveCharacter.class); kryo.register(Character.class); kryo.register(MoveCharacter.class); }
Example #18
Source Project: ytk-mp4j Author: yuantiku File: ProcessCommSlave.java License: MIT License | 5 votes |
@Override public List<T> read(Kryo kryo, Input input, Class<List<T>> type) { int size = input.readInt(); List<T> list = new ArrayList<T>(size); for (int i = 0; i < size; i++) { list.add(valSerializer.read(kryo, input, this.valType)); } return list; }
Example #19
Source Project: artemis-odb-orion Author: FEOMedia File: OrionKryoSerialization.java License: Apache License 2.0 | 5 votes |
@Override public void write(Kryo kryo, Output output, Interpolation.Elastic object) { output.writeFloat(object.value); output.writeFloat(object.power); output.writeFloat(abs(object.scale / MathUtils.PI)); output.writeFloat(object.bounces); }
Example #20
Source Project: stratosphere Author: stratosphere File: WritableSerializer.java License: Apache License 2.0 | 5 votes |
private final void checkKryoInitialized() { if (this.kryo == null) { this.kryo = new Kryo(); this.kryo.setAsmEnabled(true); this.kryo.register(typeClass); } }
Example #21
Source Project: incubator-heron Author: apache File: SerializableSerializer.java License: Apache License 2.0 | 5 votes |
@Override public void write(Kryo kryo, Output output, Object object) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(object); oos.flush(); } catch (IOException e) { throw new RuntimeException(e); } byte[] ser = bos.toByteArray(); output.writeInt(ser.length); output.writeBytes(ser); }
Example #22
Source Project: yauaa Author: nielsbasjes File: AbstractUserAgentAnalyzerDirect.java License: Apache License 2.0 | 5 votes |
@Override public AbstractUserAgentAnalyzerDirect read(Kryo kryo, Input input, Class<AbstractUserAgentAnalyzerDirect> type) { AbstractUserAgentAnalyzerDirect uaa = super.read(kryo, input, type); uaa.initTransientFields(); uaa.showDeserializationStats(); return uaa; }
Example #23
Source Project: tassal Author: mast-group File: KryoSerialization.java License: BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public byte[] serialize(final Object obj) throws SerializationException { LOGGER.info("Serializing object of type " + obj.getClass().getName() + " to byte[]"); final Kryo kryo = new Kryo(); final ByteArrayOutputStream st = new ByteArrayOutputStream(); final Output output = new Output(st); kryo.writeClassAndObject(output, obj); output.close(); return st.toByteArray(); }
Example #24
Source Project: hgraphdb Author: rayokota File: HBaseEdgeSerializer.java License: Apache License 2.0 | 5 votes |
public HBaseEdge read(Kryo kryo, Input input, Class<? extends HBaseEdge> type) { HBaseEdge edge = super.read(kryo, input, type); int outVBytesLen = input.readInt(); Object outVId = ValueUtils.deserialize(input.readBytes(outVBytesLen)); int inVBytesLen = input.readInt(); Object inVId = ValueUtils.deserialize(input.readBytes(inVBytesLen)); edge.setOutVertex(new HBaseVertex(null, outVId)); edge.setInVertex(new HBaseVertex(null, inVId)); return edge; }
Example #25
Source Project: attic-apex-core Author: apache File: DefaultStatefulStreamCodecTest.java License: Apache License 2.0 | 5 votes |
@Test public void testVirginKryo() { Kryo coder = new Kryo(); Kryo decoder = new Kryo(); ClassIdPair cip = new ClassIdPair(); Output output = new Output(4096, Integer.MAX_VALUE); coder.writeClassAndObject(output, cip); Input input = new Input(); input.setBuffer(output.toBytes()); decoder.readClassAndObject(input); }
Example #26
Source Project: swblocks-decisiontree Author: jpmorganchase File: TreeSerializationTest.java License: Apache License 2.0 | 5 votes |
@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); }
Example #27
Source Project: trainbenchmark Author: ftsrg File: AbstractConfig.java License: Eclipse Public License 1.0 | 5 votes |
public static <T extends AbstractConfig<?>> T fromFile(final String path, final Class<T> clazz) throws FileNotFoundException { final Kryo kryo = new Kryo(); kryo.setInstantiatorStrategy(new DefaultInstantiatorStrategy(new StdInstantiatorStrategy())); try (final Input input = new Input(new FileInputStream(path))) { final T bc = kryo.readObject(input, clazz); return bc; } }
Example #28
Source Project: Thunder Author: Nepxion File: BinarySerializerTest.java License: Apache License 2.0 | 5 votes |
@Test public void testKyroFunction1() throws Exception { User user1 = CoreFactory.createUser1(); Kryo kryo = KryoSerializerFactory.createKryo(); byte[] bytes = KryoSerializer.serialize(kryo, user1); LOG.info(bytes.toString()); User user2 = KryoSerializer.deserialize(kryo, bytes); LOG.info(user2.toString()); }
Example #29
Source Project: geowave Author: locationtech File: GridCoverageWritableSerializer.java License: Apache License 2.0 | 5 votes |
@Override public GridCoverageWritable read( final Kryo arg0, final Input arg1, final Class<GridCoverageWritable> arg2) { final GridCoverageWritable gcw = new GridCoverageWritable(); final byte[] data = arg1.readBytes(arg1.readInt()); try (DataInputStream is = new DataInputStream(new ByteArrayInputStream(data))) { gcw.readFields(is); } catch (final IOException e) { LOGGER.error("Cannot deserialize GridCoverageWritable", e); return null; } return gcw; }
Example #30
Source Project: samoa Author: YahooArchive File: AttributeContentEvent.java License: Apache License 2.0 | 5 votes |
@Override public void write(Kryo kryo, Output output, AttributeContentEvent event) { output.writeLong(event.learningNodeId, true); output.writeInt(event.obsIndex, true); output.writeDouble(event.attrVal, PRECISION, true); output.writeInt(event.classVal, true); output.writeDouble(event.weight, PRECISION, true); output.writeBoolean(event.isNominal); }