Class ByteArrayCoder
- All Implemented Interfaces:
Serializable
Coder for byte[].
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:
-
Nested Class Summary
Nested classes/interfaces inherited from class org.apache.beam.sdk.coders.Coder
Coder.Context, Coder.NonDeterministicException -
Method Summary
Modifier and TypeMethodDescriptionbyte[]decode(InputStream inStream) Decodes a value of typeTfrom the given input stream in the given context.byte[]decode(InputStream inStream, Coder.Context context) Decodes a value of typeTfrom the given input stream in the given context.voidencode(byte[] value, OutputStream outStream) Encodes the given value of typeTonto the given output stream.voidencode(byte[] value, OutputStream outStream, Coder.Context context) Encodes the given value of typeTonto the given output stream in the given context.voidencodeAndOwn(byte[] value, OutputStream outStream, Coder.Context context) Encodes the providedvaluewith the identical encoding toencode(byte[], java.io.OutputStream), but with optimizations that take ownership of the value.protected longgetEncodedElementByteSize(byte[] value) Returns the size in bytes of the encoded value using this coder.TypeDescriptor<byte[]> Returns theTypeDescriptorfor the type encoded.booleanisRegisterByteSizeObserverCheap(byte[] value) Returns whetherCoder.registerByteSizeObserver(T, org.apache.beam.sdk.util.common.ElementByteSizeObserver)cheap enough to call for every element, that is, if thisCodercan calculate the byte size of the element to be coded in roughly constant time (or lazily).static ByteArrayCoderof()structuralValue(byte[] value) Returns an object with anObject.equals()method that represents structural equality on the argument.voidThrowCoder.NonDeterministicExceptionif the coding is not deterministic.Methods inherited from class org.apache.beam.sdk.coders.AtomicCoder
equals, getCoderArguments, getComponents, hashCodeMethods inherited from class org.apache.beam.sdk.coders.StructuredCoder
toStringMethods inherited from class org.apache.beam.sdk.coders.Coder
consistentWithEquals, getEncodedElementByteSizeUsingCoder, registerByteSizeObserver, verifyDeterministic, verifyDeterministic
-
Method Details
-
of
-
encode
Description copied from class:CoderEncodes the given value of typeTonto the given output stream. Multiple elements can be encoded next to each other on the output stream, each coder should encode information to know how many bytes to read when decoding. A common approach is to prefix the encoding with the element's encoded length.- Specified by:
encodein classCoder<byte[]>- Throws:
IOException- if writing to theOutputStreamfails for some reasonCoderException- if the value could not be encoded for some reason
-
encode
public void encode(byte[] value, OutputStream outStream, Coder.Context context) throws IOException, CoderException Description copied from class:CoderEncodes the given value of typeTonto the given output stream in the given context.- Overrides:
encodein classCoder<byte[]>- Throws:
IOException- if writing to theOutputStreamfails for some reasonCoderException- if the value could not be encoded for some reason
-
encodeAndOwn
public void encodeAndOwn(byte[] value, OutputStream outStream, Coder.Context context) throws IOException, CoderException Encodes the providedvaluewith the identical encoding toencode(byte[], java.io.OutputStream), but with optimizations that take ownership of the value.Once passed to this method,
valueshould never be observed or mutated again.- Throws:
IOExceptionCoderException
-
decode
Description copied from class:CoderDecodes a value of typeTfrom the given input stream in the given context. Returns the decoded value. Multiple elements can be encoded next to each other on the input stream, each coder should encode information to know how many bytes to read when decoding. A common approach is to prefix the encoding with the element's encoded length.- Specified by:
decodein classCoder<byte[]>- Throws:
IOException- if reading from theInputStreamfails for some reasonCoderException- if the value could not be decoded for some reason
-
decode
public byte[] decode(InputStream inStream, Coder.Context context) throws IOException, CoderException Description copied from class:CoderDecodes a value of typeTfrom the given input stream in the given context. Returns the decoded value.- Overrides:
decodein classCoder<byte[]>- Throws:
IOException- if reading from theInputStreamfails for some reasonCoderException- if the value could not be decoded for some reason
-
verifyDeterministic
public void verifyDeterministic()Description copied from class:AtomicCoderThrowCoder.NonDeterministicExceptionif the coding is not deterministic.In order for a
Coderto 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
Coderalways 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
AtomicCoderis presumed to be deterministic- Overrides:
verifyDeterministicin classAtomicCoder<byte[]>
- two values that compare as equal (via
-
structuralValue
Returns an object with anObject.equals()method that represents structural equality on the argument.For any two values
xandyof 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
nullshould 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:
structuralValuein 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 thisCodercan 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
PipelineRunnerimplementations.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:
isRegisterByteSizeObserverCheapin classCoder<byte[]>- Returns:
truesincegetEncodedElementByteSize(byte[])runs in constant time using thelengthof the provided array.
-
getEncodedTypeDescriptor
Description copied from class:CoderReturns theTypeDescriptorfor the type encoded.- Overrides:
getEncodedTypeDescriptorin classCoder<byte[]>
-
getEncodedElementByteSize
Description copied from class:CoderReturns the size in bytes of the encoded value using this coder.- Overrides:
getEncodedElementByteSizein classCoder<byte[]>- Throws:
Exception
-