Java IO Learning (13) data input stream cognition, source code and example

Source: Internet
Author: User
Tags 0xc0 final range readline

DataInputStream (data input stream), source and example

DataInputStream Introduction

DataInputStream is the data input stream. It inherits from FilterInputStream.

DataInputStream is used to decorate other input streams, which "allows applications to read basic Java data types from the underlying input stream in a machine-independent manner." Applications can use DataOutputStream (data output streams) to write data that is read by the DataInputStream (data input stream).

DataInputStream Function List

DataInputStream (InputStream in)
final int     read (byte[] buffer, int offset, int length)
final int     Read ( Byte[] Final
boolean     Readboolean () Final
byte     readbyte () final
char     Readchar () Final
Double     readdouble () Final
float     readfloat () final
void     readfully (byte[] DST) Final
void     readfully (byte[] DST, int offset, int byteCount)
final int     readInt ()
final String     ReadLine () Final
long     Readlong () final short     Readshort () final
static String     readUTF (Datainput in)
final String     readUTF () final
int     readunsignedbyte ()
final int     Readunsignedshort ()
final int     skipbytes (int count)

Datainputstream.java Source Analysis (based on jdk1.7.40)

Package java.io;
    public class DataInputStream extends FilterInputStream implements Datainput {//constructor.
    Public DataInputStream (InputStream in) {super (in);
    Private byte bytearr[] = new BYTE[80];
     
    Private char chararr[] = new CHAR[80];
    Reads a byte from the data entry stream public final int read (byte b[]) throws IOException {return In.read (b, 0, b.length);
    //Read the data from the data entry stream and store it in byte array B.
    Off is the starting position of the starting storage element in byte array B.
    Len is the number of bytes read.
    Public final int Read (byte b[], int out, int len) throws IOException {return In.read (b, off, Len);
    Read the data from the data entry stream and fill in byte array b, and do not fill the array B until it fills the position.
    Store from position 0 of byte array b, and the number of bytes read equals the length of B.
    Public final void readfully (Byte b[]) throws IOException {readfully (b, 0, b.length);
    ///reads data from the data entry stream and stores it in byte array b, if Len bytes are not read until the Len Byte is read.
     Public final void readfully (byte b[], int off, int len) throws IOException {if (len < 0)       throw new Indexoutofboundsexception ();
        int n = 0;
            while (n < len) {int count = In.read (b, off + N, len-n);
            if (count < 0) throw new eofexception ();
        n + = count;
        }//Skip n bytes Public final int skipbytes (int n) throws IOException {int total = 0;
     
        int cur = 0;
        while ((Total<n) && ((cur = (int) in.skip (n-total)) > 0)} {total = cur;
    Return to total; ///Read Boolean value from data entry stream public final Boolean Readboolean () throws ioexception {int ch = in.read
        ();
        if (Ch < 0) throw new eofexception ();
    return (ch!= 0);
        ///Read byte of value from data input stream public final byte readbyte () throws ioexception {int ch = in.read ();
        if (Ch < 0) throw new eofexception ();
    Return (BYTE) (CH); //read "unsigned byte" from "Data entry stream"The value of type, that is, the byte value that reads the positive value is public final int readunsignedbyte () throws ioexception {int ch = in.read ();
        if (Ch < 0) throw new eofexception ();
    return ch; 
        ///Read the value of "short type" from "Data entry Flow" public final short Readshort () throws IOException {int ch1 = In.read ();
        int CH2 = In.read ();
        if ((Ch1 | ch2) < 0) throw new eofexception ();
    Return (short) ((CH1 << 8) + (CH2 << 0)); The value public final int readunsignedshort () throws IOException {int ch1 = read "Unsigned short type" from "Data input stream"
        In.read ();
        int CH2 = In.read ();
        if ((Ch1 | ch2) < 0) throw new eofexception ();
    Return (CH1 << 8) + (CH2 << 0);
        ///Read the value of "char type" from "Data entry Flow" public final char Readchar () throws IOException {int ch1 = In.read ();
        int CH2 = In.read ();
        if ((Ch1 | ch2) < 0) throw new eofexception (); ReturN (Char) ((CH1 << 8) + (CH2 << 0));
        ///read "int type" value from "Data entry Flow" public final int readInt () throws IOException {int ch1 = In.read ();
        int CH2 = In.read ();
        int CH3 = In.read ();
        int CH4 = In.read ();
        if ((ch1 | ch2 | CH3 | CH4) < 0) throw new eofexception ();
    Return ((ch1 <<) + (CH2 <<) + (CH3 << 8) + (CH4 << 0));
     
    Private byte readbuffer[] = new BYTE[8];
        Read the value "Long" from the data entry stream public final long Readlong () throws IOException {readfully (readbuffer, 0, 8);
                Return ((Long) readbuffer[0] << + ((long) (readbuffer[1) & 255) << 48) +
                ((Long) (Readbuffer[2] & 255) <<) + ((long) (Readbuffer[3] & 255) << 32) +
                ((Long) (Readbuffer[4] & 255) << ((readbuffer[5) & 255) << 16) + ((ReADBUFFER[6] & 255 << 8) + ((Readbuffer[7] & 255) << 0)); ///Read the value of float type from the data entry stream public final float readfloat () throws IOException {return float.intbits
    Tofloat (ReadInt ()); ///Read the value of "double type" from "Data input stream" public final double readdouble () throws IOException {return Double.lon
    Gbitstodouble (Readlong ());
     
    Private Char linebuffer[];
     
        @Deprecated Public final String ReadLine () throws IOException {char buf[] = Linebuffer;
        if (buf = = null) {buf = Linebuffer = new char[128];
        int room = Buf.length;
        int offset = 0;
     
int C;
                Loop:while (TRUE) {switch (c = in.read ()) {case-1: case ' \ n ':
     
              Break loop;
                Case ' \ r ': int c2 = In.read ();
                   if ((C2!= ' \ n ') && (C2!=-1)) { if (!) (
                    In instanceof Pushbackinputstream)} {this.in = new pushbackinputstream (in);
                } ((Pushbackinputstream) in). Unread (C2);
     
              Break loop;
                    Default:if (--room < 0) {buf = new Char[offset + 128];
                    room = buf.length-offset-1;
                    System.arraycopy (linebuffer, 0, buf, 0, offset);
                Linebuffer = BUF;
                } buf[offset++] = (char) c;
            Break
        } if ((c = = 1) && (offset = 0)) {return null;
    Return string.copyvalueof (buf, 0, offset);
    ///Read the value of "UTF type" from "Data entry Flow" public final String readUTF () throws IOException {return readUTF (this); Public final static String readUTF (Datainput in) throws IOException {//Read the value of unsigned short type from the data entry stream://Note: The first 2 bytes of the UTF-8 input stream are the length of the data int utflen = In.readunsignedshort ();
        byte[] Bytearr = null;
     
        char[] Chararr = null;
        If in itself is a "data input stream", set the byte array Bytearr = Member of the data entry stream Bytearr//Set the character array Chararr = Member of the data entry stream Chararr Otherwise, the new array Bytearr and Chararr if (in instanceof datainputstream) {datainputstream dis = (datainputstre
            AM) in;
                if (Dis.bytearr.length < Utflen) {Dis.bytearr = new byte[utflen*2];
            Dis.chararr = new Char[utflen*2];
            } Chararr = Dis.chararr;
        Bytearr = Dis.bytearr;
            else {Bytearr = new Byte[utflen];
        Chararr = new Char[utflen];
        int C, CHAR2, CHAR3;
        int count = 0;
     
        int chararr_count=0;
        Reads data from the data entry stream and stores it in a byte array bytearr, starting at the Bytearr location 0 and storing the length utflen. Note that this is stored to an array of bytes!
        And it reads all the data.
     
 In.readfully (Bytearr, 0, Utflen);       Copy data from "byte array Bytearr" to "character array Chararr" Note: This corresponds to "single-byte symbols in a preprocessed input stream" because the UTF-8 is 1-4 bytes variable.
            while (Count < Utflen) {//converts each byte to int value c = (int) bytearr[count] & 0xFF;
            UTF-8 data has a value of no more than 127, so, more than 127, exits.
            if (C > 127) break;
            count++;
        Save C to the character array Chararr chararr[chararr_count++]= (char) C;
        After processing the single-byte symbols in the input stream, we proceed.
            while (Count < Utflen) {//The following statement performs a 2-step operation.
            (01) Converts the byte from "byte type" to "int type".
            For example, after "11001010" is converted to int, it is "00000000 00000000 00000000 11001010"//(02) Moves the data "int type" to the left 4 bit For example, "00000000 00000000 00000000 11001010" moves to the left 4 bit, then becomes "00000000 00000000 00000000 00001100" c = (int) by
            Tearr[count] & 0xFF; Switch (c >> 4) {//If UTF-8 is Single-byte, that is, Bytearr[count] corresponds to the "0xxxxxxx" form;//Then Bytearr[c Ount] corresponds to C of the type intThe range of values is 0-7. Case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:/* 0xxxxxxx*/Coun
                    t++;
                    Chararr[chararr_count++]= (char) C;
     
                Break If UTF-8 is a double byte, that is, Bytearr[count] corresponds to the first in the "110xxxxx 10xxxxxx" form, that is, "110xxxxx"/Then Bytearr[count] the int type
                The value range of C is 12-13.
                    Case 12:case:/* 110x xxxx 10xx xxxx*/count + 2; if (Count > Utflen) throw new Utfdataformatexception ("Malformed in
                    Put:partial character at end ");
                    CHAR2 = (int) bytearr[count-1]; if ((Char2 & 0xc0)!= 0x80) throw new Utfdataformatexception ("Mal
                    Formed input around byte "+ count);
   Chararr[chararr_count++]= (char) ((C & 0x1F) << 6) |                                                 (Char2 & 0x3F));
     
                Break If the UTF-8 is three bytes, that is, Bytearr[count] corresponds to the first in the "1110xxxx 10xxxxxx 10xxxxxx" form, that is, "1110xxxx"/Is Bytearr[count
                The value of C of the corresponding int type is 14.
                    Case: * 1110 xxxx 10xx xxxx 10xx xxxx/count + + 3; if (Count > Utflen) throw new Utfdataformatexception ("Malformed in
                    Put:partial character at end ");
                    CHAR2 = (int) bytearr[count-2];
                    CHAR3 = (int) bytearr[count-1]; if ((Char2 & 0xc0)!= 0x80) | |
                        ((Char3 & 0xc0)!= 0x80))
                    throw new Utfdataformatexception ("Malformed input around byte" + (count-1));
                                        Chararr[chararr_count++]= (char) ((C & 0x0f) << 12) |            ((Char2 & 0x3F) << 6) |
                    ((Char3 & 0x3F) << 0));
     
                Break If the UTF-8 is four bytes, that is, the bytearr[count] corresponds to the first in the "11110xxx 10xxxxxx 10xxxxxx 10xxxxxx" form, that is, "11110xxx"/byte
                    Arr[count] The value of C of the int type is default:/* 10xx xxxx, 1111 xxxx * *
            throw new Utfdataformatexception ("Malformed input around byte" + count); }//The number of chars produced May is less than Utflen return new String (Chararr, 0, Chararr_
    count); }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.