code: public static void writeRAMFiles(DataOutput out, RAMDirectory dir,
        String[] names) throws IOException {
      out.writeInt(names.length);
  
      for (int i = 0; i < names.length; i++) {
        Text.writeString(out, names[i]);
        long length = dir.fileLength(names[i]);
        out.writeLong(length);
  
        if (length > 0) {
          // can we avoid the extra copy?
          IndexInput input = null;
          try {
            input = dir.openInput(names[i], BUFFER_SIZE);
  
            int position = 0;
            byte[] buffer = new byte[BUFFER_SIZE];
  
            while (position < length) {
              int len =
                  position + BUFFER_SIZE <= length ? BUFFER_SIZE
                      : (int) (length - position);
              input.readBytes(buffer, 0, len);
              out.write(buffer, 0, len);
              position += len;
            }
          } finally {
            if (input != null) {
              input.close();
            }
          }
        }
      }
    }
docs: Write a number of files from a ram directory to a data output.
  @param out  the data output
  @param dir  the ram directory
  @param names  the names of the files to write
  @throws IOException