Program: 172976
Project: 172976
Buggy source:
/** 
 * Save the list of records to the file system
 * @param records records to be written
 * @param size total size of the records
 * @param outFileName out file name
 */
public static int saveKeyValues(List<KeyValue> records,List<Integer> sizes,long size,String outFileName,MessageType keyType,KryoMemorySerializer serializer){
  try {
    int totalSize=0;
    List<byte[]> byteKeys=new ArrayList<>();
    if (keyType == MessageType.OBJECT) {
      for (      KeyValue record : records) {
        byte[] data=serializer.serialize(record.getKey());
        totalSize+=data.length;
        byteKeys.add(data);
      }
    }
 else {
      for (      KeyValue record : records) {
        if (keyType == MessageType.BYTE) {
          byte[] key=(byte[])record.getKey();
          totalSize+=key.length;
        }
 else         if (keyType == MessageType.DOUBLE) {
          double[] d=(double[])record.getKey();
          totalSize+=d.length * 8;
        }
 else         if (keyType == MessageType.INTEGER) {
          int[] d=(int[])record.getKey();
          totalSize+=d.length * 4;
        }
 else         if (keyType == MessageType.LONG) {
          long[] d=(long[])record.getKey();
          totalSize+=d.length * 8;
        }
 else         if (keyType == MessageType.CHAR) {
          char[] d=(char[])record.getKey();
          totalSize+=d.length;
        }
 else         if (keyType == MessageType.SHORT) {
          short[] d=(short[])record.getKey();
          totalSize+=d.length * 2;
        }
      }
    }
    long sum=0;
    for (    Integer s : sizes) {
      sum+=s;
    }
    if (sum != size) {
      LOG.log(Level.WARNING,"String_Node_Str" + sum + "String_Node_Str"+ size);
    }
    totalSize+=size + records.size() * 8;
    LOG.log(Level.INFO,String.format("String_Node_Str",totalSize,sum,size,records.size()));
    Files.createDirectories(Paths.get(outFileName).getParent());
    FileChannel rwChannel=new RandomAccessFile(outFileName,"String_Node_Str").getChannel();
    ByteBuffer os=rwChannel.map(FileChannel.MapMode.READ_WRITE,0,totalSize);
    int totalWritten=0;
    for (int i=0; i < records.size(); i++) {
      KeyValue keyValue=records.get(i);
      byte[] r=(byte[])keyValue.getValue();
      if (keyType == MessageType.OBJECT) {
        byte[] src=byteKeys.get(i);
        os.putInt(src.length);
        os.put(src);
      }
 else       if (keyType == MessageType.BYTE) {
        byte[] key=(byte[])keyValue.getKey();
        os.putInt(key.length);
        os.put(key);
      }
 else       if (keyType == MessageType.DOUBLE) {
        double[] kd=(double[])keyValue.getKey();
        os.putInt(kd.length * 8);
        for (        double d : kd) {
          os.putDouble(d);
        }
      }
 else       if (keyType == MessageType.INTEGER) {
        int[] kd=(int[])keyValue.getKey();
        os.putInt(kd.length * 4);
        for (        int d : kd) {
          os.putInt(d);
          totalWritten+=4;
          LOG.log(Level.INFO,String.format("String_Node_Str",totalWritten));
        }
      }
 else       if (keyType == MessageType.LONG) {
        long[] kd=(long[])keyValue.getKey();
        os.putInt(kd.length * 8);
        for (        long d : kd) {
          os.putLong(d);
        }
      }
 else       if (keyType == MessageType.CHAR) {
        char[] kd=(char[])keyValue.getKey();
        os.putInt(kd.length);
        for (        char d : kd) {
          os.putChar(d);
        }
      }
 else       if (keyType == MessageType.SHORT) {
        short[] kd=(short[])keyValue.getKey();
        os.putInt(kd.length * 2);
        for (        short d : kd) {
          os.putShort(d);
        }
      }
      os.putInt(sizes.get(i));
      os.put(r,0,sizes.get(i));
    }
    rwChannel.close();
    return totalSize;
  }
 catch (  IOException e) {
    LOG.log(Level.SEVERE,"String_Node_Str",e);
    throw new RuntimeException(e);
  }
}

Fixed version (ground truth):
/** 
 * Save the list of records to the file system
 * @param records records to be written
 * @param size total size of the records
 * @param outFileName out file name
 */
public static int saveKeyValues(List<KeyValue> records,List<Integer> sizes,long size,String outFileName,MessageType keyType,KryoMemorySerializer serializer){
  try {
    int totalSize=0;
    List<byte[]> byteKeys=new ArrayList<>();
    if (keyType == MessageType.OBJECT) {
      for (      KeyValue record : records) {
        byte[] data=serializer.serialize(record.getKey());
        totalSize+=data.length;
        byteKeys.add(data);
      }
    }
 else {
      for (      KeyValue record : records) {
        if (keyType == MessageType.BYTE) {
          byte[] key=(byte[])record.getKey();
          totalSize+=key.length;
        }
 else         if (keyType == MessageType.DOUBLE) {
          double[] d=(double[])record.getKey();
          totalSize+=d.length * 8;
        }
 else         if (keyType == MessageType.INTEGER) {
          int[] d=(int[])record.getKey();
          totalSize+=d.length * 4;
        }
 else         if (keyType == MessageType.LONG) {
          long[] d=(long[])record.getKey();
          totalSize+=d.length * 8;
        }
 else         if (keyType == MessageType.CHAR) {
          char[] d=(char[])record.getKey();
          totalSize+=d.length;
        }
 else         if (keyType == MessageType.SHORT) {
          short[] d=(short[])record.getKey();
          totalSize+=d.length * 2;
        }
      }
    }
    long sum=0;
    for (    Integer s : sizes) {
      sum+=s;
    }
    if (sum != size) {
      LOG.log(Level.WARNING,"String_Node_Str" + sum + "String_Node_Str"+ size);
    }
    totalSize+=size + records.size() * 8;
    LOG.log(Level.INFO,String.format("String_Node_Str",totalSize,sum,size,records.size()));
    Files.createDirectories(Paths.get(outFileName).getParent());
    FileChannel rwChannel=new RandomAccessFile(outFileName,"String_Node_Str").getChannel();
    ByteBuffer os=rwChannel.map(FileChannel.MapMode.READ_WRITE,0,totalSize);
    int totalWritten=0;
    for (int i=0; i < records.size(); i++) {
      KeyValue keyValue=records.get(i);
      byte[] r=(byte[])keyValue.getValue();
      if (keyType == MessageType.OBJECT) {
        byte[] src=byteKeys.get(i);
        os.putInt(src.length);
        os.put(src);
      }
 else       if (keyType == MessageType.BYTE) {
        byte[] key=(byte[])keyValue.getKey();
        os.putInt(key.length);
        os.put(key);
      }
 else       if (keyType == MessageType.DOUBLE) {
        double[] kd=(double[])keyValue.getKey();
        os.putInt(kd.length * 8);
        for (        double d : kd) {
          os.putDouble(d);
        }
      }
 else       if (keyType == MessageType.INTEGER) {
        int[] kd=(int[])keyValue.getKey();
        os.putInt(kd.length * 4);
        for (        int d : kd) {
          os.putInt(d);
          totalWritten+=4;
        }
      }
 else       if (keyType == MessageType.LONG) {
        long[] kd=(long[])keyValue.getKey();
        os.putInt(kd.length * 8);
        for (        long d : kd) {
          os.putLong(d);
        }
      }
 else       if (keyType == MessageType.CHAR) {
        char[] kd=(char[])keyValue.getKey();
        os.putInt(kd.length);
        for (        char d : kd) {
          os.putChar(d);
        }
      }
 else       if (keyType == MessageType.SHORT) {
        short[] kd=(short[])keyValue.getKey();
        os.putInt(kd.length * 2);
        for (        short d : kd) {
          os.putShort(d);
        }
      }
      os.putInt(sizes.get(i));
      os.put(r,0,sizes.get(i));
    }
    rwChannel.close();
    return totalSize;
  }
 catch (  IOException e) {
    LOG.log(Level.SEVERE,"String_Node_Str",e);
    throw new RuntimeException(e);
  }
}