T
- the compile-time managed typepublic abstract class Marshaler<T> extends Object
This class should not be created directly; an instance of it can be obtained
using MarshalQueryable.createMarshaler(android.hardware.camera2.utils.TypeReference<T>, int)
for the same type T
if the native type
mapping for T
supported
.
Modifier and Type | Field and Description |
---|---|
protected int |
mNativeType |
protected TypeReference<T> |
mTypeReference |
static int |
NATIVE_SIZE_DYNAMIC
Used to denote variable-length data structures.
|
Modifier | Constructor and Description |
---|---|
protected |
Marshaler(MarshalQueryable<T> query,
TypeReference<T> typeReference,
int nativeType)
Instantiate a marshaler between a single managed/native type combination.
|
Modifier and Type | Method and Description |
---|---|
int |
calculateMarshalSize(T value)
Get the size in bytes for how much space would be required to write this
value
into a byte buffer using the given nativeType . |
abstract int |
getNativeSize()
How many bytes a single instance of
T will take up if marshalled to/from
nativeType . |
int |
getNativeType()
The native type corresponding to this marshaler for the native side of this marshaler.
|
TypeReference<T> |
getTypeReference()
The type reference for
T for the managed type side of this marshaler. |
abstract void |
marshal(T value,
ByteBuffer buffer)
Marshal the specified object instance (value) into a byte buffer.
|
abstract T |
unmarshal(ByteBuffer buffer)
Unmarshal a new object instance from the byte buffer into its managed type.
|
protected final TypeReference<T> mTypeReference
protected final int mNativeType
public static int NATIVE_SIZE_DYNAMIC
If the size is dynamic then we can't know ahead of time how big of a data structure to preallocate for e.g. arrays, so one object must be unmarshaled at a time.
protected Marshaler(MarshalQueryable<T> query, TypeReference<T> typeReference, int nativeType)
This particular managed/native type combination must be supported by
#isTypeMappingSupported
.
query
- an instance of MarshalQueryable
typeReference
- the managed type reference
Must be one for which #isTypeMappingSupported
returns true
nativeType
- the native type, e.g.
TYPE_BYTE
.
Must be one for which #isTypeMappingSupported
returns true
.NullPointerException
- if any args were null
UnsupportedOperationException
- if the type mapping was not supportedpublic abstract void marshal(T value, ByteBuffer buffer)
Upon completion, the Buffer.position()
will have advanced by
the marshal size
of value
.
value
- the value of type T that we wish to write into the byte bufferbuffer
- the byte buffer into which the marshaled object will be writtenpublic int calculateMarshalSize(T value)
value
into a byte buffer using the given nativeType
.
If the size of this T
instance when serialized into a buffer is always constant,
then this method will always return the same value (and particularly, it will return
an equivalent value to getNativeSize()
.
Overriding this method is a must when the size is dynamic
.
value
- the value of type T that we wish to write into the byte bufferpublic abstract T unmarshal(ByteBuffer buffer)
Upon completion, the Buffer.position()
will have advanced by
the marshal size
of the returned T
instance.
buffer
- the byte buffer, from which we will read the objectpublic abstract int getNativeSize()
T
will take up if marshalled to/from
nativeType
.
When unmarshaling data from native to managed, the instance T
is not yet
available. If the native size is always a fixed mapping regardless of the instance of
T
(e.g. if the type is not a container of some sort), it can be used to preallocate
containers for T
to avoid resizing them.
In particular, the array marshaler takes advantage of this (when size is not dynamic)
to preallocate arrays of the right length when unmarshaling an array T[]
.
NATIVE_SIZE_DYNAMIC
if the size is dynamicpublic TypeReference<T> getTypeReference()
T
for the managed type side of this marshaler.public int getNativeType()