public void testFlushRoundTrip() throws Exception { Kryo kryo = new Kryo(); String s1 = "12345"; ByteArrayOutputStream os = new ByteArrayOutputStream(); ObjectOutputStream objOutput = new ObjectOutputStream(os); Output output = new Output(objOutput); kryo.writeClass(output, s1.getClass()); kryo.writeObject(output, s1); output.flush(); // objOutput.flush(); // this layer wasn't flushed prior to this bugfix, add it for a // workaround byte[] b = os.toByteArray(); System.out.println("size: " + b.length); ByteArrayInputStream in = new ByteArrayInputStream(b); ObjectInputStream objIn = new ObjectInputStream(in); Input input = new Input(objIn); Registration r = kryo.readClass(input); String s2 = kryo.readObject(input, r.getType()); assertEquals(s1, s2); }
/** * register a class * * @param kryo * @param s name of a class - might not exist * @param handled Set of classes already handles */ protected void doRegistration(final Kryo kryo, final Class pC) { if (kryo != null) { kryo.register(pC); // also register arrays of that class Class arrayType = Array.newInstance(pC, 0).getClass(); kryo.register(arrayType); } }
/** * do the real work of registering all classes * * @param kryo */ @Override public void registerClasses(@Nonnull Kryo kryo) { kryo.register(Object[].class); kryo.register(scala.Tuple2[].class); doRegistration(kryo, "scala.collection.mutable.WrappedArray$ofRef"); doRegistration(kryo, "com.nielsen.perfengg.SparkD"); // and many more similar nines }
private void execute(Map<Object, Object> map, int inserts) { Random random = new Random(); for (int i = 0; i < inserts; i++) map.put(random.nextLong(), random.nextBoolean()); Kryo kryo = new Kryo(); kryo.register(HashMap.class, new MapSerializer()); kryo.register(ConcurrentHashMap.class, new MapSerializer()); Output output = new Output(2048, -1); kryo.writeClassAndObject(output, map); output.close(); Input input = new Input(output.toBytes()); Object deserialized = kryo.readClassAndObject(input); input.close(); Assert.assertEquals(map, deserialized); }