/** * Clones the given writable using the {@link IOReadableWritable serialization}. * * @param original Object to clone * @param <T> Type of the object to clone * @return Cloned object * @throws IOException Thrown is the serialization fails. */ public static <T extends IOReadableWritable> T createCopyWritable(T original) throws IOException { if (original == null) { return null; } final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (DataOutputViewStreamWrapper out = new DataOutputViewStreamWrapper(baos)) { original.write(out); } final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); try (DataInputViewStreamWrapper in = new DataInputViewStreamWrapper(bais)) { @SuppressWarnings("unchecked") T copy = (T) instantiate(original.getClass()); copy.read(in); return copy; } }
@Override protected void serialize(T record, DataOutputView dataOutputView) throws IOException { record.write(dataOutputView); } }
@Override protected T deserialize(T reuse, DataInputView dataInput) throws IOException { reuse.read(dataInput); return reuse; } }
@Override protected void serialize(T record, DataOutputView dataOutputView) throws IOException { record.write(dataOutputView); } }
@Override protected T deserialize(T reuse, DataInputView dataInput) throws IOException { reuse.read(dataInput); return reuse; } }
/** * Clones the given writable using the {@link IOReadableWritable serialization}. * * @param original Object to clone * @param <T> Type of the object to clone * @return Cloned object * @throws IOException Thrown is the serialization fails. */ public static <T extends IOReadableWritable> T createCopyWritable(T original) throws IOException { if (original == null) { return null; } final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (DataOutputViewStreamWrapper out = new DataOutputViewStreamWrapper(baos)) { original.write(out); } final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); try (DataInputViewStreamWrapper in = new DataInputViewStreamWrapper(bais)) { @SuppressWarnings("unchecked") T copy = (T) instantiate(original.getClass()); copy.read(in); return copy; } }
@Override protected void serialize(T record, DataOutputView dataOutputView) throws IOException { record.write(dataOutputView); } }
@Override protected T deserialize(T reuse, DataInputView dataInput) throws IOException { reuse.read(dataInput); return reuse; } }
/** * Clones the given writable using the {@link IOReadableWritable serialization}. * * @param original Object to clone * @param <T> Type of the object to clone * @return Cloned object * @throws IOException Thrown is the serialization fails. */ public static <T extends IOReadableWritable> T createCopyWritable(T original) throws IOException { if (original == null) { return null; } final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (DataOutputViewStreamWrapper out = new DataOutputViewStreamWrapper(baos)) { original.write(out); } final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); try (DataInputViewStreamWrapper in = new DataInputViewStreamWrapper(bais)) { @SuppressWarnings("unchecked") T copy = (T) instantiate(original.getClass()); copy.read(in); return copy; } }
/** * Serializes the complete record to an intermediate data serialization buffer. * * @param record the record to serialize */ @Override public void serializeRecord(T record) throws IOException { if (CHECKED) { if (dataBuffer.hasRemaining()) { throw new IllegalStateException("Pending serialization of previous record."); } } serializationBuffer.clear(); lengthBuffer.clear(); // write data and length record.write(serializationBuffer); int len = serializationBuffer.length(); lengthBuffer.putInt(0, len); dataBuffer = serializationBuffer.wrapAsByteBuffer(); }
/** * Serializes the complete record to an intermediate data serialization buffer. * * @param record the record to serialize */ @Override public void serializeRecord(T record) throws IOException { if (CHECKED) { if (dataBuffer.hasRemaining()) { throw new IllegalStateException("Pending serialization of previous record."); } } serializationBuffer.clear(); lengthBuffer.clear(); // write data and length record.write(serializationBuffer); int len = serializationBuffer.length(); lengthBuffer.putInt(0, len); dataBuffer = serializationBuffer.wrapAsByteBuffer(); }
/** * Serializes the complete record to an intermediate data serialization * buffer and starts copying it to the target buffer (if available). * * @param record the record to serialize * @return how much information was written to the target buffer and * whether this buffer is full * @throws IOException */ @Override public SerializationResult addRecord(T record) throws IOException { if (CHECKED) { if (this.dataBuffer.hasRemaining()) { throw new IllegalStateException("Pending serialization of previous record."); } } this.serializationBuffer.clear(); this.lengthBuffer.clear(); // write data and length record.write(this.serializationBuffer); int len = this.serializationBuffer.length(); this.lengthBuffer.putInt(0, len); this.dataBuffer = this.serializationBuffer.wrapAsByteBuffer(); // Copy from intermediate buffers to current target memory segment copyToTargetBufferFrom(this.lengthBuffer); copyToTargetBufferFrom(this.dataBuffer); return getSerializationResult(); }
/** * Serializes the complete record to an intermediate data serialization buffer. * * @param record the record to serialize */ @Override public void serializeRecord(T record) throws IOException { // Reserve 4 bytes for the length. int prevPosition = serializationBuffer.position(); serializationBuffer.skipBytesToWrite(4); record.write(serializationBuffer); // Write length field. int newPosition = serializationBuffer.position(); serializationBuffer.position(prevPosition); serializationBuffer.writeInt(newPosition - prevPosition - 4); serializationBuffer.position(newPosition); }
/** * Serializes the complete record to an intermediate data serialization buffer. * * @param record the record to serialize */ @Override public void serializeRecord(T record) throws IOException { if (CHECKED) { if (dataBuffer.hasRemaining()) { throw new IllegalStateException("Pending serialization of previous record."); } } serializationBuffer.clear(); serializationBuffer.position(4); // write data and length record.write(serializationBuffer); int len = serializationBuffer.length(); serializationBuffer.position(0); serializationBuffer.writeInt(len - 4); serializationBuffer.position(len); dataBuffer = serializationBuffer.wrapAsByteBuffer(); }