@Override
 public synchronized void appendEntry(final Reads.ReadEntry.Builder entryBuilder)
     throws IOException {
   collectionBuilder.addReads(entryBuilder.build());
   messageChunkWriter.writeAsNeeded(collectionBuilder);
   barcodeIndex = -1;
 }
  /**
   * Append an entry with a specific read index.
   *
   * @param readIndex Index of the read that will be written
   * @throws IOException If an error occurs while writing the file.
   */
  @Override
  public synchronized void appendEntry(final int readIndex) throws IOException {

    Reads.ReadEntry.Builder entryBuilder = Reads.ReadEntry.newBuilder();

    entryBuilder.setReadIndex(readIndex);

    // set current read index to enable interleaving calls to appendEntry(readIndex)/appendEntry().
    this.readIndex = readIndex;
    if (barcodeIndex != -1) {
      entryBuilder.setBarcodeIndex(barcodeIndex);
    }
    if (description != null) {
      entryBuilder.setDescription(description.toString());
      description = null;
    }
    if (identifier != null) {
      entryBuilder.setReadIdentifier(identifier.toString());
      identifier = null;
    }
    if (sequence != null) {
      entryBuilder.setSequence(encodeSequence(sequence));
      sequence = null;
    }
    entryBuilder.setReadLength(previousReadLength);
    if (pairSequence != null) {
      entryBuilder.setSequencePair(encodeSequence(pairSequence));
      pairSequence = null;
      entryBuilder.setReadLengthPair(previousReadLength);
    }

    if (qualityScores != null) {
      entryBuilder.setQualityScores(ByteString.copyFrom(qualityScores));
      qualityScores = null;
    }

    if (qualityScoresPair != null) {
      entryBuilder.setQualityScoresPair(ByteString.copyFrom(qualityScoresPair));
      qualityScoresPair = null;
    }

    if (firstRead == true && keyValuePairs != null) {
      // Append meta data on the very first read of each file. This is used instead of a separate
      // header file.
      for (Object keyObject : keyValuePairs.keySet()) {
        String key = keyObject.toString();
        String value = keyValuePairs.get(key).toString();
        entryBuilder.addMetaData(Reads.MetaData.newBuilder().setKey(key).setValue(value));
      }
      firstRead = false;
    }
    if (codec != null) {
      if (collectionBuilder.getReadsCount() == 0) {
        codec.newChunk();
      }
      Reads.ReadEntry.Builder result = codec.encode(entryBuilder);
      if (result != null) {
        entryBuilder = result;
      }
    }
    collectionBuilder.addReads(entryBuilder.build());

    messageChunkWriter.writeAsNeeded(collectionBuilder);
    barcodeIndex = -1;
  }