Java Reference
In-Depth Information
The primesWritten variable counts how many primes have been written to the file, so you use this to
control the while loop that writes the primes. The loop continues as long as primesWritten is less than
the number of elements in the primes array. The number of primes that the LongBuffer object can hold
corresponds to longBuf.capacity() . You can transfer this number of primes to the buffer as long as
there is that many left in the array still to be written to the file, so you transfer a block of primes to the
buffer like this:
longBuf.put(primes, // Array to be written
primesWritten, // Index of 1st
element to write
min(longBuf.capacity(), primes.length -
primesWritten));
The first argument to the put() method is the array that is the source of the data, and the second argument
is the index position of the first element to be transferred. The third argument is the capacity of the buffer
as long as there is more than that number of primes still in the array. If there is less than this number on
the last iteration, you transfer primes.length-primesWritten values to the buffer.
Because you are using a relative put operation, loading the view buffer changes the position for that buf-
fer to reflect the number of values transferred to it. However, the backing byte buffer that you use in the
channel write operation still has its limit and position unchanged. You therefore set the limit for the byte
buffer with the statement:
buf.limit(8*longBuf.position());
Each prime occupies 8 bytes, so multiplying the position value for the view buffer by 8 gives you the
number of bytes occupied in the primary buffer. You then go ahead and write that buffer to the file and in-
crement primesWritten by the position value for the view buffer, because this corresponds to the num-
ber of primes that were written. Before the next iteration, you call clear() for both buffers to reset their
positions and limits to their original states — to 0 and the capacity, respectively. When you have written
all the primes, the loop ends and you output the length of the file. The channel closes automatically be-
cause it was created within the try block with resources.
Because this file contains binary data, you will not want to view it except perhaps for debugging pur-
poses.
Writing Mixed Data to a File
Sometimes, you might want to write more than one kind of binary data to a file. You might want to mix
integers with floating-point values with text for example. The text might be descriptive information about
each floating-point value, and there could be an integer sequence number. Generally you will want to write
floating-point data as binary because the character representation of a value may not be accurate and of
course it often makes sense to write text as text.
One way to do this is to use multiple view buffers. You can get an idea of how this works by outputting
some text along with each binary prime value in the previous example. Rather than taking the easy route
by just writing the same text for each prime value, let's add a character representation of the prime value
preceding each binary value. You add something like "prime = nnn" ahead of each binary value.
The first point to keep in mind is that if you ever want to read the file successfully, you can't just dump
strings of varying lengths in it. You would have no way to tell where the text ended and where the binary
Search WWH ::




Custom Search