Java Tutorial - Java Script : Channels

Java Tutorial - Java Script :

Channels

A common use for a buffer is to associate it with an input or output stream. You can fill a buffer with data from an input stream or write a buffer to an output stream. To do this, you must use a channel, an object that connects a buffer to the stream. Channels are part of the java.nio.channels package. Channels can be associated with a stream by calling the getChannel() method available in some of the stream classes in the java.io package. The FileInputStream and FileOutputStream classes have getChannel() methods that return a FileChannel object. This file channel can be used to read, write, and modify the data in the file. The following statements create a file input stream and a channel associated with that
file:
try {
String source = “prices.dat”;
FileInputStream inSource = new FileInputStream(source);
FileChannel inChannel = inSource.getChannel();
} catch (FileNotFoundException fne) {
System.out.println(fne.getMessage());
}
After you have created the file channel, you can find out how many bytes the file contains by calling its size() method. This is necessary if you want to create a byte buffer to hold the contents of the file. Bytes are read from a channel into a ByteBuffer with the read(ByteBuffer, long) method. The first argument is the buffer. The second argument is the current position in the buffer, which determines where the file’s contents will begin to be stored. The following statements extend the last example by reading a file into a byte buffer using the inChannel file channel:
long inSize = inChannel.size();
ByteBuffer data = ByteBuffer.allocate( (int)inSize );
inChannel.read(data, 0);
data.position(0);
for (int i = 0; data.remaining() > 0; i++)
System.out.print(data.get() + “ “);
The attempt to read from the channel generates an IOException error if a problem occurs. Although the byte buffer is the same size as the file, this isn’t a requirement. If you are reading the file into the buffer so that you can modify it, you can allocate a larger buffer. The next project you undertake incorporates the new input/output features you have learned about so far: buffers, character sets, and channels. The BufferConverter application reads a small file into a byte buffer, displays the contents of the buffer, converts it to a character buffer, and then displays the characters.Enter the text of Listing 17.4 and save it as BufferConverter.java.
After you compile the file, you need a copy of friends.dat, the small file of byte data used in the application. To download it from the book’s website at http://www. java21days.com, open the Day 17 page, click the friends.dat hyperlink, and save the file in the same place as BufferConverter.class.
If you use the copy of friends.dat from the book’s website, the output of the BufferConverter application is the following:
Original byte data:
70 114 105 101 110 100 115 44 32 82 111 109 97 110 115 44 32 99 111 117 110 116 114 121 109 101 110 44 32 108 101 110 100 32 109 101 32 121 111 117 114 32 101 97 114 115 46 13 10 13 10
New character data:
Friends, Romans, countrymen, lend me your ears.
The BufferConverter application uses the techniques introduced today to read data and represent it as bytes and characters, but you could have accomplished the same thing with the old input/output package, java.io. For this reason, you might wonder why it’s worth learning the new package at all. One reason is that buffers enable you to manipulate large amounts of data much more quickly. You’ll find out another reason in the next section.