Program: 113244
Project: 113244
Buggy source:
/** 
 * <pre>  {@code NodeSet = <HEADER>,[DATA] HEADER = <int>, // size of this header <int>, //size, number of nodes <byte[]>, // node flags (4 bits per node) <uvarint>, //size in bytes of node data section <uvarint>, //size in bytes of objectids array <uvarint>, //size in bytes of bounds section <uvarint>; //size in bytes of extra data section DATA =  <uvarint[]>,  //node name indexes on string table <objectid[]>, //node object ids <bitset>, //metadata ids presence bitset [objectid[]], //metadata ids (only those present) (bounds and extra data TBD) NODE =   <uvarint>, //name index <uvarint>, //objectid index [uvarint], //metadataid index [uvarint], //bounds index [uvarint]; //extra data relative offset}</pre>
 */
public static void encode(DataOutput out,final List<Node> nodes,StringTable stringTable) throws IOException {
  final int size=nodes.size();
  if (size == 0) {
    out.writeInt(Integer.BYTES);
    return;
  }
  InternalDataOutput header=stream(32);
  InternalDataOutput nodeData=stream(size * 8);
  InternalDataOutput inlineExtraData=stream(size * 32);
  Index<ObjectId> uniqueIds=new Index<>();
  InternalDataOutput objectIds=stream(size * ObjectId.NUM_BYTES);
  header.writeInt(0);
  NodeFlags flags=new NodeFlags(size);
  Envelope bounds=new Envelope();
  List<Coordinate> boundsCoords=new ArrayList<>(2 * size);
  for (int nodeIndex=0; nodeIndex < size; nodeIndex++) {
    final Node node=nodes.get(nodeIndex);
    Preconditions.checkArgument(node != null);
    final int nameIndex, oidIndex, mdIdIndex, boundsIndex, extraDataRelOffset;
    nameIndex=stringTable.add(node.getName());
    checkState(nameIndex > -1);
    oidIndex=objectIdIndex(node.getObjectId(),uniqueIds,objectIds);
    Optional<ObjectId> metadataId=node.getMetadataId();
    flags.metadataPresent(nodeIndex,metadataId.isPresent());
    mdIdIndex=metadataId.isPresent() ? objectIdIndex(metadataId.get(),uniqueIds,objectIds) : -1;
    bounds.setToNull();
    node.expand(bounds);
    flags.boundsPresent(nodeIndex,!bounds.isNull());
    if (bounds.isNull()) {
      boundsIndex=-1;
    }
 else {
      boundsIndex=boundsCoords.size();
      boolean isPoint=bounds.getWidth() == 0D && bounds.getHeight() == 0D;
      flags.isPoint(nodeIndex,isPoint);
      boundsCoords.add(new Coordinate(bounds.getMinX(),bounds.getMinY()));
      if (!isPoint) {
        boundsCoords.add(new Coordinate(bounds.getMaxX(),bounds.getMaxY()));
      }
    }
    final Map<String,Object> extraData=node.getExtraData();
    if (extraData.isEmpty()) {
      flags.extraDataPresent(nodeIndex,false);
      extraDataRelOffset=-1;
    }
 else {
      flags.extraDataPresent(nodeIndex,true);
      extraDataRelOffset=inlineExtraData.size();
      ExtraData.encode(extraData,inlineExtraData,stringTable);
    }
    writeUnsignedVarInt(nameIndex,nodeData);
    writeUnsignedVarInt(oidIndex,nodeData);
    if (metadataId.isPresent()) {
      writeUnsignedVarInt(mdIdIndex,nodeData);
    }
    if (!bounds.isNull()) {
      writeUnsignedVarInt(boundsIndex,nodeData);
    }
    if (!extraData.isEmpty()) {
      writeUnsignedVarInt(extraDataRelOffset,nodeData);
    }
  }
  header.writeInt(size);
  final byte[] flagsData=flags.bitset.toByteArray();
  header.write(flagsData);
  int nodeDataSize=nodeData.size();
  writeUnsignedVarInt(nodeDataSize,header);
  int oidSectionSize=objectIds.size();
  writeUnsignedVarInt(oidSectionSize,header);
  IntCoordinateSequence boundsSeq=new IntCoordinateSequence(2,boundsCoords);
  InternalDataOutput boundsStream=stream(boundsSeq.size() * 4);
  long[] xordinates=boundsSeq.ordinates()[0];
  long[] yordinates=boundsSeq.ordinates()[1];
  Varints.writeLongArrayDeltaEncoded(xordinates,boundsStream);
  Varints.writeLongArrayDeltaEncoded(yordinates,boundsStream);
  final int coordsSectionSzie=boundsStream.size();
  writeUnsignedVarInt(coordsSectionSzie,header);
  writeUnsignedVarInt(inlineExtraData.size(),header);
  final int headerSize=header.size();
  header.reset().writeInt(headerSize);
  header.setSize(headerSize);
  header.writeTo(out);
  nodeData.writeTo(out);
  objectIds.writeTo(out);
  boundsStream.writeTo(out);
  inlineExtraData.writeTo(out);
}

Fixed version (ground truth):
/** 
 * <pre>  {@code NodeSet = <HEADER>,[DATA] HEADER = <int>, // size of this header <int>, //size, number of nodes <byte[]>, // node flags (4 bits per node) <uvarint>, //size in bytes of node data section <uvarint>, //size in bytes of objectids array <uvarint>, //size in bytes of bounds section <uvarint>; //size in bytes of extra data section DATA =  <uvarint[]>,  //node name indexes on string table <objectid[]>, //node object ids <bitset>, //metadata ids presence bitset [objectid[]], //metadata ids (only those present) (bounds and extra data TBD) NODE =   <uvarint>, //name index <uvarint>, //objectid index [uvarint], //metadataid index [uvarint], //bounds index [uvarint]; //extra data relative offset}</pre>
 */
public static void encode(DataOutput out,final List<Node> nodes,StringTable stringTable) throws IOException {
  final int size=nodes.size();
  if (size == 0) {
    out.writeInt(Integer.BYTES);
    return;
  }
  InternalDataOutput header=stream(32);
  InternalDataOutput nodeData=stream(size * 8);
  InternalDataOutput inlineExtraData=stream(size * 32);
  Index<ObjectId> uniqueIds=new Index<>();
  InternalDataOutput objectIds=stream(size * ObjectId.NUM_BYTES);
  header.writeInt(0);
  NodeFlags flags=new NodeFlags(size);
  Envelope bounds=new Envelope();
  List<Coordinate> boundsCoords=new ArrayList<>(2 * size);
  for (int nodeIndex=0; nodeIndex < size; nodeIndex++) {
    final Node node=nodes.get(nodeIndex);
    Preconditions.checkArgument(node != null);
    final int nameIndex, oidIndex, mdIdIndex, boundsIndex, extraDataRelOffset;
    nameIndex=stringTable.add(node.getName());
    checkState(nameIndex > -1);
    oidIndex=objectIdIndex(node.getObjectId(),uniqueIds,objectIds);
    Optional<ObjectId> metadataId=node.getMetadataId();
    flags.metadataPresent(nodeIndex,metadataId.isPresent());
    mdIdIndex=metadataId.isPresent() ? objectIdIndex(metadataId.get(),uniqueIds,objectIds) : -1;
    bounds.setToNull();
    node.expand(bounds);
    flags.boundsPresent(nodeIndex,!bounds.isNull());
    if (bounds.isNull()) {
      boundsIndex=-1;
    }
 else {
      boundsIndex=boundsCoords.size();
      boolean isPoint=bounds.getWidth() == 0D && bounds.getHeight() == 0D;
      flags.isPoint(nodeIndex,isPoint);
      boundsCoords.add(new Coordinate(bounds.getMinX(),bounds.getMinY()));
      if (!isPoint) {
        boundsCoords.add(new Coordinate(bounds.getMaxX(),bounds.getMaxY()));
      }
    }
    final Map<String,Object> extraData=node.getExtraData();
    if (extraData.isEmpty()) {
      flags.extraDataPresent(nodeIndex,false);
      extraDataRelOffset=-1;
    }
 else {
      flags.extraDataPresent(nodeIndex,true);
      extraDataRelOffset=inlineExtraData.size();
      ExtraData.encode(extraData,inlineExtraData,stringTable);
    }
    writeUnsignedVarInt(nameIndex,nodeData);
    writeUnsignedVarInt(oidIndex,nodeData);
    if (metadataId.isPresent()) {
      writeUnsignedVarInt(mdIdIndex,nodeData);
    }
    if (!bounds.isNull()) {
      writeUnsignedVarInt(boundsIndex,nodeData);
    }
    if (!extraData.isEmpty()) {
      writeUnsignedVarInt(extraDataRelOffset,nodeData);
    }
  }
  header.writeInt(size);
  final byte[] flagsData=flags.bitset.toByteArray();
  header.writeInt(flagsData.length);
  header.write(flagsData);
  int nodeDataSize=nodeData.size();
  writeUnsignedVarInt(nodeDataSize,header);
  int oidSectionSize=objectIds.size();
  writeUnsignedVarInt(oidSectionSize,header);
  IntCoordinateSequence boundsSeq=new IntCoordinateSequence(2,boundsCoords);
  InternalDataOutput boundsStream=stream(boundsSeq.size() * 4);
  long[] xordinates=boundsSeq.ordinates()[0];
  long[] yordinates=boundsSeq.ordinates()[1];
  Varints.writeLongArrayDeltaEncoded(xordinates,boundsStream);
  Varints.writeLongArrayDeltaEncoded(yordinates,boundsStream);
  final int coordsSectionSzie=boundsStream.size();
  writeUnsignedVarInt(coordsSectionSzie,header);
  writeUnsignedVarInt(inlineExtraData.size(),header);
  final int headerSize=header.size();
  header.reset().writeInt(headerSize);
  header.setSize(headerSize);
  header.writeTo(out);
  nodeData.writeTo(out);
  objectIds.writeTo(out);
  boundsStream.writeTo(out);
  inlineExtraData.writeTo(out);
}