Byte Arrays

As already mentioned, the DataInputStream class has the usual two methods for reading bytes into a byte array:

public int read(byte[] data) throws IOException
public int read(byte[] data, int offset, int length) throws IOException

Neither of these methods guarantees that all of the bytes requested will be read. Instead, you're expected to check the number of bytes actually read and then call read( ) again as necessary for different parts of the array. For example, to read 1024 bytes from the InputStream in into the byte array data:

int offset = 0;
while (true){
 int bytesRead = in.read(data, offset, data.length - offset);
 offset += bytesRead;
 if (bytesRead == -1 || offset >= data.length) break;
}

The DataInputStream class has two readFully( ) methods that provide this logic. Each reads repeatedly from the underlying input stream until the array data or a specified portion thereof is filled.

public final void readFully(byte[] data) throws IOException
public final void readFully(byte[] data, int offset, int length)
 throws IOException

If the data runs out before the array is filled and no more data is forthcoming, an IOException is thrown.

8.5.1. Determining the Number of Bytes Written

The DataOutputStream class has a protected field called written that stores the number of bytes written to the output stream using any of its methods since the point it was constructed. The value of this field is returned by the public size( ) method:

protected int written
public final int size( )

Every time you invoke writeInt( ), writeBytes( ) , writeUTF( ) , or some other write method, the written field is incremented by the number of bytes written. This might be useful if for some reason you're trying to limit the number of bytes you write. For instance, you may prefer to open a new file when you reach some preset size rather than continuing to write into a very large file.

8.5.2. Skipping Bytes

The DataInputStream class's skipBytes( ) method skips over a specified number of bytes without reading them. Unlike the skip( ) method of java.io.InputStream that DataInputStream inherits, skipBytes( ) either skips over all of the bytes it's asked to skip or it throws an exception:

public final int skipBytes(int n) throws IOException

skipBytes( ) blocks and waits for more data until n bytes have been skipped (successful execution) or until an exception is thrown. The method returns the number of bytes skipped, which is always n (because if it's not n, an exception is thrown and nothing is returned). On end of stream, it throws an EOFException. It throws an IOException if the underlying stream throws an IOException.

Basic I/O

Introducing I/O

Output Streams

Input Streams

Data Sources

File Streams

Network Streams

Filter Streams

Filter Streams

Print Streams

Data Streams

Streams in Memory

Compressing Streams

JAR Archives

Cryptographic Streams

Object Serialization

New I/O

Buffers

Channels

Nonblocking I/O

The File System

Working with Files

File Dialogs and Choosers

Text

Character Sets and Unicode

Readers and Writers

Formatted I/O with java.text

Devices

The Java Communications API

USB

The J2ME Generic Connection Framework

Bluetooth

Character Sets



Java I/O
Java I/O
ISBN: 0596527500
EAN: 2147483647
Year: 2004
Pages: 244

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net