下面列出了怎么用com.esotericsoftware.kryo.io.OutputChunked的API类实例代码及写法,或者点击链接到github查看源代码。
KryoState getOrCreate(KryoCoder<?> coder) {
return kryoStateMap
.get()
.computeIfAbsent(
coder.getInstanceId(),
k -> {
final Kryo kryo = new Kryo();
// fallback in case serialized class does not have default constructor
kryo.setInstantiatorStrategy(
new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
kryo.setReferences(coder.getOptions().getReferences());
kryo.setRegistrationRequired(coder.getOptions().getRegistrationRequired());
kryo.setClassLoader(Thread.currentThread().getContextClassLoader());
// first id of user provided class registration
final int firstRegistrationId = kryo.getNextRegistrationId();
// register user provided classes
for (KryoRegistrar registrar : coder.getRegistrars()) {
registrar.registerClasses(kryo);
}
return new KryoState(
kryo,
firstRegistrationId,
new InputChunked(coder.getOptions().getBufferSize()),
new OutputChunked(coder.getOptions().getBufferSize()));
});
}
public void write(OutputStream out, T object) {
KryoContext kryoContext = KRYOS.get();
OutputChunked output = kryoContext.getOutputChunked();
output.setOutputStream(out);
writeObject(kryoContext.getKryo(), output, object);
output.endChunks();
output.flush();
}
private KryoState(
Kryo kryo, int firstRegistrationId, InputChunked inputChunked, OutputChunked outputChunked) {
this.kryo = kryo;
this.firstRegistrationId = firstRegistrationId;
this.inputChunked = inputChunked;
this.outputChunked = outputChunked;
}
@Test
public void test() throws SchemaException {
final Kryo kryo = new Kryo();
kryo.register(SimpleFeatureImpl.class, new FeatureSerializer());
final SimpleFeatureType schema =
DataUtilities.createType("testGeo", "location:Point:srid=4326,name:String");
final List<AttributeDescriptor> descriptors = schema.getAttributeDescriptors();
final Object[] defaults = new Object[descriptors.size()];
int p = 0;
for (final AttributeDescriptor descriptor : descriptors) {
defaults[p++] = descriptor.getDefaultValue();
}
final SimpleFeature feature =
SimpleFeatureBuilder.build(schema, defaults, UUID.randomUUID().toString());
final GeometryFactory geoFactory = new GeometryFactory();
feature.setAttribute("location", geoFactory.createPoint(new Coordinate(-45, 45)));
final Output output = new OutputChunked();
kryo.getSerializer(SimpleFeatureImpl.class).write(kryo, output, feature);
final Input input = new InputChunked();
input.setBuffer(output.getBuffer());
final SimpleFeature f2 =
(SimpleFeature) kryo.getSerializer(SimpleFeatureImpl.class).read(
kryo,
input,
SimpleFeatureImpl.class);
assertEquals(feature, f2);
}
protected KryoContext initialValue() {
Kryo kryo = newKryoInstance();
OutputChunked output = new OutputChunked(BUFFER_SIZE);
InputChunked input = new InputChunked(BUFFER_SIZE);
return new KryoContext(kryo, input, output);
}
KryoContext(Kryo kryo, InputChunked inputChunked, OutputChunked outputChunked) {
this.kryo = kryo;
this.inputChunked = inputChunked;
this.outputChunked = outputChunked;
}
public OutputChunked getOutputChunked() {
return outputChunked;
}
/**
* {@link KryoState#outputChunked}.
*
* @return output buffer
*/
OutputChunked getOutputChunked() {
return outputChunked;
}