org.apache.avro.io
Class ResolvingDecoder

java.lang.Object
  extended by org.apache.avro.io.Decoder
      extended by org.apache.avro.io.ParsingDecoder
          extended by org.apache.avro.io.ValidatingDecoder
              extended by org.apache.avro.io.ResolvingDecoder
All Implemented Interfaces:
Parser.ActionHandler, SkipParser.SkipHandler

public class ResolvingDecoder
extends ValidatingDecoder

Decoder that peforms type-resolution between the reader's and writer's schemas.

When resolving schemas, this class will return the values of fields in _writer's_ order, not the reader's order. (However, it returns _only_ the reader's fields, not any extra fields the writer may have written.) To help clients handle fields that appear to be coming out of order, this class defines the methods readFieldName() and readFieldIndex(). When called just before reading the value of a record-field, they return the name/index of the field about to be read. See readFieldIndex() for usage details.

See the parser documentation for information on how this works.


Field Summary
 
Fields inherited from class org.apache.avro.io.ValidatingDecoder
in
 
Fields inherited from class org.apache.avro.io.ParsingDecoder
parser
 
Constructor Summary
ResolvingDecoder(Schema writer, Schema reader, Decoder in)
           
 
Method Summary
 Symbol doAction(Symbol input, Symbol top)
           
 double readDouble()
          Reads a double written by Encoder.writeDouble(double).
 int readEnum()
          Reads an enumeration.
 int readFieldIndex()
          Returns the (zero-based) index of the next field of the record we're reading.
 String readFieldName()
          Returns the name of the next field of the record we're reading.
 int readIndex()
          Reads the tag of a union written by Encoder.writeIndex(int).
 long readLong()
          Reads a long written by Encoder.writeLong(long).
 void skipAction()
          Skips the action at the top of the stack.
 
Methods inherited from class org.apache.avro.io.ValidatingDecoder
arrayNext, init, mapNext, readArrayStart, readBoolean, readBytes, readFixed, readFloat, readInt, readMapStart, readNull, readString, skipArray, skipBytes, skipFixed, skipFixed, skipMap, skipString
 
Methods inherited from class org.apache.avro.io.ParsingDecoder
skipTopSymbol
 
Methods inherited from class org.apache.avro.io.Decoder
readFixed
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ResolvingDecoder

public ResolvingDecoder(Schema writer,
                        Schema reader,
                        Decoder in)
                 throws IOException
Throws:
IOException
Method Detail

readFieldName

public String readFieldName()
                     throws IOException
Returns the name of the next field of the record we're reading. Similar to readFieldIndex() -- see that method for details.

Throws:
IllegalStateExcpetion - If we're not about to read a record-field
IOException

readFieldIndex

public int readFieldIndex()
                   throws IOException
Returns the (zero-based) index of the next field of the record we're reading. This method is useful because ResolvingDecoder returns values in the order written by the writer, rather than the order expected by the reader. This method allows reader's to figure out what fields to expect. Let's say the reader is expecting a three-field record, the first field is a long, the second a string, and the third an array. In this case, a typical usage might be as follows:
   for (int i = 0; i < 3; i++) {
     switch (in.readFieldIndex()) {
     case 1:
       foo(in.readLong());
       break;
     case 2:
       someVariable = in.readString();
       break;
     case 3:
       bar(in); // The code of "bar" will read an array-of-int
       break;
     }
 
Note that ResolvingDecoder will return only the fields expected by the reader, not other fields that may have been written by the writer. Thus, the iteration-count of "3" in the above loop will always be correct. Throws a runtime exception if we're not just about to read the field of a record. Also, this method (and readFieldName()) will consume the field information, and thus may only be called once before reading the field value. (However, if the client knows the order of incoming fields and does not need to reorder them, then the client does not need to call this or readFieldName().)

Throws:
IllegalStateExcpetion - If we're not about to read a record-field
IOException

readLong

public long readLong()
              throws IOException
Description copied from class: Decoder
Reads a long written by Encoder.writeLong(long).

Overrides:
readLong in class ValidatingDecoder
Throws:
IOException

readDouble

public double readDouble()
                  throws IOException
Description copied from class: Decoder
Reads a double written by Encoder.writeDouble(double).

Overrides:
readDouble in class ValidatingDecoder
Throws:
IOException

readEnum

public int readEnum()
             throws IOException
Description copied from class: Decoder
Reads an enumeration.

Overrides:
readEnum in class ValidatingDecoder
Returns:
The enumeration's value.
Throws:
IOException

readIndex

public int readIndex()
              throws IOException
Description copied from class: Decoder
Reads the tag of a union written by Encoder.writeIndex(int).

Overrides:
readIndex in class ValidatingDecoder
Throws:
IOException

doAction

public Symbol doAction(Symbol input,
                       Symbol top)
                throws IOException
Specified by:
doAction in interface Parser.ActionHandler
Overrides:
doAction in class ValidatingDecoder
Throws:
IOException

skipAction

public void skipAction()
                throws IOException
Description copied from interface: SkipParser.SkipHandler
Skips the action at the top of the stack.

Specified by:
skipAction in interface SkipParser.SkipHandler
Overrides:
skipAction in class ParsingDecoder
Throws:
IOException


Copyright © 2009 The Apache Software Foundation