23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

UDP<br />

Byte array input and output streams are commonly used when sending and receiving<br />

UDP data over the Internet. Unlike the more common TCP data, which acts like the<br />

streams I discuss in this book, UDP data arrives in raw packets of bytes, which do<br />

not necessarily have any relation to the previous packet or the next packet. Each<br />

packet is just a group of bytes to be processed in isolation from other packets. Thus,<br />

you may get nothing for several seconds, or even minutes, and then suddenly have a<br />

few hundred numbers to deal with.<br />

In <strong>Java</strong>, UDP data is sent and received via the java.net.DatagramSocket and<br />

java.net.DatagramPacket classes. The receive() method of the<br />

DatagramSocket class returns its data in a DatagramPacket, which is little more<br />

than a wrapper around a byte array. This byte array can be easily used as the source<br />

of a ByteArrayInputStream . UDP is discussed in more detail in Chapter 9 of my<br />

book <strong>Java</strong> Network Programming (O'Reilly & Associates, 1997).<br />

8.2.1 Byte Array Input Streams<br />

<strong>Java</strong> I/O<br />

The ByteArrayInputStream class reads data from a byte array using the methods of<br />

java.io.InputStream :<br />

public class ByteArrayInputStream extends InputStream<br />

There are two ByteArrayInputStream() constructors. Both take a byte array as an<br />

argument. This byte array is the buffer from which data will be read. The first constructor<br />

uses the entire buffer array as an input stream. The second constructor only uses the subarray<br />

of length bytes of buffer starting with the byte at offset.<br />

public ByteArrayInputStream(byte[] buffer)<br />

public ByteArrayInputStream(byte[] buffer, int offset, int length)<br />

Other than these two constructors, the ByteArrayInputStream class just has the usual<br />

read(), available(), close(), mark(), and reset() methods. Byte array input streams do<br />

support marking and resetting up to the full length of the stream. This is relatively<br />

straightforward to implement, because a byte array contains all the data in the stream in<br />

memory at any time. There's no need to implement special buffering as with other kinds of<br />

streams and no need to worry that you'll try to reset further back than the buffer allows.<br />

8.2.2 Byte Array Output Streams<br />

The ByteArrayOutputStream class writes data into the successive components of a byte<br />

array using the methods of java.io.OutputStream :<br />

public class ByteArrayOutputStream extends OutputStream<br />

This class has the following two constructors, plus the usual write(), close(), and flush()<br />

methods:<br />

133

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!