public class ValidatingEncoder extends ParsingEncoder implements Parser.ActionHandler
Encoder that wraps another Encoder and
ensures that the sequence of operations conforms to the provided schema.
Use EncoderFactory.validatingEncoder(Schema, Encoder) to construct
and configure.
ValidatingEncoder is not thread-safe.Encoder,
EncoderFactory| Modifier and Type | Field and Description |
|---|---|
protected Encoder |
out |
protected Parser |
parser |
pos| Modifier and Type | Method and Description |
|---|---|
ValidatingEncoder |
configure(Encoder encoder)
Reconfigures this ValidatingEncoder to wrap the encoder provided.
|
Symbol |
doAction(Symbol input,
Symbol top)
Handle the action symbol top when the input is
sought to be taken off the stack.
|
void |
flush() |
void |
setItemCount(long itemCount)
Call this method before writing a batch of items in an array or a map.
|
void |
startItem()
Start a new item of an array or map.
|
void |
writeArrayEnd()
Call this method to finish writing an array.
|
void |
writeArrayStart()
Call this method to start writing an array.
|
void |
writeBoolean(boolean b)
Write a boolean value.
|
void |
writeBytes(byte[] bytes,
int start,
int len)
Write a byte string.
|
void |
writeBytes(ByteBuffer bytes)
Write a byte string.
|
void |
writeDouble(double d)
Write a double.
|
void |
writeEnum(int e)
Writes an enumeration.
|
void |
writeFixed(byte[] bytes,
int start,
int len)
Writes a fixed size binary object.
|
void |
writeFloat(float f)
Write a float.
|
void |
writeIndex(int unionIndex)
Call this method to write the tag of a union.
|
void |
writeInt(int n)
Writes a 32-bit integer.
|
void |
writeLong(long n)
Write a 64-bit integer.
|
void |
writeMapEnd()
Call this method to terminate the inner-most, currently-opened
map.
|
void |
writeMapStart()
Call this to start a new map.
|
void |
writeNull()
"Writes" a null value.
|
void |
writeString(CharSequence charSequence)
Write a Unicode character string.
|
void |
writeString(String str)
Write a Unicode character string.
|
void |
writeString(Utf8 utf8)
Write a Unicode character string.
|
depth, pop, pushwriteBytes, writeFixed, writeFixedpublic void flush()
throws IOException
flush in interface FlushableIOExceptionpublic ValidatingEncoder configure(Encoder encoder)
encoder - The Encoder to wrap for validation.public void writeNull()
throws IOException
EncoderwriteNull in class EncoderIOExceptionpublic void writeBoolean(boolean b)
throws IOException
EncoderwriteBoolean in class EncoderIOExceptionpublic void writeInt(int n)
throws IOException
EncoderwriteInt in class EncoderIOExceptionpublic void writeLong(long n)
throws IOException
EncoderwriteLong in class EncoderIOExceptionpublic void writeFloat(float f)
throws IOException
EncoderwriteFloat in class EncoderIOExceptionpublic void writeDouble(double d)
throws IOException
EncoderwriteDouble in class EncoderIOExceptionpublic void writeString(Utf8 utf8) throws IOException
EncoderwriteString in class EncoderIOExceptionpublic void writeString(String str) throws IOException
EncoderUtf8. Some Encoder
implementations may want to do something different as a performance optimization.writeString in class EncoderIOExceptionpublic void writeString(CharSequence charSequence) throws IOException
EncoderUtf8 it writes this directly, otherwise
the CharSequence is converted to a String via toString() and written.writeString in class EncoderIOExceptionpublic void writeBytes(ByteBuffer bytes) throws IOException
EncoderwriteBytes in class EncoderIOExceptionpublic void writeBytes(byte[] bytes,
int start,
int len)
throws IOException
EncoderwriteBytes in class EncoderIOExceptionpublic void writeFixed(byte[] bytes,
int start,
int len)
throws IOException
EncoderwriteFixed in class Encoderbytes - The contents to writestart - The position within bytes where the contents
start.len - The number of bytes to write.IOExceptionpublic void writeEnum(int e)
throws IOException
EncoderwriteEnum in class EncoderIOExceptionpublic void writeArrayStart()
throws IOException
EncoderEncoder.writeArrayStart(). Then, before writing any data for any item
call Encoder.setItemCount(long) followed by a sequence of
Encoder.startItem() and the item itself. The number of
Encoder.startItem() should match the number specified in
Encoder.setItemCount(long).
When actually writing the data of the item, you can call any Encoder method (e.g., Encoder.writeLong(long)). When all items
of the array have been written, call Encoder.writeArrayEnd().
As an example, let's say you want to write an array of records,
the record consisting of an Long field and a Boolean field.
Your code would look something like this:
out.writeArrayStart();
out.setItemCount(list.size());
for (Record r : list) {
out.startItem();
out.writeLong(r.longField);
out.writeBoolean(r.boolField);
}
out.writeArrayEnd();
writeArrayStart in class EncoderIOExceptionpublic void writeArrayEnd()
throws IOException
EncoderEncoder.writeArrayStart() for usage information.writeArrayEnd in class EncoderIOExceptionpublic void writeMapStart()
throws IOException
EncoderEncoder.writeArrayStart() for details on usage.
As an example of usage, let's say you want to write a map of
records, the record consisting of an Long field and a Boolean
field. Your code would look something like this:
out.writeMapStart(); out.setItemCount(list.size()); for (Map.Entryentry : map.entrySet()) { out.startItem(); out.writeString(entry.getKey()); out.writeLong(entry.getValue().longField); out.writeBoolean(entry.getValue().boolField); } out.writeMapEnd();
writeMapStart in class EncoderIOExceptionpublic void writeMapEnd()
throws IOException
EncoderEncoder.writeArrayStart() for more details.writeMapEnd in class EncoderIOExceptionpublic void setItemCount(long itemCount)
throws IOException
EncoderEncoder.startItem() followed by any of the
other write methods of Encoder. The number of calls
to Encoder.startItem() must be equal to the count specified
in Encoder.setItemCount(long). Once a batch is completed you
can start another batch with Encoder.setItemCount(long).setItemCount in class ParsingEncoderitemCount - The number of Encoder.startItem() calls to follow.IOExceptionpublic void startItem()
throws IOException
EncoderEncoder.writeArrayStart() for usage information.startItem in class ParsingEncoderIOExceptionpublic void writeIndex(int unionIndex)
throws IOException
Encoderout.writeIndex(1); out.writeLong(record.longField); out.writeBoolean(record.boolField);
writeIndex in class EncoderIOExceptionpublic Symbol doAction(Symbol input, Symbol top) throws IOException
Parser.ActionHandlerdoAction in interface Parser.ActionHandlerinput - The input symbol from the caller of advancetop - The symbol at the top the stack.IOExceptionCopyright © 2009-2014 The Apache Software Foundation. All Rights Reserved.