E
- the type of the object demarshalledpublic interface BytesReader<E> extends Serializable
Bytes
, mirroring the BytesWriter
, i. e. assuming the
length of the serialized form isn't written in the beginning of the serialized form itself,
but managed by ChronicleHash
implementation and passed to the reading methods.
Implementation example:
class LongPair { long first, second; }
enum LongPairArrayReader implements BytesReader<LongPair[]> {
INSTANCE;
@Override
public LongPair[] read(Bytes bytes, long size) {
return read(bytes, size, null);
}
@Override
public LongPair[] read(Bytes bytes, long size, LongPair[] toReuse) {
if (size > Integer.MAX_VALUE * 16L)
throw new IllegalStateException("LongPair[] size couldn't be " + (size / 16L));
int resLen = (int) (size / 16L);
LongPair[] res;
if (toReuse != null) {
if (toReuse.length == resLen) {
res = toReuse;
} else {
res = Arrays.copyOf(toReuse, resLen);
}
} else {
res = new LongPair[resLen];
}
for (int i = 0; i < resLen; i++) {
LongPair pair = res[i];
if (pair == null)
res[i] = pair = new LongPair();
pair.first = bytes.readLong();
pair.second = bytes.readLong();
}
return res;
}
}
BytesWriter
Modifier and Type | Method and Description |
---|---|
E |
read(Bytes bytes,
long size)
Reads and returns the object from
[position(), position() + size] bytes of the given
Bytes . |
E |
read(Bytes bytes,
long size,
E toReuse)
Similar to
read(Bytes, long) , but should attempt to reuse the given object, i. |
E read(Bytes bytes, long size)
[position(), position() + size]
bytes of the given
Bytes
.
Implementation of this method should increment the bytes
' position by the given
size, i. e. "consume" the bytes of the deserialized object. It must not alter the
bytes
' limit
and contents.
bytes
- the Bytes
to read the object fromsize
- the size of the serialized form of the returned objectBytesWriter.write(Bytes, Object)
,
read(Bytes, long, Object)
E read(Bytes bytes, long size, @Nullable E toReuse)
read(Bytes, long)
, but should attempt to reuse the given object, i. e.
to read the deserialized data into the given instance. If it is possible, this objects than
returned from this method. If it isn't possible for any reason, a new object should be
created and returned, just like in read(Bytes, long)
method. The given object could
be null
, in this case this method should behave exactly the same as
read(Bytes, long)
method does.bytes
- the Bytes
to read the object fromsize
- the size of the serialized form of the returned objecttoReuse
- the object to read the deserialized data intoread(Bytes, long)
Copyright © 2015. All rights reserved.