@Override public SerializationResult setNextBuffer(Buffer buffer) throws IOException { this.targetBuffer = buffer; this.position = 0; this.limit = buffer.getSize(); if (this.lengthBuffer.hasRemaining()) { copyToTargetBufferFrom(this.lengthBuffer); } if (this.dataBuffer.hasRemaining()) { copyToTargetBufferFrom(this.dataBuffer); } SerializationResult result = getSerializationResult(); // make sure we don't hold onto the large buffers for too long if (result.isFullRecord()) { this.serializationBuffer.clear(); this.serializationBuffer.pruneBuffer(); this.dataBuffer = this.serializationBuffer.wrapAsByteBuffer(); } return result; }
/** * 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(); }