title: Serialize an array of Avro GenericRecord
body: <p>I use following code to serialize and deserialize between a list of GenericRecord and bytes:</p>
  
  <pre><code>public static byte[] encode(List&lt;GenericRecord&gt; records, Schema schema) throws IOException {
      SpecificDatumWriter&lt;GenericRecord&gt;
          datumWriter =
          new SpecificDatumWriter&lt;&gt;(schema);
      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
      byteArrayOutputStream.reset();
      BinaryEncoder binaryEncoder = new EncoderFactory().binaryEncoder(byteArrayOutputStream, null);
      for(GenericRecord segment: records) {
          datumWriter.write(segment, binaryEncoder);
      }
      binaryEncoder.flush();
      byte[] bytes = byteArrayOutputStream.toByteArray();
      return bytes;
  }
  
  
  public static List&lt;GenericRecord&gt;  decode(byte[] recordBytes, Schema schema) throws IOException {
      DatumReader&lt;GenericRecord&gt;
          datumReader =
          new SpecificDatumReader&lt;&gt;(schema);
      ByteArrayInputStream stream = new ByteArrayInputStream(recordBytes);
      stream.reset();
      BinaryDecoder binaryDecoder = new DecoderFactory().binaryDecoder(stream, null);
      List&lt;GenericRecord&gt; records = new ArrayList&lt;&gt;();
      while (true) {
          try {
              GenericRecord record = datumReader.read(null, binaryDecoder);
              records.add(record);
          } catch (EOFException e) {
              break;
          }
      }
      return records;
  }
  </code></pre>
  
  <p>and I got following exception:</p>
  
  <pre><code>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -40
          at org.apache.avro.io.parsing.Symbol$Alternative.getSymbol(Symbol.java:402)
          at org.apache.avro.io.ResolvingDecoder.doAction(ResolvingDecoder.java:290)
          at org.apache.avro.io.parsing.Parser.advance(Parser.java:88)
          at org.apache.avro.io.ResolvingDecoder.readIndex(ResolvingDecoder.java:267)
          at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:155)
          at org.apache.avro.generic.GenericDatumReader.readField(GenericDatumReader.java:193)
          at org.apache.avro.generic.GenericDatumReader.readRecord(GenericDatumReader.java:183)
          at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:151)
          at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:142)
          at com.yahoo.gup.utils.SerilizationUtils.decode(SerilizationUtils.java:60)
  </code></pre>
  
  <p>Any ideas of the root cause? Am I doing the serialization in the right way? </p>