Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#ccccff
private void readIntoDirectBuffer() throws IOException {
  ByteBuffer buffer = ByteBuffer.allocateDirect(16*1024);
  FileChannel rdr = (new FileInputStream("file")).getChannel();
  while(rdr.read(buffer) > 0) {
    // do something with the buffer
    buffer.clear();
  }
  rdr.close();
}

Note that manual clearing of the buffer data is mandatory because direct buffers are not subject to garbage collection.

Risk Assessment

Failure to limit the lifetime of sensitive data can lead to sensitive information leaks.

...