...
| Code Block | ||
|---|---|---|
| ||
ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
try(FileChannel rdr = (new FileInputStream("file")).getChannel()){
while (rdr.read(buffer) > 0) {
// Do something with the buffer
buffer.clear();
}
}
catch (Exception e) {
//handle Handle error
}
|
Note that manual clearing of the buffer data is mandatory because direct buffers are exempt from garbage collection.
...
| [API 2011] | Class ByteBuffer |
| [Oracle 2012b] | Reading ASCII Passwords from an InputStream Example (Java Cryptography Architecture ([JCA) ] Reference Guide) |
| [Tutorials 2008] | I/O from the Command Line |
...