Пример #1
0
 /**
  * Writes from a byte array using the supplied file descriptor.
  *
  * @param src The source buffer.
  * @return Number of bytes written.
  * @throws IOException
  */
 public int write(byte[] src, int pos, int len) throws IOException {
   if (MemoryManager.willNeverMove(src)) {
     return write(nfd.getNativeFD(), src, pos, len);
   } else {
     byte[] buffer;
     // Rebuffer the IO in a thread-local DirectBuffer
     buffer = localByteArray.get(len);
     if (VM.VerifyAssertions) VM._assert(MemoryManager.willNeverMove(buffer));
     System.arraycopy(src, pos, buffer, 0, len);
     return write(nfd.getNativeFD(), buffer, 0, len);
   }
 }
Пример #2
0
  /**
   * Reads a byte array directly. Performs optimal buffering.
   *
   * <p>If the target buffer is pinned, use it directly. Otherwise allocate one of the thread-local
   * buffers, perform the IO to that, and copy the result to the target array.
   *
   * @param dst Byte array to read to
   * @return Number of bytes read.
   * @throws IOException If an error occurs or dst is not a direct buffers.
   */
  private int read(byte[] dst, int pos, int len) throws IOException {
    if (MemoryManager.willNeverMove(dst)) {
      return read(nfd.getNativeFD(), dst, pos, len);
    } else {
      byte[] buffer;
      // Rebuffer the IO in a thread-local byte array
      buffer = localByteArray.get(len);

      /* perform the read */
      int bytes = read(nfd.getNativeFD(), buffer, 0, len);
      if (bytes > 0) System.arraycopy(buffer, 0, dst, pos, bytes);
      return bytes;
    }
  }