Java Code Examples for com.esotericsoftware.kryo.pool.KryoPool

The following are top voted examples for showing how to use com.esotericsoftware.kryo.pool.KryoPool. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to product more good examples. 

+ Save this class to your library

Example 1

Project: ecgine   File: SerializationUtil.java View source code7 votesvote downvote up
public static byte[] kryoSerialize(Object obj) throws Exception {
	Output output = null;
	try {
		KryoPool pool = getKryoPool();
		Kryo kryo = pool.borrow();
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		output = new Output(bos);
		if (obj instanceof ARefType) {
			kryo.writeObject(output, obj,
					new FieldSerializer(kryo, obj.getClass()));
		} else {
			kryo.writeClassAndObject(output, obj);
		}
		output.flush();
		byte[] data = bos.toByteArray();
		output.close();
		pool.release(kryo);
		// Serializable last = deserialize(data);
		return data;
	} catch (Exception e) {
		throw e;
	} finally {
		output.close();
	}
}

Example 2

Project: reactor   File: KryoPoolCodec.java View source code6 votesvote downvote up
@Override
protected Function<byte[], IN> deserializer(final KryoPool engine,
		final Class<IN> type) {
	return new Function<byte[], IN>() {
		@Override
		public IN apply(byte[] bytes) {
			final Kryo kryo = engine.borrow();
			try {
				return kryo.readObject(new UnsafeMemoryInput(bytes), type);
			}
			finally {
				engine.release(kryo);
			}
		}
	};
}

Example 3

Project: reactor   File: KryoPoolCodec.java View source code6 votesvote downvote up
@Override
protected Function<OUT, byte[]> serializer(final KryoPool engine) {
	return new Function<OUT, byte[]>() {
		@Override
		public byte[] apply(OUT o) {
			final Kryo kryo = engine.borrow();
			try {
				UnsafeMemoryOutput out =
						new UnsafeMemoryOutput(Buffer.SMALL_BUFFER_SIZE, Buffer.MAX_BUFFER_SIZE);
				kryo.writeObject(out, o);
				out.flush();
				return out.toBytes();
			}
			finally {
				engine.release(kryo);
			}
		}
	};
}

Example 4

Project: ecgine   File: SerializationUtil.java View source code6 votesvote downvote up
public static KryoSerializable kryoDeserialize(InputStream bis)
		throws Exception {
	if (bis.available() <= 0) {
		return null;
	}
	try {
		KryoPool pool = getKryoPool();
		Kryo kryo = pool.borrow();
		Input input = new Input(bis);
		KryoSerializable opt = (KryoSerializable) kryo
				.readClassAndObject(input);
		input.close();
		pool.release(kryo);
		return opt;
	} finally {
		bis.close();
	}
}

Example 5

Project: ratpack-examples   File: KryoValueSerializer.java View source code6 votesvote downvote up
public ByteBuf serialize(Registry registry, ByteBufAllocator bufAllocator, Object value) throws Exception {
  Objects.requireNonNull(value);
  KryoPool kryoPool = registry.get(KryoPool.class);
  Kryo kryo = kryoPool.borrow();
  try {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    Output output = new Output(stream);
    kryo.writeClassAndObject(output, value);
    output.close();
    byte[] bytes = stream.toByteArray();
    String encoded = ENCODER.encodeToString(bytes);
    return ByteBufUtil.encodeString(bufAllocator, CharBuffer.wrap(encoded), CharsetUtil.UTF_8);
  } catch (Exception ex) {
    throw ex;
  } finally {
    kryoPool.release(kryo);
  }
}

Example 6

Project: ratpack-examples   File: KryoValueSerializer.java View source code6 votesvote downvote up
public Object deserialize(Registry registry, String value) throws Exception {
  if (value == null || value.isEmpty()) {
    return null;
  }
  KryoPool kryoPool = registry.get(KryoPool.class);
  Kryo kryo = kryoPool.borrow();
  try {
    byte[] bytes = DECODER.decode(value);
    ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
    Input input = new Input(stream);
    Object obj = kryo.readClassAndObject(input);
    input.close();
    return obj;
  } catch (Exception ex) {
    throw ex;
  } finally {
    kryoPool.release(kryo);
  }
}

 

 

 

https://www.programcreek.com/java-api-examples/index.php?api=com.esotericsoftware.kryo.pool.KryoPool

转载于:https://my.oschina.net/xiaominmin/blog/1585377

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值