com.esotericsoftware.kryo.io.Output类的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(250)

本文整理了Java中com.esotericsoftware.kryo.io.Output类的一些代码示例,展示了Output类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Output类的具体详情如下:
包路径:com.esotericsoftware.kryo.io.Output
类名称:Output

Output介绍

[英]An OutputStream that buffers data in a byte array and optionally flushes to another OutputStream. Utility methods are provided for efficiently writing primitive types and strings. Encoding of integers: BIG_ENDIAN is used for storing fixed native size integer values LITTLE_ENDIAN is used for a variable length encoding of integer values
[中]在字节数组中缓冲数据并可选地刷新到另一个输出流的输出流。提供了实用方法来高效地编写基元类型和字符串。整数编码:BIG_ENDIAN用于存储固定的本机大小的整数值LITTLE_ENDIAN用于整数值的可变长度编码

代码示例

代码示例来源:origin: changmingxie/tcc-transaction

public byte[] execute(Kryo kryo) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    Output output = new Output(byteArrayOutputStream);
    kryo.writeClassAndObject(output, object);
    output.flush();
    return byteArrayOutputStream.toByteArray();
  }
});

代码示例来源:origin: apache/storm

@Override
public void write(Kryo kryo, Output output, Object object) {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  try {
    ObjectOutputStream oos = new ObjectOutputStream(bos);
    oos.writeObject(object);
    oos.flush();
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
  byte[] ser = bos.toByteArray();
  output.writeInt(ser.length);
  output.writeBytes(ser);
}

代码示例来源:origin: apache/hive

private static byte[] serializeObjectToKryo(Serializable object) {
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 Output output = new Output(baos);
 Kryo kryo = borrowKryo();
 try {
  kryo.writeObject(output, object);
 } finally {
  releaseKryo(kryo);
 }
 output.close();
 return baos.toByteArray();
}

代码示例来源:origin: apache/incubator-dubbo

@Override
public void writeBytes(byte[] v, int off, int len) throws IOException {
  if (v == null) {
    output.writeInt(-1);
  } else {
    output.writeInt(len);
    output.write(v, off, len);
  }
}

代码示例来源:origin: apache/storm

/**
 * Serializes the given object into a byte array using Kryo serialization.
 *
 * @param obj Object to be serialized.
 */
public byte[] serialize(Object obj) {
  output.clear();
  kryo.writeClassAndObject(output, obj);
  return output.toBytes();
}

代码示例来源:origin: alibaba/jstorm

@Override 
protected byte[] serialize(Object obj) { 
  output.clear(); 
  kryo.writeObject(output, obj);
  return output.toBytes(); 
}

代码示例来源:origin: apache/storm

private byte[] getKryoSerializedBytes (final Object obj) {
  final Kryo kryo = new Kryo();
  final ByteArrayOutputStream os = new ByteArrayOutputStream();
  final Output output = new Output(os);
  kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());
  kryo.writeClassAndObject(output, obj);
  output.flush();
  return os.toByteArray();
}

代码示例来源:origin: apache/hive

private void safeWriteToOutput(Output output,
                BaseProtocol.JobResult<?> jobResult) throws IOException {
 ByteArrayOutputStream boas = new ByteArrayOutputStream();
 ObjectOutputStream oos = new ObjectOutputStream(boas);
 oos.writeObject(jobResult);
 oos.flush();
 output.write(boas.toByteArray());
 output.flush();
}

代码示例来源:origin: apache/flink

checkKryoInitialized();
try {
  return kryo.copy(from);
  ByteArrayOutputStream baout = new ByteArrayOutputStream();
  Output output = new Output(baout);
  kryo.writeObject(output, from);
  output.close();
  ByteArrayInputStream bain = new ByteArrayInputStream(baout.toByteArray());
  Input input = new Input(bain);
  return (T)kryo.readObject(input, from.getClass());

代码示例来源:origin: uber/hudi

public static <T extends Serializable> T serializeDeserialize(T object, Class<T> clazz)
  throws IOException, ClassNotFoundException {
 // 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;
}

代码示例来源:origin: boonproject/boon

@Test
public void testKyro() {
  Employee employee = new Employee("Rick", "Hightower");
  Kryo kryo = new Kryo();
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  Output output = new Output(outputStream);
  kryo.writeClassAndObject(output, employee);
  output.close();
  final byte[] bytes = outputStream.toByteArray();
  ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
  Input input = new Input(inputStream);
  Object object = kryo.readClassAndObject(input);
  puts(object);
}

代码示例来源:origin: eu.stratosphere/stratosphere-testutil

protected void testKryoSerialization(final Object original) {
  final Kryo kryo = KryoUtil.getKryo();
  kryo.reset();
  final ByteArrayOutputStream baos = new ByteArrayOutputStream();
  final Output output = new Output(baos);
  kryo.writeClassAndObject(output, original);
  output.close();
  kryo.reset();
  final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
  final Object deserialized = kryo.readClassAndObject(new Input(bais));
  Assert.assertEquals(original, deserialized);
}

代码示例来源:origin: org.apache.apex/malhar-library

/**
 * Clone object by serializing and deserializing using Kryo.
 * Note this is different from using {@link Kryo#copy(Object)}, which will attempt to also clone transient fields.
 *
 * @param kryo kryo object used to clone objects
 * @param src src object that copy from
 * @return
 */
@SuppressWarnings("unchecked")
public static <SRC> SRC cloneObject(Kryo kryo, SRC src)
{
 kryo.setClassLoader(src.getClass().getClassLoader());
 ByteArrayOutputStream bos = null;
 Output output;
 Input input = null;
 try {
  bos = new ByteArrayOutputStream();
  output = new Output(bos);
  kryo.writeObject(output, src);
  output.close();
  input = new Input(bos.toByteArray());
  return (SRC)kryo.readObject(input, src.getClass());
 } finally {
  IOUtils.closeQuietly(input);
  IOUtils.closeQuietly(bos);
 }
}

代码示例来源:origin: stackoverflow.com

Kryo kryo = new Kryo();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Output output = new Output(bos);
kryo.writeObject(output, wrapped);
output.close();
stream.writeObject(bos.toByteArray());
Kryo kryo = new Kryo();
byte[] bytes = (byte[]) stream.readObject();
ByteArrayInputStream bis = new ByteInputStream(bytes, bytes.length);
Input input = new Input(bis);
wrapped = kryo.readObject(input, LazyMap.class);
input.close();

代码示例来源:origin: com.twitter/chill-java

protected SerDeState newInstance() {
  return new SerDeState(ki.newKryo(), new Input(), new Output(new ByteArrayOutputStream())) {
   /*
    * We have to take extra care of the ByteArrayOutputStream
    */
   @Override
   public void clear() {
    super.clear();
    ByteArrayOutputStream byteStream = (ByteArrayOutputStream)output.getOutputStream();
    byteStream.reset();
   }
   @Override
   public byte[] outputToBytes() {
    output.flush();
    ByteArrayOutputStream byteStream = (ByteArrayOutputStream)output.getOutputStream();
    return byteStream.toByteArray();
   }
   @Override
   public void writeOutputTo(OutputStream os) throws IOException {
    output.flush();
    ByteArrayOutputStream byteStream = (ByteArrayOutputStream)output.getOutputStream();
    byteStream.writeTo(os);
   }
  };
 }
};

代码示例来源:origin: yu199195/Raincat

@Override
public byte[] serialize(final Object obj) throws TransactionException {
  byte[] bytes;
  try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Output output = new Output(outputStream)) {
    //获取kryo对象
    Kryo kryo = new Kryo();
    kryo.writeObject(output, obj);
    bytes = output.toBytes();
    output.flush();
  } catch (IOException ex) {
    throw new TransactionException("kryo serialize error" + ex.getMessage());
  }
  return bytes;
}

代码示例来源:origin: qiujiayu/AutoLoadCache

@Override
public byte[] serialize(Object obj, int bufferSize) {
  Kryo kryo = pool.borrow();
  try (Output output = new Output(new ByteArrayOutputStream(), bufferSize)) {
    kryo.writeClassAndObject(output, obj);
    return output.toBytes();
  } finally {
    pool.release(kryo);
  }
}

代码示例来源:origin: apache/metron

/**
 * Ensure that the Profiler configuration can undergo Kryo serialization which
 * occurs when the Profiler is running in Storm.
 */
@Test
public void testKryoSerialization() throws Exception {
 // setup a profiler config to serialize
 ProfilerConfig expected = ProfilerConfig.fromJSON(profilesToSerialize);
 assertNotNull(expected);
 Kryo kryo = new Kryo();
 // serialize
 ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
 Output output = new Output(byteStream);
 kryo.writeObject(output, expected);
 // validate serialization
 byte[] bits = output.toBytes();
 assertNotNull(bits);
 // deserialize
 Input input = new Input(new ByteArrayInputStream(bits));
 ProfilerConfig actual = kryo.readObject(input, ProfilerConfig.class);
 // validate deserialization
 assertNotNull(actual);
 assertEquals(expected, actual);
}

代码示例来源:origin: techery/snapper

public KryoConverter(Class<T> clazz) {
  this.clazz = clazz;
  this.kryo = new Kryo();
  this.kryo.register(clazz);
  this.kryo.register(Date.class, new DateSerializer());
  this.kryo.setDefaultSerializer(CompatibleFieldSerializer.class);
  this.kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
  //
  this.input = new Input();
  this.output = new Output(new ByteArrayOutputStream());
}

代码示例来源:origin: apache/ignite

@Override public void apply(TestObject obj) {
    out.reset();
    Output kryoOut = null;
    try {
      kryoOut = new Output(out);
      kryo.writeObject(kryoOut, obj);
    }
    finally {
      U.close(kryoOut, log);
    }
  }
};

相关文章