// TiStream interface methods
  @Kroll.method
  public int read(Object args[]) throws IOException {
    if (!isOpen) {
      throw new IOException("Unable to read from file, not open");
    }

    BufferProxy bufferProxy = null;
    int offset = 0;
    int length = 0;

    if (args.length == 1 || args.length == 3) {
      if (args.length > 0) {
        if (args[0] instanceof BufferProxy) {
          bufferProxy = (BufferProxy) args[0];
          length = bufferProxy.getLength();

        } else {
          throw new IllegalArgumentException("Invalid buffer argument");
        }
      }

      if (args.length == 3) {
        if (args[1] instanceof Integer) {
          offset = ((Integer) args[1]).intValue();

        } else if (args[1] instanceof Double) {
          offset = ((Double) args[1]).intValue();

        } else {
          throw new IllegalArgumentException("Invalid offset argument");
        }

        if (args[2] instanceof Integer) {
          length = ((Integer) args[2]).intValue();

        } else if (args[2] instanceof Double) {
          length = ((Double) args[2]).intValue();

        } else {
          throw new IllegalArgumentException("Invalid length argument");
        }
      }

    } else {
      throw new IllegalArgumentException("Invalid number of arguments");
    }

    try {
      return TiStreamHelper.read(
          fileProxy.getBaseFile().getExistingInputStream(), bufferProxy, offset, length);

    } catch (IOException e) {
      Log.e(TAG, "Unable to read from file, IO error", e);
      throw new IOException("Unable to read from file, IO error");
    }
  }
  @Kroll.method
  public int read(BufferProxy bufferProxy, int offset, int length) throws IOException {
    InputStream inputStream = tiBlob.getInputStream();
    if (inputStream != null) {
      return TiStreamHelper.read(inputStream, bufferProxy, offset, length);

    } else {
      throw new IOException("Unable to read from blob, IO error");
    }
  }
  @Kroll.method
  public int write(Object args[]) throws IOException {
    if (!isOpen) {
      throw new IOException("Unable to write to file, not open");
    }

    byte[] bytes = null;
    int offset = 0;
    int length = 0;

    if (args.length == 1 || args.length == 3) {
      if (args.length > 0) {
        bytes = TiConvert.toBytes(args[0]);
        if (bytes != null) {
          length = bytes.length;
        } else {
          throw new IllegalArgumentException("Invalid data argument");
        }
      }

      if (args.length == 3) {
        if (args[1] instanceof Integer) {
          offset = ((Integer) args[1]).intValue();

        } else if (args[1] instanceof Double) {
          offset = ((Double) args[1]).intValue();

        } else {
          throw new IllegalArgumentException("Invalid offset argument");
        }

        if (args[2] instanceof Integer) {
          length = ((Integer) args[2]).intValue();

        } else if (args[2] instanceof Double) {
          length = ((Double) args[2]).intValue();

        } else {
          throw new IllegalArgumentException("Invalid length argument");
        }
      }

    } else {
      throw new IllegalArgumentException("Invalid number of arguments");
    }

    try {
      return TiStreamHelper.write(
          fileProxy.getBaseFile().getExistingOutputStream(), bytes, offset, length);

    } catch (IOException e) {
      Log.e(TAG, "Unable to write to file, IO error", e);
      throw new IOException("Unable to write to file, IO error");
    }
  }