Java Reference
In-Depth Information
If you use the copy of friends.dat from the topic'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.
Network Channels
A popular feature of the java.nio package is its support for nonblocking input and out-
put over a networking connection.
In Java, blocking refers to a statement that must complete execution before anything else
happens in the program. All the socket programming you have done up to this point has
used blocking methods exclusively. For example, in the TimeServer application, when
the server socket's accept() method is called, nothing else happens in the program until
a client makes a connection.
As you can imagine, it's problematic for a networking program to wait until a particular
statement is executed because numerous things can go wrong. Connections can be bro-
ken. A server could go offline. A socket connection could appear to be stalled because a
blocked statement is waiting for something to happen.
For example, a client application that reads and buffers data over HTTP might be waiting
for a buffer to be filled even though no more data remains to be sent. The program will
appear to have halted because the blocked statement never finishes executing.
With the java.nio package, you can create networking connections and read and write
from them using nonblocking methods.
Search WWH ::




Custom Search