Ejemplo n.º 1
0
  /**
   * @param file The file to open
   * @param mode Either "rw" or "r", depending on whether this file is read only
   * @param bufferSize The size of the buffer used to manually buffer I/O If -1 the default buffer
   *     size is used, which depends on how {@link DirectIoLib} is implemented.
   * @throws IOException
   */
  public DirectRandomAccessFile(File file, String mode, int bufferSize) throws IOException {

    boolean readOnly = false;
    if (mode.equals("r")) {
      readOnly = true;
    } else if (!mode.equals("rw")) {
      throw new IllegalArgumentException("only r and rw modes supported");
    }

    if (readOnly && !file.isFile()) {
      throw new FileNotFoundException("couldn't find file " + file);
    }

    this.channel =
        bufferSize != -1
            ? DirectIoByteChannelAligner.open(file, bufferSize, readOnly)
            : DirectIoByteChannelAligner.open(file, readOnly);
  }
Ejemplo n.º 2
0
 @Override
 public void write(int v) throws IOException {
   channel.write(v);
 }
Ejemplo n.º 3
0
 @Override
 public void close() throws IOException {
   channel.close();
 }
Ejemplo n.º 4
0
 /** @return The current position in the file */
 public long getFilePointer() {
   return channel.position();
 }
Ejemplo n.º 5
0
 /** @return The current length of the file */
 public long length() {
   return channel.size();
 }
Ejemplo n.º 6
0
 /**
  * Seeks to position <tt>pos</tt> within the file.
  *
  * @param pos The position to which to seek
  * @throws IOException
  */
 public void seek(long pos) throws IOException {
   channel.position(pos);
 }
Ejemplo n.º 7
0
 /**
  * Bulk read bytes from channel into dst.
  *
  * @param dst The destination byte array
  * @throws IOException
  */
 public void read(byte[] dst) throws IOException {
   channel.readBytes(dst, 0, dst.length);
 }
Ejemplo n.º 8
0
 /**
  * Bulk read bytes from channel into dst.
  *
  * @param dst The destination byte array
  * @param offset The offset within dst to start reading
  * @param length The number of bytes to read
  * @throws IOException
  */
 public void read(byte[] dst, int offset, int length) throws IOException {
   channel.readBytes(dst, offset, length);
 }
Ejemplo n.º 9
0
 /**
  * Reads one byte, and returns cast as an int.
  *
  * @return one byte from the underlying channel
  * @throws IOException
  */
 public int read() throws IOException {
   return channel.read();
 }
Ejemplo n.º 10
0
 @Override
 public void write(byte[] src, int offset, int length) throws IOException {
   channel.writeBytes(src, offset, length);
 }