mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
HCOLL-136 HCOLL-137 Improved BytesReader, BytesWriter and BytesInterop docs [dc]
This commit is contained in:
@@ -18,13 +18,66 @@
|
||||
|
||||
package net.openhft.chronicle.hash.serialization;
|
||||
|
||||
import net.openhft.chronicle.hash.ChronicleHash;
|
||||
import net.openhft.lang.io.Bytes;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Serializer (object to {@link Bytes} writer) which 1) knows the length of serialized form
|
||||
* of any object before actual serialization 2) doesn't include that length in the serialized
|
||||
* form itself, assuming it will be passed by the {@link ChronicleHash} into
|
||||
* {@link BytesReader#read(Bytes, long)} deserialization method.
|
||||
*
|
||||
* <p>Implementation example: <pre><code>
|
||||
* class LongPair { long first, second; }
|
||||
*
|
||||
* enum LongPairArrayWriter implements BytesWriter<LongPair[]> {
|
||||
* INSTANCE;
|
||||
*
|
||||
* @Override
|
||||
* public long size(LongPair[] longPairs) {
|
||||
* return longPairs.length * 16L;
|
||||
* }
|
||||
*
|
||||
* @Override
|
||||
* public void write(Bytes bytes, LongPair[] longPairs) {
|
||||
* for (LongPair pair : longPairs) {
|
||||
* bytes.writeLong(pair.first);
|
||||
* bytes.writeLong(pair.second);
|
||||
* }
|
||||
* }
|
||||
* }</code></pre>
|
||||
*
|
||||
* @param <E> the type of the object marshalled
|
||||
* @see BytesReader
|
||||
* @see BytesInterop
|
||||
*/
|
||||
public interface BytesWriter<E> extends Serializable {
|
||||
|
||||
/**
|
||||
* Returns the length (in bytes) of the serialized form of the given object. Serialization form
|
||||
* in terms of this interface, i. e. how much bytes are written to {@code bytes} on
|
||||
* {@link #write(Bytes, Object) write(bytes, e)} call.
|
||||
*
|
||||
* @param e the object which serialized form length should be returned
|
||||
* @return the length (in bytes) of the serialized form of the given object
|
||||
*/
|
||||
long size(E e);
|
||||
|
||||
/**
|
||||
* Serializes the given object to the given {@code bytes}, without writing the length of the
|
||||
* serialized form itself.
|
||||
*
|
||||
* <p>Implementation of this method should increment the {@code bytes}' {@linkplain
|
||||
* Bytes#position() position} by {@link #size(Object) size(e)}. The given object should
|
||||
* be written into these range between the initial {@code bytes}' position and the position
|
||||
* after this method call returns. Implementation of this method must not alter the
|
||||
* {@code bytes}' {@linkplain Bytes#limit() limit} and contents outside of the specified range.
|
||||
*
|
||||
* @param bytes the {@code Bytes} to write the given object to
|
||||
* @param e the object to serialize to the given bytes
|
||||
* @see BytesReader#read(Bytes, long)
|
||||
*/
|
||||
void write(Bytes bytes, E e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user