Class ByteArrayCoder
- java.lang.Object
-
- org.apache.beam.sdk.coders.Coder<T>
-
- org.apache.beam.sdk.coders.StructuredCoder<T>
-
- org.apache.beam.sdk.coders.AtomicCoder<byte[]>
-
- org.apache.beam.sdk.coders.ByteArrayCoder
-
- All Implemented Interfaces:
java.io.Serializable
public class ByteArrayCoder extends AtomicCoder<byte[]>
ACoder
forbyte[]
.The encoding format is as follows:
- If in a non-nested context (the
byte[]
is the only value in the stream), the bytes are read/written directly. - If in a nested context, the bytes are prefixed with the length of the array, encoded via a
VarIntCoder
.
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.apache.beam.sdk.coders.Coder
Coder.Context, Coder.NonDeterministicException
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]
decode(java.io.InputStream inStream)
Decodes a value of typeT
from the given input stream in the given context.byte[]
decode(java.io.InputStream inStream, Coder.Context context)
Decodes a value of typeT
from the given input stream in the given context.void
encode(byte[] value, java.io.OutputStream outStream)
Encodes the given value of typeT
onto the given output stream.void
encode(byte[] value, java.io.OutputStream outStream, Coder.Context context)
Encodes the given value of typeT
onto the given output stream in the given context.void
encodeAndOwn(byte[] value, java.io.OutputStream outStream, Coder.Context context)
Encodes the providedvalue
with the identical encoding toencode(byte[], java.io.OutputStream)
, but with optimizations that take ownership of the value.protected long
getEncodedElementByteSize(byte[] value)
Returns the size in bytes of the encoded value using this coder.TypeDescriptor<byte[]>
getEncodedTypeDescriptor()
Returns theTypeDescriptor
for the type encoded.boolean
isRegisterByteSizeObserverCheap(byte[] value)
Returns whetherCoder.registerByteSizeObserver(T, org.apache.beam.sdk.util.common.ElementByteSizeObserver)
cheap enough to call for every element, that is, if thisCoder
can calculate the byte size of the element to be coded in roughly constant time (or lazily).static ByteArrayCoder
of()
java.lang.Object
structuralValue(byte[] value)
Returns an object with anObject.equals()
method that represents structural equality on the argument.void
verifyDeterministic()
ThrowCoder.NonDeterministicException
if the coding is not deterministic.-
Methods inherited from class org.apache.beam.sdk.coders.AtomicCoder
equals, getCoderArguments, getComponents, hashCode
-
Methods inherited from class org.apache.beam.sdk.coders.StructuredCoder
toString
-
Methods inherited from class org.apache.beam.sdk.coders.Coder
consistentWithEquals, registerByteSizeObserver, verifyDeterministic, verifyDeterministic
-
-
-
-
Method Detail
-
of
public static ByteArrayCoder of()
-
encode
public void encode(byte[] value, java.io.OutputStream outStream) throws java.io.IOException, CoderException
Description copied from class:Coder
Encodes the given value of typeT
onto the given output stream.- Specified by:
encode
in classCoder<byte[]>
- Throws:
java.io.IOException
- if writing to theOutputStream
fails for some reasonCoderException
- if the value could not be encoded for some reason
-
encode
public void encode(byte[] value, java.io.OutputStream outStream, Coder.Context context) throws java.io.IOException, CoderException
Description copied from class:Coder
Encodes the given value of typeT
onto the given output stream in the given context.- Overrides:
encode
in classCoder<byte[]>
- Throws:
java.io.IOException
- if writing to theOutputStream
fails for some reasonCoderException
- if the value could not be encoded for some reason
-
encodeAndOwn
public void encodeAndOwn(byte[] value, java.io.OutputStream outStream, Coder.Context context) throws java.io.IOException, CoderException
Encodes the providedvalue
with the identical encoding toencode(byte[], java.io.OutputStream)
, but with optimizations that take ownership of the value.Once passed to this method,
value
should never be observed or mutated again.- Throws:
java.io.IOException
CoderException
-
decode
public byte[] decode(java.io.InputStream inStream) throws java.io.IOException, CoderException
Description copied from class:Coder
Decodes a value of typeT
from the given input stream in the given context. Returns the decoded value.- Specified by:
decode
in classCoder<byte[]>
- Throws:
java.io.IOException
- if reading from theInputStream
fails for some reasonCoderException
- if the value could not be decoded for some reason
-
decode
public byte[] decode(java.io.InputStream inStream, Coder.Context context) throws java.io.IOException, CoderException
Description copied from class:Coder
Decodes a value of typeT
from the given input stream in the given context. Returns the decoded value.- Overrides:
decode
in classCoder<byte[]>
- Throws:
java.io.IOException
- if reading from theInputStream
fails for some reasonCoderException
- if the value could not be decoded for some reason
-
verifyDeterministic
public void verifyDeterministic()
Description copied from class:AtomicCoder
ThrowCoder.NonDeterministicException
if the coding is not deterministic.In order for a
Coder
to be considered deterministic, the following must be true:- two values that compare as equal (via
Object.equals()
orComparable.compareTo()
, if supported) have the same encoding. - the
Coder
always produces a canonical encoding, which is the same for an instance of an object even if produced on different computers at different times.
Unless overridden, does not throw. An
AtomicCoder
is presumed to be deterministic- Overrides:
verifyDeterministic
in classAtomicCoder<byte[]>
- two values that compare as equal (via
-
structuralValue
public java.lang.Object structuralValue(byte[] value)
Returns an object with anObject.equals()
method that represents structural equality on the argument.For any two values
x
andy
of typeT
, if their encoded bytes are the same, then it must be the case thatstructuralValue(x).equals(structuralValue(y))
.Most notably:
- The structural value for an array coder should perform a structural comparison of the contents of the arrays, rather than the default behavior of comparing according to object identity.
- The structural value for a coder accepting
null
should be a proper object with anequals()
method, even if the input value isnull
.
See also
Coder.consistentWithEquals()
.By default, if this coder is
Coder.consistentWithEquals()
, and the value is not null, returns the provided object. Otherwise, encodes the value into abyte[]
, and returns an object that performs array equality on the encoded bytes.- Overrides:
structuralValue
in classCoder<byte[]>
- Returns:
- objects that are equal if the two arrays contain the same bytes.
-
isRegisterByteSizeObserverCheap
public boolean isRegisterByteSizeObserverCheap(byte[] value)
Returns whetherCoder.registerByteSizeObserver(T, org.apache.beam.sdk.util.common.ElementByteSizeObserver)
cheap enough to call for every element, that is, if thisCoder
can calculate the byte size of the element to be coded in roughly constant time (or lazily).Not intended to be called by user code, but instead by
PipelineRunner
implementations.By default, returns false. The default
Coder.registerByteSizeObserver(T, org.apache.beam.sdk.util.common.ElementByteSizeObserver)
implementation invokesCoder.getEncodedElementByteSize(T)
which requires re-encoding an element unless it is overridden. This is considered expensive.- Overrides:
isRegisterByteSizeObserverCheap
in classCoder<byte[]>
- Returns:
true
sincegetEncodedElementByteSize(byte[])
runs in constant time using thelength
of the provided array.
-
getEncodedTypeDescriptor
public TypeDescriptor<byte[]> getEncodedTypeDescriptor()
Description copied from class:Coder
Returns theTypeDescriptor
for the type encoded.- Overrides:
getEncodedTypeDescriptor
in classCoder<byte[]>
-
getEncodedElementByteSize
protected long getEncodedElementByteSize(byte[] value) throws java.lang.Exception
Description copied from class:Coder
Returns the size in bytes of the encoded value using this coder.- Overrides:
getEncodedElementByteSize
in classCoder<byte[]>
- Throws:
java.lang.Exception
-
-