JAVA 상용 클래스 (IO 편) - Data Output Stream 상세 설명

22252 단어 io
글 은 읽 기와 이해 가 편리 합 니 다. 본 고 는 원본 코드 에서 번역 되 고 일정한 데이터 구조 / 알고리즘 기반 을 가 지 며 원본 코드 를 연구 하고 자 하 는 친구 가 읽 기 에 적합 합 니 다. 초보 자 는 무시 하 십시오.틀린 곳 을 지적 해 주 셔 서 감사합니다!
package java.io;
/**
 * DataOutputStream     java         .               
 */
public
class DataOutputStream extends FilterOutputStream implements DataOutput {
    /**
     *        byte  
     *   counter    ,      Integer.MAX_VALUE
     */
    protected int written;

    /**
     * bytearr    writeUTF   
     */
    private byte[] bytearr = null;

    /**
     *      data output stream        output stream   ,
     *   counter    0
     *
     * @param   out     output strean,           
     * @see     java.io.FilterOutputStream#out
     */
    public DataOutputStream(OutputStream out) {
        super(out);
    }

    /**
     *  written  value,    Integer.MAX_VALUE,written   *Integer.MAX_VALUE
     */
    private void incCount(int value) {
        int temp = written + value;
        if (temp < 0) {
            temp = Integer.MAX_VALUE;
        }
        written = temp;
    }

    /**
     *     byte    (  b  8       int  )  output stream .        ,    written   1
     * 

* OutputStram write * * @param b byte * @exception I/O I/O * @see java.io.FilterOutputStream#out */

public synchronized void write(int b) throws IOException { out.write(b); incCount(1); } /** * byte len byte output stream , byte offset。 written len * * @param b byte * @param off . * @param len byte . * @exception I/O I/O * @see java.io.FilterOutputStream#out */ public synchronized void write(byte b[], int off, int len) throws IOException { out.write(b, off, len); incCount(len); } /** * output stream, output stream * * DataOutputStream flush output stream flush * @exception I/O I/O * @see java.io.FilterOutputStream#out * @see java.io.OutputStream#flush() */ public void flush() throws IOException { out.flush(); } /** * output stream bool( 1 ), v true 1,false 0。 written 1 * * @param v bool * @exception I/O I/O * @see java.io.FilterOutputStream#out */ public final void writeBoolean(boolean v) throws IOException { out.write(v ? 1 : 0); incCount(1); } /** * output stream byte。 written 1 * * @param v * @exception I/O I/O * @see java.io.FilterOutputStream#out */ public final void writeByte(int v) throws IOException { out.write(v); incCount(1); } /** * output stream 2 byte(1 short)。 written 2 * * @param v short * @exception I/O I/O . * @see java.io.FilterOutputStream#out */ public final void writeShort(int v) throws IOException { out.write((v >>> 8) & 0xFF); out.write((v >>> 0) & 0xFF); incCount(2); } /** * char(2 byte), . written 2. * * @param v char * @exception I/O I/O . * @see java.io.FilterOutputStream#out */ public final void writeChar(int v) throws IOException { out.write((v >>> 8) & 0xFF); out.write((v >>> 0) & 0xFF); incCount(2); } /** * int(4 byte), . written 4. * @param v int * @exception I/O I/O . * @see java.io.FilterOutputStream#out */ public final void writeInt(int v) throws IOException { out.write((v >>> 24) & 0xFF); out.write((v >>> 16) & 0xFF); out.write((v >>> 8) & 0xFF); out.write((v >>> 0) & 0xFF); incCount(4); } private byte writeBuffer[] = new byte[8]; /** * long(8 byte), . written 8. * * @param v a long to be written. * @exception IOException if an I/O error occurs. * @see java.io.FilterOutputStream#out */ public final void writeLong(long v) throws IOException { writeBuffer[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); } /** * Float floatToIntBits float int, int output stream( 4 byte ), written 4. * @param v float * @exception I/O I/O * @see java.io.FilterOutputStream#out * @see java.lang.Float#floatToIntBits(float) */ public final void writeFloat(float v) throws IOException { writeInt(Float.floatToIntBits(v)); } /** * Double doubleToLongBits float double, double output stream( 8 byte ), written 8. * @param v double * @exception I/O I/O . * @see java.io.FilterOutputStream#out * @see java.lang.Double#doubleToLongBits(double) */ public final void writeDouble(double v) throws IOException { writeLong(Double.doubleToLongBits(v)); } /** * bytes output stream 。 , 8 bit。 written s * * @param s * @exception I/O I/O . * @see java.io.FilterOutputStream#out */ 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 to the underlying output stream as a sequence of * characters * output stream . writeChar output stream . written s * * @param s * @exception IOException I/O I/O . * @see java.io.DataOutputStream#writeChar(int) * @see java.io.FilterOutputStream#out */ 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); } /** * UTF-8 , DataOutput ,2byte writeShort , byte 。 byte , string , , output, , UTF-8 。 , written output stream byte , , 。 * * @param str a string to be written. * @exception IOException if an I/O error occurs. */ public final void writeUTF(String str) throws IOException { writeUTF(str, this); } /** * UTF-8 , DataOutput ,2byte writeShort , byte 。 byte , string , , output, , UTF-8 。 , written output stream byte , , 。 * @param str * @param out * @return byte * @exception IOException I/O */ static int writeUTF(String str, DataOutput out) throws IOException { int strlen = str.length(); int utflen = 0; int c, count = 0; /* use charAt instead of copying String to char array */ 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 string too long: " + utflen + " bytes"); 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]; } bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF); bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF); int i=0; for (i=0; iif (!((c >= 0x0001) && (c <= 0x007F))) break; bytearr[count++] = (byte) c; } for (;i < strlen; i++){ c = str.charAt(i); if ((c >= 0x0001) && (c <= 0x007F)) { bytearr[count++] = (byte) c; } else if (c > 0x07FF) { bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F)); bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F)); bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); } else { bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F)); bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); } } out.write(bytearr, 0, utflen+2); return utflen + 2; } /** * written , output stream . * , Integer.MAX_VALUE. * * @return written * @see java.io.DataOutputStream#written */ public final int size() { return written; } }

좋은 웹페이지 즐겨찾기