Java Code Examples for com.esotericsoftware.kryo.io.Output#close()

The following examples show how to use com.esotericsoftware.kryo.io.Output#close() . 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: SerializerTest.java    From jim-framework with Apache License 2.0 6 votes vote down vote up
public static <T> byte[] serialize(T obj) {
    if (obj == null) {
        throw new RuntimeException("KroySerialize.serialize:obj is null");
    }

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    Output ko = new Output(output);

    try {
        kryo.writeObject(ko, obj);
        return ko.toBytes();
    }
    finally {
        ko.close();
    }


}
 
Example 2
Source File: MessageEncoder.java    From lionrpc with Apache License 2.0 6 votes vote down vote up
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 3
Source File: SerializerImpl.java    From tephra with MIT License 6 votes vote down vote up
@Override
public void serialize(Object object, OutputStream outputStream) {
    if (object == null || outputStream == null)
        return;

    if (object instanceof JSONObject) {
        serializeJson(jsonObject, object, outputStream);

        return;
    }

    if (object instanceof JSONArray) {
        serializeJson(jsonArray, object, outputStream);

        return;
    }

    Output output = new Output(outputStream);
    kryo().writeClassAndObject(output, object);
    output.close();
}
 
Example 4
Source File: TinyMetricTest.java    From cubedb with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testSerDe() {
  TinyMetric metric = createMetric();
  for (int i = 1; i <= 10; i++) {
    metric.append(i);
  }
  Kryo kryo = new Kryo();
  ByteArrayOutputStream bao = new ByteArrayOutputStream();
  Output output = new Output(bao);
  kryo.writeObject(output, metric);
  output.close();
  TinyMetric deser =
      kryo.readObject(new Input(new ByteArrayInputStream(bao.toByteArray())), TinyMetric.class);
  assertEquals(metric.size(), deser.size());
  assertEquals(metric.getNumRecords(), deser.getNumRecords());
  for (int i = 0; i < metric.getNumRecords(); i++) {
    assertEquals(metric.get(i), deser.get(i));
  }
}
 
Example 5
Source File: AbstractFileInputOperatorTest.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
/**
 * 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 File: ReadMetadata.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Serializes a read-metadata by itself into a file.
 * @param meta the read-metadata to serialize.
 * @param whereTo the name of the file or resource where it will go to.
 * @throws IllegalArgumentException if either {@code meta} or {@code whereTo}
 * is {@code null}.
 * @throws UserException if there was a problem during serialization.
 */
public static void writeStandalone(final ReadMetadata meta, final String whereTo) {
    try {
        final OutputStream outputStream = BucketUtils.createFile(whereTo);
        final OutputStream actualStream = IOUtil.hasBlockCompressedExtension(whereTo)
                ? new GzipCompressorOutputStream(outputStream) : outputStream;
        final Output output = new Output(actualStream);
        final Kryo kryo = new Kryo();
        final Serializer serializer = new Serializer();
        output.writeString(MAGIC_STRING);
        output.writeString(VERSION_STRING);
        serializer.write(kryo, output, meta);
        output.close();
    } catch (final IOException ex) {
        throw new UserException.CouldNotCreateOutputFile(whereTo, ex);
    }
}
 
Example 7
Source File: HoodieTestUtils.java    From hudi with Apache License 2.0 5 votes vote down vote up
public static <T extends Serializable> T serializeDeserialize(T object, Class<T> clazz) {
  // Using Kyro as the default serializer in Spark Jobs
  Kryo kryo = new Kryo();
  kryo.register(HoodieTableMetaClient.class, new JavaSerializer());

  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  Output output = new Output(baos);
  kryo.writeObject(output, object);
  output.close();

  Input input = new Input(new ByteArrayInputStream(baos.toByteArray()));
  T deseralizedObject = kryo.readObject(input, clazz);
  input.close();
  return deseralizedObject;
}
 
Example 8
Source File: TestUtils.java    From cubedb with GNU General Public License v3.0 5 votes vote down vote up
public static File dumpToTmpFile(Partition p) throws IOException {
  File destination = File.createTempFile("partition_", ".gz");
  Output output = new Output(new GZIPOutputStream(new FileOutputStream(destination)));
  long t0 = System.nanoTime();
  Kryo kryo = new Kryo();
  kryo.writeClassAndObject(output, p);
  output.close();
  long t1 = System.nanoTime();
  log.info("Took {} ms to write {} records", (t1 - t0) / 1000000, p.getNumRecords());
  destination.deleteOnExit();
  return destination;
}
 
Example 9
Source File: KryoManager.java    From Mundus with Apache License 2.0 5 votes vote down vote up
/**
 * Saves the project context.
 *
 * Saves only the project's .pro file, not the individual scenes.
 *
 * @param context
 *            project context to save
 */
public void saveProjectContext(ProjectContext context) {
    try {
        Output output = new Output(new FileOutputStream(context.path + "/" +
                context.name + "." + ProjectManager.PROJECT_EXTENSION));

        ProjectDescriptor descriptor = DescriptorConverter.convert(context);
        kryo.writeObject(output, descriptor);

        output.flush();
        output.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}
 
Example 10
Source File: KryoSerialization.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void serialize(final Object obj, final String filename)
		throws SerializationException {
	LOGGER.info("Serializing object of type " + obj.getClass().getName()
			+ " to " + filename);
	try {
		final Kryo kryo = new Kryo();
		final Output output = new Output(new FileOutputStream(filename));
		kryo.writeClassAndObject(output, obj);
		output.close();
	} catch (final FileNotFoundException e) {
		throw new ISerializationStrategy.SerializationException(e);
	}

}
 
Example 11
Source File: TestSerDeserPer.java    From eagle with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerDeserPerf() throws Exception {
    Kryo kryo = new Kryo();
    String outputPath = temporaryFolder.newFile().toString();
    Output output = new Output(new FileOutputStream(outputPath));
    for (int i = 0; i < 1000; i++) {
        kryo.writeObject(output, constructPE());
    }
    output.close();
    Input input = new Input(new FileInputStream(outputPath));
    PartitionedEvent someObject = kryo.readObject(input, PartitionedEvent.class);
    input.close();
    Assert.assertTrue(someObject.getData().length == 1);
}
 
Example 12
Source File: PSKmerUtils.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Writes an object to a URI using Kryo serialization.
 */
public static void writeKryoObject(final Object obj, String uri) {
    final Output output = new Output(BucketUtils.createFile(uri));
    final Kryo kryo = new Kryo();
    kryo.writeObject(output, obj);
    output.close();
}
 
Example 13
Source File: ModelIO.java    From ContactMerger with Apache License 2.0 5 votes vote down vote up
public static void store(ArrayList<MergeContact> model, File file)
        throws FileNotFoundException, IOException
{
    Kryo kryo = new Kryo();
    register(kryo);
    Output output = new Output(
        new GZIPOutputStream(new FileOutputStream(file)));
    kryo.writeObject(output, model);
    output.flush();
    output.close();
}
 
Example 14
Source File: KryoSerializer.java    From javabase with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    Kryo kryo = new Kryo();
    SerializerBean serializerBean = new SerializerBean();
    serializerBean.setName("高广金");
    serializerBean.setAge(18);
    Output output = new Output(new FileOutputStream("/tmp/test.bin"));
    kryo.writeObject(output, serializerBean);
    output.flush();
    output.close();
    log.info("序列化前:{}", serializerBean.toString());
    SerializerBean deseriable = kryo.readObject(new Input(new FileInputStream("/tmp/test.bin")),
            SerializerBean.class);
    log.info("反序列化:{}", deseriable.toString());
}
 
Example 15
Source File: TestSiteToSiteClient.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerialization() {
    final SiteToSiteClientConfig clientConfig = new SiteToSiteClient.Builder()
            .url("http://localhost:8080/nifi")
            .portName("input")
            .buildConfig();

    final Kryo kryo = new Kryo();

    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final Output output = new Output(out);

    try {
        kryo.writeObject(output, clientConfig);
    } finally {
        output.close();
    }

    final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    final Input input = new Input(in);

    try {
        SiteToSiteClientConfig clientConfig2 = kryo.readObject(input, SiteToSiteClient.StandardSiteToSiteClientConfig.class);
        Assert.assertEquals(clientConfig.getUrls(), clientConfig2.getUrls());
    } finally {
        input.close();
    }
}
 
Example 16
Source File: KryoSerialization.java    From api-mining with GNU General Public License v3.0 5 votes vote down vote up
@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 17
Source File: KryoSerializer.java    From mobility-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] serialize(Object object) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Output output = new Output(baos);
    kryo.writeClassAndObject(output, object);
    output.flush();
    output.close();
    return baos.toByteArray();
}
 
Example 18
Source File: KryoSerialization.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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 19
Source File: FloatOperand.java    From ytk-mp4j with MIT License 5 votes vote down vote up
@Override
public void send(Output output, MetaData metaData) throws IOException, Mp4jException {
    Kryo kryo = KryoUtils.getKryo();
    try {
        switch (container) {
            case ARRAY:
                Serializer arrSerializer = new FloatOperand.Mp4jFloatArraySerializer(metaData.convertToArrayMetaData());
                if (compress) {
                    arrSerializer = new DeflateSerializer(arrSerializer);
                }
                arrSerializer.write(kryo, output, null);
                break;
            case MAP:
                Serializer mapSerializer = new FloatOperand.Mp4jFloatMapSerializer(metaData.convertToMapMetaData());
                if (compress) {
                    mapSerializer = new DeflateSerializer(mapSerializer);
                }
                mapSerializer.write(kryo, output, null);

                break;
            default:
                throw new Mp4jException("unsupported container:" + container);
        }
    } catch (Exception e) {
        LOG.error("send exception", e);
        throw new Mp4jException(e);
    } finally {
        output.close();
    }


}
 
Example 20
Source File: ZeroMQProxy.java    From gsn with GNU General Public License v3.0 4 votes vote down vote up
public ZeroMQProxy (final int portOUT,final int portMETA){
	kryo.register(DataField[].class);
	ctx = Main.getZmqContext();
	
	subscriberX = ctx.createSocket(ZMQ.XSUB);
    publisherX = ctx.createSocket(ZMQ.XPUB);
    publisherX.setXpubVerbose(true);
    publisherX.setHWM(0);
    subscriberX.setHWM(0);
    publisherX.bind("tcp://*:"+portOUT);
    
    clients = ctx.createSocket(ZMQ.REP);
    clients.bind ("tcp://*:"+portMETA);

    Thread dataProxy = new Thread(new Runnable(){

		@Override
		public void run() {
	           ZMQ.proxy(subscriberX, publisherX, null);
		}
    });
    dataProxy.setName("ZMQ-PROXY-Thread");
    dataProxy.start();
    
    Thread metaResponder =  new Thread(new Runnable(){
		@Override
		public void run() {
			while (true) {
				String request = clients.recvStr (0);
				String [] parts = request.split("\\?");
				if (parts.length > 1){
					try{
						long startTime = System.currentTimeMillis();
						if (parts.length > 2){
							startTime = Long.parseLong(parts[2]);
						}
						ZeroMQDeliverySync d = new ZeroMQDeliverySync(parts[0], parts[1]);
						final DefaultDistributionRequest  distributionReq = DefaultDistributionRequest.create(d, Mappings.getVSensorConfig(parts[0]), "select * from "+parts[0], startTime);
						logger.info("ZMQ request received: "+distributionReq.toString());
						DataDistributer.getInstance(d.getClass()).addListener(distributionReq);
					}catch (Exception e){
						logger.warn("ZMQ request parsing error: " + request, e);
					}
				}
				byte[] b=new byte[0];
				ByteArrayOutputStream bais = new ByteArrayOutputStream();
	            Output o = new Output(bais);
	            kryo.writeObjectOrNull(o,structures.get(parts[0]),DataField[].class);
	            o.close();
	            b = bais.toByteArray();
	            clients.send(b, 0);
			}
		}
	});
    metaResponder.setName("ZMQ-META-Thread");
       metaResponder.start();
}