Java Reference
In-Depth Information
buf.asDoubleBuffer().put(0, (double)primeStr.length());
This creates a view buffer of type
DoubleBuffer
and calls its
put()
method to transfer the length of the
string to the buffer. You don't save the view buffer reference because you need a different view buffer
on the next iteration — one that maps to the position in the byte buffer following the data that you are
transferring for the current prime.
The next statement increments the position of the byte buffer by the number of bytes in the string length
value. You then execute the statement:
buf.position(buf.position()
+
2*buf.asCharBuffer().put(primeStr).position());
This statement is a little complicated so let's dissect it. The expression for the argument to the
posi-
tion()
method within the parentheses executes first. This calculates the new position for
buf
as the cur-
rent position, given by
buf.position()
, plus the value resulting from the expression:
2*buf.asCharBuffer().put(primeStr).position()
The subexpression,
buf.asCharBuffer()
, creates a view buffer of type
CharBuffer
. You call the
put()
method for this buffer to transfer
primeStr
to it, and this returns a reference to the
CharBuffer
object.
You use this reference to call the
put()
method for the
CharBuffer
object to transfer the string. You use
the reference that the
put()
method returns to call the
position()
method for the
CharBuffer
object,
which returns the buffer position after the string has been transferred, so multiplying this value by 2 gives
the number of bytes occupied by the string in
buf
. Thus, you update the position for
buf
to the point
following the string that you transfer to the buffer.
The last step in the inner loop is to execute the following statements:
buf.asLongBuffer().put(primes[primesWritten++]);
buf.position(buf.position() + 8);
The first statement here transfers the binary prime value to the buffer via a view buffer of type
LongBuffer
and increments the count of the number of primes written to the file. The second statement
updates the position for
buf
to the next available byte. The inner
while
loop then continues with the next
iteration to load the data for the next prime into the buffer. This continues until there is insufficient space
for data for another prime, whereupon the inner loop ends, and the buffer is written to the file in the outer
loop.
Writing from Multiple Buffers
I will introduce one further file channel output capability before you try reading a file — the ability to trans-
fer data to a file from several buffers in sequence in a single write operation. This is called a
gathering-write
operation. The advantage of this capability is that it avoids the necessity to copy information into a single
buffer before writing it to a file. A gathering-write operation is one side of what are called
scatter-gather
I/O operations
. You look into the other side — the
scattering-read
operation — in the next chapter.
A file channel has two methods declared by the
GatheringByteChannel
interface that performs
gathering-write operations from an array of
ByteBuffer
objects: