Java IO Learning (14) DataOutputStream's knowledge, source code, and example

Source: Internet
Author: User

DataOutputStream (data output stream) 's cognition, source code and example

This chapter describes DataOutputStream. We first have a general understanding of DataOutputStream, and then in-depth study of its source code, and finally through the example to deepen its understanding.

DataOutputStream Introduction

DataOutputStream is the data output stream. It inherits from Filteroutputstream.

The dataoutputstream is used to decorate other output streams and to work with dataoutputstream and datainputstream input streams, "allowing applications to read and write basic Java data types from the underlying input stream in a machine-independent manner."

DataOutputStream Source Analysis (based on jdk1.7.40)

Package java.io; public class DataOutputStream extends Filteroutputstream implements DataOutput {//Data output stream bytes protected int writ
     
    Ten
     
    "Data output stream" corresponds to the byte array private byte[] Bytearr = null;
    Constructor public DataOutputStream (OutputStream out) {super (out);
        }//Add "output value" private void Inccount (int value) {int temp = written + value;
        if (Temp < 0) {temp = Integer.max_value;
    } written = temp;
        Writes the value of type int to the data output stream, public synchronized void write (int b) throws IOException {out.write (b);
    Inccount (1); ///Writes Len bytes of byte array B from off to the data output stream, public synchronized void write (byte b[], int off, int len) thr
        oWS IOException {out.write (b, off, Len);
    Inccount (len);
    //empty buffer, the data in the buffer will be written to the output stream public void flush () throws IOException {Out.flush ();
 //Writes a Boolean value to the data output stream   Public final void Writeboolean (Boolean v) throws IOException {Out.write (v 1:0);
    Inccount (1);
        //Writes a value of byte to the public final void writebyte (int v) throws IOException {Out.write (v) in the data output stream;
    Inccount (1);
        //Writes the value of type short to the data output flow//Note: Short is 2 bytes public final void Writeshort (int v) throws IOException {
        Write short high 8-bit byte out.write ((v >>> 8) & 0xFF);
        Writes short low 8 bit corresponding byte Out.write ((v >>> 0) & 0xFF);
    Inccount (2);
        //write value of char type to data output stream Note: Char occupies 2 bytes public final void Writechar (int v) throws IOException {
        Writes char high 8 bits corresponding byte Out.write ((v >>> 8) & 0xFF);
        Writes char low 8 bit corresponding byte Out.write ((v >>> 0) & 0xFF);
    Inccount (2); //write value of type int to data output stream Note: Int occupies 4 bytes public final void Writeint (int v) throws IOException {O Ut.write ((v);>>) & 0xFF);
        Out.write ((v >>>) & 0xFF);
        Out.write ((v >>> 8) & 0xFF);
        Out.write ((v >>> 0) & 0xFF);
    Inccount (4);
     
    Private byte writebuffer[] = new BYTE[8]; Writes a long value to the data output stream//Note: Long is 8 bytes public final void Writelong (Long v) throws IOException {Writebuff
        Er[0] = (byte) (v >>> 56);
        WRITEBUFFER[1] = (byte) (v >>> 48);
        WRITEBUFFER[2] = (byte) (v >>> 40);
        WRITEBUFFER[3] = (byte) (v >>> 32);
        WRITEBUFFER[4] = (byte) (v >>> 24);
        WRITEBUFFER[5] = (byte) (v >>> 16);
        WRITEBUFFER[6] = (byte) (v >>> 8);
        WRITEBUFFER[7] = (byte) (v >>> 0);
        Out.write (WriteBuffer, 0, 8);
    Inccount (8); //Writes float values to the data output stream public final void writefloat (float v) throws IOException {Writeint (float
    . Floattointbits (v));//Writes a double value to the data output stream public final void writedouble (Double v) throws IOException {Writelong (D
    Ouble.doubletolongbits (v));
    //Writes a string of values to the data output stream//actual write, converts each character of string to byte data and writes to the output stream.
        Public final void Writebytes (String s) throws IOException {int len = s.length ();
        for (int i = 0; i < len; i++) {Out.write ((byte) S.charat (i));
    Inccount (len);
    //Writes a string of values to the data output stream//actual write, converts each character of string to char data and writes to the output stream.
        Public final void WriteChars (String s) throws IOException {int len = s.length ();
            for (int i = 0; i < len; i++) {int v = s.charat (i);
            Out.write ((v >>> 8) & 0xFF);
        Out.write ((v >>> 0) & 0xFF);
    Inccount (len * 2);  //Writes the value of the UTF-8 type to the data output stream in the public final void writeUTF (String str) throws IOException {writeUTF (str,
    this);
   }  
    Writes string data as a UTF-8 type to the static int writeUTF (String str, dataoutput out) throws IOException {
        Gets the length of the string int strlen = Str.length ();
        int utflen = 0;
     
        int C, count = 0;
        Because the UTF-8 is 1~4 bytes,//Here, according to the UTF-8 first byte range, the UTF-8 is a few bytes.
            for (int i = 0; i < strlen i++) {c = Str.charat (i);
            if ((c >= 0x0001) && (c <= 0x007f)) {utflen++;
            else if (C > 0x07ff) {utflen + = 3;
            else {Utflen = 2; } if (Utflen > 65535) throw new Utfdataformatexception ("encoded ST
     
        Ring too long: "+ Utflen +" bytes "); New "byte array Bytearr"//view this column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/programming/java/byte[] Bytearr
        = NULL; if (out instanceof DataOutputStream) {DataOutputStream dos = (dataoutputstrEAM) out; if (Dos.bytearr = null | |
                (Dos.bytearr.length < (utflen+2))
            Dos.bytearr = new byte[(utflen*2) + 2];
        Bytearr = Dos.bytearr;
        else {Bytearr = new byte[utflen+2];
        The first 2 bytes of the//"byte array" are saved with "UTF-8 data Length" bytearr[count++] = (byte) (Utflen >>> 8) & 0xFF);
     
        bytearr[count++] = (byte) ((Utflen >>> 0) & 0xFF);
        Preprocessing the single byte data in the UTF-8 int i=0;
           for (i=0; i<strlen; i++) {c = Str.charat (i); if (!) (
           (c >= 0x0001) && (c <= 0x007f))) break;
        bytearr[count++] = (byte) c;
            //The preprocessed data, followed by processing for (; i < strlen; i++) {c = Str.charat (i); UTF-8 data is 1 bytes if ((C >= 0x0001) && (c <= 0x007f)) {bytearr[count++] = (byt
     
            e) C;
   else if (C > 0x07ff) {//UTF-8 data is 3 bytes             bytearr[count++] = (byte) (0xe0 |
                ((c >>) & 0x0f)); bytearr[count++] = (byte) (0x80 |
                ((c >> 6) & 0x3F)); bytearr[count++] = (byte) (0x80 |
            ((c >> 0) & 0x3F)); else {//UTF-8 data is 2 bytes bytearr[count++] = (byte) (0xc0 |
                ((c >> 6) & 0x1F)); bytearr[count++] = (byte) (0x80 |
            ((c >> 0) & 0x3F));
        The byte array is written to the data output stream Out.write (Bytearr, 0, utflen+2);
    return Utflen + 2;
    Public final int size () {return written; }
}

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.