Java Code Examples for com.esotericsoftware.kryo.io.Output#flush()
The following examples show how to use
com.esotericsoftware.kryo.io.Output#flush() .
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: FieldValueSerializableGenerator.java From attic-apex-malhar with Apache License 2.0 | 6 votes |
/** * get the object which is serialized. * this method will convert the object into a map from column name to column value and then serialize it * * @param obj * @return */ public byte[] serializeObject( Object obj ) { //if don't have field information, just convert the whole object to byte[] Object convertObj = obj; //if fields are specified, convert to map and then convert map to byte[] if ( fieldGetterMap != null && !fieldGetterMap.isEmpty() ) { convertObj = getFieldsValueAsMap(obj); } ByteArrayOutputStream os = new ByteArrayOutputStream(); Output output = new Output(os); getKryo().writeClassAndObject(output, convertObj); output.flush(); //output.toBytes() is empty. return os.toByteArray(); }
Example 2
Source File: KryoSerializer.java From journalkeeper with 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 File: MessageEncoder.java From lionrpc with Apache License 2.0 | 6 votes |
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); } } } }
Example 4
Source File: EventCodec.java From attic-apex-malhar with Apache License 2.0 | 6 votes |
@Override public Slice toByteArray(Event event) { ByteArrayOutputStream os = new ByteArrayOutputStream(); Output output = new Output(os); Map<String, String> headers = event.getHeaders(); if (headers != null && headers.getClass() != HashMap.class) { HashMap<String, String> tmp = new HashMap<String, String>(headers.size()); tmp.putAll(headers); headers = tmp; } kryo.writeObjectOrNull(output, headers, HashMap.class); kryo.writeObjectOrNull(output, event.getBody(), byte[].class); output.flush(); final byte[] bytes = os.toByteArray(); return new Slice(bytes, 0, bytes.length); }
Example 5
Source File: LongBloomFilterTest.java From gatk with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test void serializationTest() { final Random rng = new Random(RAND_SEED); final LongBloomFilter bloomFilter = new LongBloomFilter(HHASH_NVALS, FPP); final HashSet<Long> hashSet = new HashSet<>(HHASH_NVALS); for (int valNo = 0; valNo != HHASH_NVALS; ++valNo) { final long randLong = randomLong(rng); bloomFilter.add(randLong); hashSet.add(randLong); } final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final Output out = new Output(bos); final Kryo kryo = new Kryo(); kryo.writeObject(out, bloomFilter); out.flush(); final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); final Input in = new Input(bis); final LongBloomFilter bloomFilter2 = kryo.readObject(in, LongBloomFilter.class); Assert.assertEquals(bloomFilter, bloomFilter2); for (Long val : hashSet) { Assert.assertTrue(bloomFilter2.contains(val)); } }
Example 6
Source File: NovelAdjacencyAndAltHaplotypeUnitTest.java From gatk with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test(groups = "sv", dataProvider = "forKryoSerializationAndHashCode") public void testKryoSerializerAndHashCode(final NovelAdjacencyAndAltHaplotype novelAdjacencyAndAltHaplotype) throws IOException { try (final ByteArrayOutputStream bos = new ByteArrayOutputStream()) { final Output out = new Output(bos); final Kryo kryo = new Kryo(); kryo.writeClassAndObject(out, novelAdjacencyAndAltHaplotype); out.flush(); try ( final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()) ) { final Input in = new Input(bis); @SuppressWarnings("unchecked") final NovelAdjacencyAndAltHaplotype roundTrip = (NovelAdjacencyAndAltHaplotype) kryo.readClassAndObject(in); Assert.assertEquals(roundTrip, novelAdjacencyAndAltHaplotype); Assert.assertEquals(roundTrip.hashCode(), novelAdjacencyAndAltHaplotype.hashCode()); } } }
Example 7
Source File: PercentileSerializerTest.java From kylin with Apache License 2.0 | 5 votes |
public static byte[] serialize(final Kryo kryo, final Object o) { if (o == null) { throw new NullPointerException("Can't serialize null"); } final Output output = new Output(4096); kryo.writeObject(output, o); output.flush(); return output.getBuffer(); }
Example 8
Source File: AlignedAssemblyUnitTest.java From gatk with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test(dataProvider="AlignedAssemblySerializationTest", groups = "sv") public void testAlignedAssemblySerialization(final Integer assemblyID, final AlignedAssembly expectedAssembly) { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final Output out = new Output(bos); final Kryo kryo = new Kryo(); kryo.writeClassAndObject(out, expectedAssembly); out.flush(); final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); final Input in = new Input(bis); @SuppressWarnings("unchecked") final AlignedAssembly roundTrip = (AlignedAssembly) kryo.readClassAndObject(in); Assert.assertEquals(roundTrip, expectedAssembly); }
Example 9
Source File: KryoTranscoder.java From cas4.0.x-server-wechat with Apache License 2.0 | 5 votes |
@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); }
Example 10
Source File: Journal.java From attic-apex-core with Apache License 2.0 | 5 votes |
final void write(Recoverable op) { if (replayMode.get()) { throw new IllegalStateException("Request to write while journal is replaying operations"); } Integer classId = RecoverableOperation.getId(op.getClass()); if (classId == null) { throw new IllegalArgumentException("Class not registered " + op.getClass()); } while (true) { final Output out = output.get(); if (out != null) { // need to atomically write id, operation and flush the output stream synchronized (out) { try { LOG.debug("WAL write {}", RecoverableOperation.get(classId)); out.writeInt(classId); op.write(out); out.flush(); break; } catch (KryoException e) { // check that no other threads sneaked between get() and synchronized block and set output stream to a new // stream or null leading to the current stream being closed if (output.get() == out) { throw e; } } } } else { LOG.warn("Journal output stream is null. Skipping write to the WAL."); break; } } }
Example 11
Source File: KryoDemo.java From javatech with Creative Commons Attribution Share Alike 4.0 International | 5 votes |
/** * 将对象序列化为 byte 数组 * * @param obj 任意对象 * @param <T> 对象的类型 * @return 序列化后的 byte 数组 */ public static <T> byte[] writeToBytes(T obj) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); Output output = new Output(byteArrayOutputStream); Kryo kryo = getInstance(); kryo.writeObject(output, obj); output.flush(); return byteArrayOutputStream.toByteArray(); }
Example 12
Source File: SerDeUtils.java From metron with Apache License 2.0 | 5 votes |
/** * Serialize a profile measurement's value. * * The value produced by a Profile definition can be any numeric data type. The data * type depends on how the profile is defined by the user. The user should be able to * choose the data type that is most suitable for their use case. * * @param value The value to serialize. */ public static byte[] toBytes(Object value) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Output output = new Output(bos); kryo.get().writeClassAndObject(output, value); output.flush(); bos.flush(); return bos.toByteArray(); } catch(Throwable t) { LOG.error("Unable to serialize: " + value + " because " + t.getMessage(), t); throw new IllegalStateException("Unable to serialize " + value + " because " + t.getMessage(), t); } }
Example 13
Source File: EncodeCriterionCodecHelper.java From onos with Apache License 2.0 | 5 votes |
@Override public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) { Output output = new Output(new ByteArrayOutputStream()); KryoNamespaces.API.borrow().writeObject(output, criterion); root.put(CriterionCodec.EXTENSION, output.toBytes()); output.flush(); output.close(); return root; }
Example 14
Source File: CpxVariantInducingAssemblyContigUnitTest.java From gatk with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Test(groups = "sv", dataProvider = "forSerialization") public void testSerialization(final CpxVariantInducingAssemblyContig cpxVariantInducingAssemblyContig) { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final Output out = new Output(bos); final Kryo kryo = new Kryo(); kryo.writeClassAndObject(out, cpxVariantInducingAssemblyContig); out.flush(); final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); final Input in = new Input(bis); @SuppressWarnings("unchecked") final CpxVariantInducingAssemblyContig roundTrip = (CpxVariantInducingAssemblyContig) kryo.readClassAndObject(in); Assert.assertEquals(cpxVariantInducingAssemblyContig, roundTrip); Assert.assertEquals(cpxVariantInducingAssemblyContig.hashCode(), roundTrip.hashCode()); }
Example 15
Source File: KryoManager.java From Mundus with Apache License 2.0 | 5 votes |
/** * Saves the registry * * @param registry * mundus registry */ public void saveRegistry(Registry registry) { try { Output output = new Output(new FileOutputStream(Registry.HOME_DATA_FILE)); RegistryDescriptor descriptor = DescriptorConverter.convert(registry); kryo.writeObject(output, descriptor); output.flush(); output.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } }
Example 16
Source File: ModelIO.java From ContactMerger with Apache License 2.0 | 5 votes |
public static byte[] generate(ArrayList<MergeContact> model) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Kryo kryo = new Kryo(); register(kryo); Output output = new Output(new GZIPOutputStream(baos)); kryo.writeObject(output, model); output.flush(); output.close(); return baos.toByteArray(); }
Example 17
Source File: FSStorageAgent.java From Bats with Apache License 2.0 | 5 votes |
public static void store(OutputStream stream, Object operator) { synchronized (kryo) { Output output = new Output(4096, Integer.MAX_VALUE); output.setOutputStream(stream); kryo.writeClassAndObject(output, operator); output.flush(); } }
Example 18
Source File: Journal.java From Bats with Apache License 2.0 | 5 votes |
final void write(Recoverable op) { if (replayMode.get()) { throw new IllegalStateException("Request to write while journal is replaying operations"); } Integer classId = RecoverableOperation.getId(op.getClass()); if (classId == null) { throw new IllegalArgumentException("Class not registered " + op.getClass()); } while (true) { final Output out = output.get(); if (out != null) { // need to atomically write id, operation and flush the output stream synchronized (out) { try { LOG.debug("WAL write {}", RecoverableOperation.get(classId)); out.writeInt(classId); op.write(out); out.flush(); break; } catch (KryoException e) { // check that no other threads sneaked between get() and synchronized block and set output stream to a new // stream or null leading to the current stream being closed if (output.get() == out) { throw e; } } } } else { LOG.warn("Journal output stream is null. Skipping write to the WAL."); break; } } }
Example 19
Source File: KryoHttpMessageConverter.java From tutorials with MIT License | 5 votes |
@Override protected void writeInternal(final Object object, final HttpOutputMessage outputMessage) throws IOException { final Output output = new Output(outputMessage.getBody()); kryoThreadLocal.get().writeClassAndObject(output, object); output.flush(); }
Example 20
Source File: ExternalizeTestData.java From chronix.spark with Apache License 2.0 | 4 votes |
/** * @param args optional first argument: file to serialize to. A default file name is provided. * @throws SolrServerException * @throws FileNotFoundException */ public static void main(String[] args) throws SolrServerException, IOException { ChronixSparkLoader chronixSparkLoader = new ChronixSparkLoader(); ChronixYAMLConfiguration config = chronixSparkLoader.getConfig(); String file = (args.length >= 1) ? args[0] : config.getTestdataFile(); Path filePath = Paths.get(file); Files.deleteIfExists(filePath); Output output = new Output(new DeflaterOutputStream(new FileOutputStream(filePath.toString()))); System.out.println("Opening test data file: " + filePath.toString()); ChronixSparkContext cSparkContext = null; //Create target file try { //Create Chronix Spark context cSparkContext = chronixSparkLoader.createChronixSparkContext(); //Read data into ChronixRDD SolrQuery query = new SolrQuery(config.getSolrReferenceQuery()); ChronixRDD rdd = cSparkContext.queryChronixChunks(query, config.getZookeeperHost(), config.getChronixCollection(), config.getStorage()); System.out.println("Writing " + rdd.count() + " time series into test data file."); //Loop through result and serialize it to disk Kryo kryo = new Kryo(); List<MetricTimeSeries> mtsList = IteratorUtils.toList(rdd.iterator()); System.out.println("Writing objects..."); kryo.writeObject(output, mtsList); output.flush(); System.out.println("Objects written."); } finally { output.close(); if (cSparkContext != null) { cSparkContext.getSparkContext().close(); } System.out.println("Test data file written successfully!"); } }