| What ByteBuffer is Not | Sequentially Reading A File |
| How It Works | Little Endian Files |
| Operations | Learning More |
| Sample Code | Links |
// how to simulate a ByteBuffer.toArray ByteBuffer bb; ... byte[] contentsOnly = Arrays.copyOf( bb.array(), bb.position() );
If you don’t want to allocate large hunks of your virtual address space, you could allocate a smaller MappedByteBuffer at some offset in the file other than 0, and read a decently large chunk of it. When done, allocate a new MappedByteBuffer. You can be considerably more generous in your chunk size than when allocating buffers.
Alternatively, you could do your I/O in a more conventional way using FileChannel. read( ByteBuffer dst ), to read the next chunk of the file into a pre-allocated ByteBuffer. This approach is clumbsier than traditional stream I/O, but can be more efficient, especially when you slew over most of the data, or access it via the backing array. It will pay off if for example you were processing just a 4-byte field in a 512-byte record, since only the bytes you need are copied from the buffer, not the entire record.
The effect is even more pronounced with MappedBuffers and large records where pages of records you don’t need are not even read into RAM (Random Access Memory).
|
|
available on the web at: |
http://mindprod.com/jgloss/bytebuffer.html |
optional Replicator mirror
|
J:\mindprod\jgloss\bytebuffer.html | |
![]() |
Please email your
feedback for publication,
letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear
wording, broken/redirected link reports, suggestions to improve this page or comments to
Roedy Green :
| |
| Blog | Canadian
Mind
Products
IP:[65.110.21.43] Your face IP:[107.20.7.65] |
|
| Feedback | You are visitor number 55,436. | |