com.esotericsoftware.kryo.io.Output.position()方法的使用及代码示例

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

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

Output.position介绍

[英]Returns the current position in the buffer. This is the number of bytes that have not been flushed.
[中]返回缓冲区中的当前位置。这是尚未刷新的字节数。

代码示例

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

/**
 * Serializes the given object into a {@link ByteBuffer} backed by the byte array returned by Kryo serialization.
 *
 * @param obj Object to be serialized.
 */
public ByteBuffer serializeToByteBuffer(Object obj) {
  output.clear();
  kryo.writeClassAndObject(output, obj);
  return ByteBuffer.wrap(output.getBuffer(), 0, output.position());
}

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

if (output.position() != 0) {
  throw new IllegalStateException("The Kryo Output still contains data from a previous " +
    "serialize call. It has to be flushed or cleared at the end of the serialize call.");

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

public void writeOutputTo(OutputStream os) throws IOException {
 os.write(output.getBuffer(), 0, output.position());
}

代码示例来源:origin: vmware/xenon

/**
 * @see #serializeObject(Object, byte[], int)
 */
public static ByteBuffer serializeObject(Object o, int maxSize) {
  Kryo k = getKryoThreadLocalForObjects();
  Output out = new Output(DEFAULT_BUFFER_SIZE_BYTES, maxSize);
  k.writeClassAndObject(out, o);
  return ByteBuffer.wrap(out.getBuffer(), 0, out.position());
}

代码示例来源:origin: org.wicketstuff/wicketstuff-serializer-kryo2

private void after(Output output, Object object)
{
  parent.serializingListener().after(output.position(), object);
}

代码示例来源:origin: org.wicketstuff/wicketstuff-serializer-kryo2

private void before(Output output, Object object)
{
  parent.serializingListener().before(output.position(), object);
}

代码示例来源:origin: org.hawkular.titan/titan-core

private void writeOutput(WriteBuffer out, Output output) {
  byte[] array = output.getBuffer();
  int limit = output.position();
  for (int i=0;i<limit;i++) out.putByte(array[i]);
}

代码示例来源:origin: com.simiacryptus/java-util

/**
 * Write kryo.
 *
 * @param <T>  the type parameter
 * @param obj  the obj
 * @param file the file
 */
public static <T> void writeKryo(T obj, OutputStream file) {
 try {
  Output output = new Output(buffer.get());
  new KryoReflectionFactorySupport().writeClassAndObject(output, obj);
  output.close();
  IOUtils.write(Arrays.copyOf(output.getBuffer(), output.position()), file);
  file.close();
 } catch (IOException e) {
  throw new RuntimeException(e);
 }
}

代码示例来源:origin: org.apache.storm/storm-core

/**
 * Serializes the given object into a {@link ByteBuffer} backed by the byte array returned by Kryo serialization.
 *
 * @param obj Object to be serialized.
 */
public ByteBuffer serializeToByteBuffer(Object obj) {
  output.clear();
  kryo.writeClassAndObject(output, obj);
  return ByteBuffer.wrap(output.getBuffer(), 0, output.position());
}

代码示例来源:origin: vmware/xenon

/**
 * Serializes an arbitrary object into a binary representation, using full
 * reference tracking and the object graph serializer.
 * Must be paired with {@code KryoSerializers#fromBytes(byte[], int, int)} or
 * {@code KryoSerializers#fromBytes(byte[])}
 */
public static int serializeObject(Object o, byte[] buffer, int position) {
  Kryo k = getKryoThreadLocalForObjects();
  Output out = new Output(buffer, buffer.length);
  out.setPosition(position);
  k.writeClassAndObject(out, o);
  return out.position();
}

代码示例来源:origin: com.vmware.dcp/dcp-common

public static int toBytes(Object o, byte[] buffer, int position) {
  Kryo k = kryoForObjectPerThread.get();
  Output out = new Output(buffer);
  out.setPosition(position);
  k.writeClassAndObject(out, o);
  return out.position();
}

代码示例来源:origin: com.vmware.dcp/dcp-common

public static int toBytes(ServiceDocument o, byte[] buffer, int position) {
  Kryo k = kryoForDocumentPerThread.get();
  Output out = new Output(buffer);
  out.setPosition(position);
  k.writeClassAndObject(out, o);
  return out.position();
}

代码示例来源:origin: us.ihmc/IHMCCommunication

/**
* Blocking write to an outputStream
* 
* @param outputStream output stream to write to
* @param object object to write
* 
* @return Number of bytes written
* 
* @throws IOException
*/
public int write(OutputStream outputStream, Object object) throws IOException
{
 output.clear();
 output.setPosition(4);
 kryo.writeClassAndObject(output, object);
 int length = output.position() - 4;
 writeIntToWriteBuffer(length);
 outputStream.write(writeBuffer, 0, output.position());
 return length + 4;
}

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

@Override
public Slice toByteArray(T o)
{
 final Output output = new Output(32, -1);
 try {
  kryo.writeClassAndObject(output, o);
 } finally {
  output.close();
 }
 return new Slice(output.getBuffer(), 0, output.position());
}

代码示例来源:origin: co.paralleluniverse/quasar-core

@Override
public void write(Kryo kryo, Output output, ThreadLocal<?> tl) {
  output.writeBoolean(tl instanceof InheritableThreadLocal);
  final Object val = tl.get();
  final int pos = output.position();
  try {
    kryo.writeClassAndObject(output, val);
  } catch (RuntimeException e) {
    if (PRINT_WARNINGS_ON_UNSERIALIZABLE_THREAD_LOCAL)
      System.err.println("WARNING: Cannot serialize ThreadLocal (" + tl + " = " + val + "), it will be restored as null.");
    output.setPosition(pos);
    kryo.writeObject(output, new DEFAULT());
  }
}

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

@Override
public DataStatePair toDataStatePair(T o)
{
 data.setPosition(0);
 writeClassAndObject(data, o);
 if (!pairs.isEmpty()) {
  state.setPosition(0);
  for (ClassIdPair cip : pairs) {
   writeClassAndObject(state, cip);
  }
  pairs.clear();
  dataStatePair.state = new Slice(state.getBuffer(), 0, state.position());
 } else {
  dataStatePair.state = null;
 }
 dataStatePair.data = new Slice(data.getBuffer(), 0, data.position());
 return dataStatePair;
}

代码示例来源:origin: net.dempsy/dempsy-serialization.kryo

@Override
public <T> void serialize(final T object, final MessageBufferOutput buffer) throws IOException {
  try (Holder k = getKryoHolder()) {
    final Output output = k.output;
    // this will allow kryo to grow the buffer as needed.
    output.setBuffer(buffer.getBuffer(), Integer.MAX_VALUE);
    output.setPosition(buffer.getPosition()); // set the position to where we already are.
    kryoRunner.doSerialize(k, output, object);
    // if we resized then we need to adjust the message buffer
    if (output.getBuffer() != buffer.getBuffer())
      buffer.replace(output.getBuffer());
    buffer.setPosition(output.position());
  } catch (final KryoException ke) {
    throw new IOException("Failed to serialize.", ke);
  } catch (final IllegalArgumentException e) { // this happens when requiring registration but serializing an unregistered class
    throw new IOException("Failed to serialize " + objectDescription(object) +
        " (did you require registration and attempt to serialize an unregistered class?)", e);
  }
}

代码示例来源:origin: vmware/xenon

public QueryValidationServiceState serializedAndCompareDocuments(
    boolean useBinary, QueryValidationServiceState original)
    throws Throwable {
  QueryValidationServiceState originalDeserializedWithSig = null;
  if (useBinary) {
    Output o = KryoSerializers.serializeDocument(original, 4096);
    originalDeserializedWithSig = (QueryValidationServiceState) KryoSerializers
        .deserializeDocument(
            o.getBuffer(),
            0, o.position());
  } else {
    String serializedDocument = Utils.toJson(original);
    originalDeserializedWithSig = Utils.fromJson(
        serializedDocument,
        QueryValidationServiceState.class);
  }
  compareDocumentFields(original, originalDeserializedWithSig);
  return originalDeserializedWithSig;
}

代码示例来源:origin: vmware/xenon

@Test
public void serializeDocumentForIndexing() {
  ExampleServiceState state = new ExampleServiceState();
  state.documentSelfLink = "selfLink";
  state.documentKind = Utils.buildKind(ExampleServiceState.class);
  Output o = KryoSerializers.serializeDocumentForIndexing(state, 2048);
  ExampleServiceState deser = (ExampleServiceState) KryoSerializers.deserializeDocument(
      o.getBuffer(), 0,
      o.position());
  assertNull(deser.documentSelfLink);
  assertNull(deser.documentKind);
}

代码示例来源:origin: com.vmware.xenon/xenon-common

@Test
public void serializeDocumentForIndexing() {
  ExampleServiceState state = new ExampleServiceState();
  state.documentSelfLink = "selfLink";
  state.documentKind = Utils.buildKind(ExampleServiceState.class);
  Output o = KryoSerializers.serializeDocumentForIndexing(state, 2048);
  ExampleServiceState deser = (ExampleServiceState) KryoSerializers.deserializeDocument(
      o.getBuffer(), 0,
      o.position());
  assertNull(deser.documentSelfLink);
  assertNull(deser.documentKind);
}

相关文章