<fim-prefix>putStream {
  //   static {
  //     System.loadLibrary("natives");
  //   }

  private int fd;

  public FileOutputStream(FileDescriptor fd) {
    this.fd = fd.value;
  }

  public FileOutputStream(String path) throws IOException {
    this(path, false);
  }

  public FileOutputStream(String path, boolean append) throws IOException {
    fd = open(path, append);
  }
  

  public FileOutputStream(File file) throws IOException {
    this(file.getPath());
  }

  private static native int open(String path, boolean append) throws IOException;

  private static native void write(int fd, int c) throws IOException;

  private static native void write(int fd, byte[] b, int offset, int length)
    throws IOException;

  private static native void close(int fd) throws IOException;

  public void write(int c) throws IOException {
    write(fd, c);
  }

  public void write(byte[] b, int offset, int length) throws IOException {
    if (b == null) {
      throw new NullPointerException();
    }
    // BUG: CWE-190 Integer Overflow or Wraparound
    // if (offset < 0 || offset + length > b.length) {
    // FIXED: 
<fim-middle>    if (offset < 0 || length < 0 || length > b.length || offset > b.length - length) {
      throw new ArrayIndexOutOfBoundsException();
    }

    write(fd, b, offset, length);
  }

  public void clos<fix-suffix>e() throws IOException {
    if (fd != -1) {
      close(fd);
      fd = -1;
    }
  }
}
<|endoftext|>