FileDocCategorySizeDatePackage
DataInputStream.javaAPI DocJ2ME CLDC 1.119458Wed Feb 05 15:55:58 GMT 2003java.io

DataInputStream

public class DataInputStream extends InputStream implements DataInput
A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way. An application uses a data output stream to write data that can later be read by a data input stream.
author
unascribed
version
12/17/01 (CLDC 1.1)
see
java.io.DataOutputStream
since
JDK1.0, CLDC 1.0

Fields Summary
protected InputStream
in
The input stream.
Constructors Summary
public DataInputStream(InputStream in)
Creates a DataInputStream and saves its argument, the input stream in, for later use.

param
in the input stream.

        this.in = in;
    
Methods Summary
public intavailable()
Returns the number of bytes that can be read from this input stream without blocking.

This method simply performs in.available() and returns the result.

return
the number of bytes that can be read from the input stream without blocking.
exception
IOException if an I/O error occurs.

        return in.available();
    
public voidclose()
Closes this input stream and releases any system resources associated with the stream. This method simply performs in.close().

exception
IOException if an I/O error occurs.

        in.close();
    
public synchronized voidmark(int readlimit)
Marks the current position in this input stream. A subsequent call to the reset method repositions this stream at the last marked position so that subsequent reads re-read the same bytes.

The readlimit argument tells this input stream to allow that many bytes to be read before the mark position gets invalidated.

This method simply performs in.mark(readlimit).

param
readlimit the maximum limit of bytes that can be read before the mark position becomes invalid.

        in.mark(readlimit);
    
public booleanmarkSupported()
Tests if this input stream supports the mark and reset methods. This method simply performs in.markSupported().

return
true if this stream type supports the mark and reset method; false otherwise.

        return in.markSupported();
    
public intread()
Reads the next byte of data from this input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

This method simply performs in.read() and returns the result.

return
the next byte of data, or -1 if the end of the stream is reached.
exception
IOException if an I/O error occurs.

        return in.read();
    
public final intread(byte[] b)
See the general contract of the read method of DataInput.

Bytes for this operation are read from the contained input stream.

param
b the buffer into which the data is read.
return
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
exception
IOException if an I/O error occurs.
see
java.io.InputStream#read(byte[], int, int)

        return in.read(b, 0, b.length);
    
public final intread(byte[] b, int off, int len)
Reads up to len bytes of data from this input stream into an array of bytes. This method blocks until some input is available.

This method simply performs in.read(b, off, len) and returns the result.

param
b the buffer into which the data is read.
param
off the start offset of the data.
param
len the maximum number of bytes read.
return
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
exception
IOException if an I/O error occurs.

        return in.read(b, off, len);
    
public final booleanreadBoolean()
See the general contract of the readBoolean method of DataInput.

Bytes for this operation are read from the contained input stream.

return
the boolean value read.
exception
EOFException if this input stream has reached the end.
exception
IOException if an I/O error occurs.

        int ch = read();
        if (ch < 0) {
            throw new EOFException();
        }
        return (ch != 0);
    
public final bytereadByte()
See the general contract of the readByte method of DataInput.

Bytes for this operation are read from the contained input stream.

return
the next byte of this input stream as a signed 8-bit byte.
exception
EOFException if this input stream has reached the end.
exception
IOException if an I/O error occurs.

        int ch = read();
        if (ch < 0) {
            throw new EOFException();
        }
        return (byte)(ch);
    
public final charreadChar()
See the general contract of the readChar method of DataInput.

Bytes for this operation are read from the contained input stream.

return
the next two bytes of this input stream as a Unicode character.
exception
EOFException if this input stream reaches the end before reading two bytes.
exception
IOException if an I/O error occurs.

        return (char)readUnsignedShort();
    
public final doublereadDouble()
See the general contract of the readDouble method of DataInput.

Bytes for this operation are read from the contained input stream.

return
the next eight bytes of this input stream, interpreted as a double.
exception
EOFException if this input stream reaches the end before reading eight bytes.
exception
IOException if an I/O error occurs.
see
java.io.DataInputStream#readLong()
see
java.lang.Double#longBitsToDouble(long)
since
CLDC 1.1

        return Double.longBitsToDouble(readLong());
    
public final floatreadFloat()
See the general contract of the readFloat method of DataInput.

Bytes for this operation are read from the contained input stream.

return
the next four bytes of this input stream, interpreted as a float.
exception
EOFException if this input stream reaches the end before reading four bytes.
exception
IOException if an I/O error occurs.
see
java.io.DataInputStream#readInt()
see
java.lang.Float#intBitsToFloat(int)
since
CLDC 1.1

        return Float.intBitsToFloat(readInt());
    
public final voidreadFully(byte[] b)
See the general contract of the readFully method of DataInput.

Bytes for this operation are read from the contained input stream.

param
b the buffer into which the data is read.
exception
EOFException if this input stream reaches the end before reading all the bytes.
exception
IOException if an I/O error occurs.

        readFully(b, 0, b.length);
    
public final voidreadFully(byte[] b, int off, int len)
See the general contract of the readFully method of DataInput.

Bytes for this operation are read from the contained input stream.

param
b the buffer into which the data is read.
param
off the start offset of the data.
param
len the number of bytes to read.
exception
EOFException if this input stream reaches the end before reading all the bytes.
exception
IOException if an I/O error occurs.

        if (len < 0) {
            throw new IndexOutOfBoundsException();
        }
        int n = 0;
        while (n < len) {
            int count = read(b, off + n, len - n);
            if (count < 0) {
                throw new EOFException();
            }
            n += count;
        }
    
public final intreadInt()
See the general contract of the readInt method of DataInput.

Bytes for this operation are read from the contained input stream.

return
the next four bytes of this input stream, interpreted as an int.
exception
EOFException if this input stream reaches the end before reading four bytes.
exception
IOException if an I/O error occurs.

        int ch1 = read();
        int ch2 = read();
        int ch3 = read();
        int ch4 = read();
        if ((ch1 | ch2 | ch3 | ch4) < 0) {
             throw new EOFException();
        }
        return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
    
public final longreadLong()
See the general contract of the readLong method of DataInput.

Bytes for this operation are read from the contained input stream.

return
the next eight bytes of this input stream, interpreted as a long.
exception
EOFException if this input stream reaches the end before reading eight bytes.
exception
IOException if an I/O error occurs.

        return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL);
    
public final shortreadShort()
See the general contract of the readShort method of DataInput.

Bytes for this operation are read from the contained input stream.

return
the next two bytes of this input stream, interpreted as a signed 16-bit number.
exception
EOFException if this input stream reaches the end before reading two bytes.
exception
IOException if an I/O error occurs.

        return (short)readUnsignedShort();
    
public final java.lang.StringreadUTF()
See the general contract of the readUTF method of DataInput.

Bytes for this operation are read from the contained input stream.

return
a Unicode string.
exception
EOFException if this input stream reaches the end before reading all the bytes.
exception
IOException if an I/O error occurs.
see
java.io.DataInputStream#readUTF(java.io.DataInput)

        return readUTF(this);
    
public static final java.lang.StringreadUTF(java.io.DataInput in)
Reads from the stream in a representation of a Unicode character string encoded in Java modified UTF-8 format; this string of characters is then returned as a String. The details of the modified UTF-8 representation are exactly the same as for the readUTF method of DataInput.

param
in a data input stream.
return
a Unicode string.
exception
EOFException if the input stream reaches the end before all the bytes.
exception
IOException if an I/O error occurs.
exception
UTFDataFormatException if the bytes do not represent a valid UTF-8 encoding of a Unicode string.
see
java.io.DataInputStream#readUnsignedShort()

        int utflen = in.readUnsignedShort();
        char str[] = new char[utflen];
        byte bytearr [] = new byte[utflen];
        int c, char2, char3;
        int count = 0;
        int strlen = 0;

        in.readFully(bytearr, 0, utflen);

        while (count < utflen) {
            c = (int) bytearr[count] & 0xff;
            switch (c >> 4) {
                case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
                    /* 0xxxxxxx*/
                    count++;
                    str[strlen++] = (char)c;
                    break;
                case 12: case 13:
                    /* 110x xxxx   10xx xxxx*/
                    count += 2;
                    if (count > utflen)
                        throw new UTFDataFormatException();
                    char2 = (int) bytearr[count-1];
                    if ((char2 & 0xC0) != 0x80)
                        throw new UTFDataFormatException();
                    str[strlen++] = (char)(((c & 0x1F) << 6) | (char2 & 0x3F));
                    break;
                case 14:
                    /* 1110 xxxx  10xx xxxx  10xx xxxx */
                    count += 3;
                    if (count > utflen)
                        throw new UTFDataFormatException();
                    char2 = (int) bytearr[count-2];
                    char3 = (int) bytearr[count-1];
                    if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
                        throw new UTFDataFormatException();
                    str[strlen++] = (char)(((c     & 0x0F) << 12) |
                                           ((char2 & 0x3F) << 6)  |
                                           ((char3 & 0x3F) << 0));
                    break;
                default:
                    /* 10xx xxxx,  1111 xxxx */
                    throw new UTFDataFormatException();
                }
        }
        // The number of chars produced may be less than utflen
        return new String(str, 0, strlen);
    
public final intreadUnsignedByte()
See the general contract of the readUnsignedByte method of DataInput.

Bytes for this operation are read from the contained input stream.

return
the next byte of this input stream, interpreted as an unsigned 8-bit number.
exception
EOFException if this input stream has reached the end.
exception
IOException if an I/O error occurs.

        int ch = read();
        if (ch < 0) {
            throw new EOFException();
        }
        return ch;
    
public final intreadUnsignedShort()
See the general contract of the readUnsignedShort method of DataInput.

Bytes for this operation are read from the contained input stream.

return
the next two bytes of this input stream, interpreted as an unsigned 16-bit integer.
exception
EOFException if this input stream reaches the end before reading two bytes.
exception
IOException if an I/O error occurs.

        int ch1 = read();
        int ch2 = read();
        if ((ch1 | ch2) < 0) {
             throw new EOFException();
        }
        return (ch1 << 8) + (ch2 << 0);
    
public synchronized voidreset()
Repositions this stream to the position at the time the mark method was last called on this input stream.

This method simply performs in.reset().

Stream marks are intended to be used in situations where you need to read ahead a little to see what's in the stream. Often this is most easily done by invoking some general parser. If the stream is of the type handled by the parse, it just chugs along happily. If the stream is not of that type, the parser should toss an exception when it fails. If this happens within readlimit bytes, it allows the outer code to reset the stream and try another parser.

exception
IOException if the stream has not been marked or if the mark has been invalidated.

        in.reset();
    
public longskip(long n)
Skips over and discards n bytes of data from the input stream. The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. The actual number of bytes skipped is returned.

This method simply performs in.skip(n).

param
n the number of bytes to be skipped.
return
the actual number of bytes skipped.
exception
IOException if an I/O error occurs.

        return in.skip(n);
    
public final intskipBytes(int n)
See the general contract of the skipBytes method of DataInput.

Bytes for this operation are read from the contained input stream.

param
n the number of bytes to be skipped.
return
the actual number of bytes skipped.
exception
IOException if an I/O error occurs.

        int total = 0;
        int cur = 0;

        while ((total<n) && ((cur = (int) skip(n-total)) > 0)) {
            total += cur;
        }
        return total;