Java Reference
In-Depth Information
compact the buffer, and the reference to buf that is returned is passed to the read() method for inCh to
read data from the file. You then flip the buffer and get the length of the string. Of course, data in the
file should be in groups of three items — the string length, the string, the binary prime value — so the
end-of-file should be detected trying to obtain the first of these by the read() method for the channel
returning −1. In this case you exit the loop by executing a break statement.
Next you get the string itself, after checking that you have sufficient bytes left in the buffer. You should
never find EOF, so you output an error message if EOF is detected. Finally, you obtain the binary prime
value in a similar way and output the group of three data items. The loop continues until all data has been
read and processed and EOF is recognized when you are looking for a string length value.
COPYING FILES
You have already seen in Chapter 9 how you can use the copy() method in the Files class to copy a file
to another location. There's another way using FileChannel objects. A FileChannel object connected to
an input file can transfer data directly to a FileChannel object that is connected to an output file without
involving explicit buffers.
The FileChannel class defines two methods for direct data transfer between channels that are particu-
larly efficient.
transferTo(long position, long count, WritableByteChannel dst) Attempts to transfer
count bytes from this channel to the channel dst . Bytes are read from this channel starting at the
file position specified by position . The position of this channel is not altered by this operation,
but the position of dst is incremented by the number of bytes written. Fewer than count bytes are
transferred if this channel's file has fewer than count bytes remaining, or if dst is non-blocking
and has fewer than count bytes free in its system output buffer. The number of bytes transferred
is returned as a value of type int .
transferFrom(ReadableByteChannel src, long position, long count)) Attempts to
transfer count bytes to this channel from the channel src . Bytes are written to this channel start-
ing at the file position specified by position . The position of this channel is not altered by the
operation, but the position of src is incremented by the number of bytes read from it. If position
is greater than the size of the file, then no bytes are transferred. Fewer than count bytes are trans-
ferred if the file corresponding to src has fewer than count bytes remaining in the file or if it is
non-blocking and has fewer than count bytes free in its system input buffer. The number of bytes
transferred is returned as a value of type int .
A channel that was opened for reading supports only the transferTo() method. Similarly, a channel that
was opened for writing supports only the transferFrom() method. Both of these methods can throw any
of the following flurry of exceptions:
IllegalArgumentException is thrown if either count or position is negative.
NonReadableChannelException is thrown if the operation attempts to read from a channel that
was not opened for reading.
NonWritableChannelException is thrown if the operation attempts to write to a channel that was
not opened for writing.
ClosedChannelException is thrown if either channel involved in the operation is closed.
Search WWH ::




Custom Search