home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / io / DataOutputStream.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  12.1 KB  |  350 lines

  1. /*
  2.  * @(#)DataOutputStream.java    1.26 98/03/18
  3.  *
  4.  * Copyright 1994-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. /**
  18.  * A data input stream lets an application write primitive Java data 
  19.  * types to an output stream in a portable way. An application can 
  20.  * then use a data input stream to read the data back in. 
  21.  *
  22.  * @author  unascribed
  23.  * @version 1.26, 03/18/98
  24.  * @see     java.io.DataInputStream
  25.  * @since   JDK1.0
  26.  */
  27. public
  28. class DataOutputStream extends FilterOutputStream implements DataOutput {
  29.     /**
  30.      * The number of bytes written to the data output stream so far. 
  31.      */
  32.     protected int written;
  33.  
  34.     /**
  35.      * Creates a new data output stream to write data to the specified 
  36.      * underlying output stream. The counter <code>written</code> is 
  37.      * set to zero.
  38.      *
  39.      * @param   out   the underlying output stream, to be saved for later 
  40.      *                use.
  41.      * @see     java.io.FilterOutputStream#out
  42.      */
  43.     public DataOutputStream(OutputStream out) {
  44.     super(out);
  45.     }
  46.  
  47.     /**
  48.      * Writes the specified byte (the low eight bits of the argument 
  49.      * <code>b</code>) to the underlying output stream. If no exception 
  50.      * is thrown, the counter <code>written</code> is incremented by 
  51.      * <code>1</code>.
  52.      * <p>
  53.      * Implements the <code>write</code> method of <code>OutputStream</code>.
  54.      *
  55.      * @param      b   the <code>byte</code> to be written.
  56.      * @exception  IOException  if an I/O error occurs.
  57.      * @see        java.io.FilterOutputStream#out
  58.      */
  59.     public synchronized void write(int b) throws IOException {
  60.     out.write(b);
  61.     written++;
  62.     }
  63.  
  64.     /**
  65.      * Writes <code>len</code> bytes from the specified byte array 
  66.      * starting at offset <code>off</code> to the underlying output stream. 
  67.      * If no exception is thrown, the counter <code>written</code> is 
  68.      * incremented by <code>len</code>.
  69.      * <p>
  70.      * Overrides the <code>write</code> method of <code>OutputStream</code>.
  71.      *
  72.      * @param      b     the data.
  73.      * @param      off   the start offset in the data.
  74.      * @param      len   the number of bytes to write.
  75.      * @exception  IOException  if an I/O error occurs.
  76.      * @see        java.io.FilterOutputStream#out
  77.      */
  78.     public synchronized void write(byte b[], int off, int len)
  79.     throws IOException
  80.     {
  81.     out.write(b, off, len);
  82.     written += len;
  83.     }
  84.  
  85.     /**
  86.      * Flushes this data output stream. This forces any buffered output 
  87.      * bytes to be written out to the stream. 
  88.      * <p>
  89.      * The <code>flush</code> method of <code>DataOuputStream</code> 
  90.      * calls the <code>flush</code> method of its underlying output stream.
  91.      * <p>
  92.      * Overrides the <code>flush</code> method of <code>OutputStream</code>.
  93.      *
  94.      * @exception  IOException  if an I/O error occurs.
  95.      * @see        java.io.FilterOutputStream#out
  96.      * @see        java.io.OutputStream#flush()
  97.      */
  98.     public void flush() throws IOException {
  99.     out.flush();
  100.     }
  101.  
  102.     /**
  103.      * Writes a <code>boolean</code> to the underlying output stream as 
  104.      * a 1-byte value. The value <code>true</code> is written out as the 
  105.      * value <code>(byte)1</code>; the value <code>false</code> is 
  106.      * written out as the value <code>(byte)0</code>. If no exception is 
  107.      * thrown, the counter <code>written</code> is incremented by 
  108.      * <code>1</code>.
  109.      *
  110.      * @param      v   a <code>boolean</code> value to be written.
  111.      * @exception  IOException  if an I/O error occurs.
  112.      * @see        java.io.FilterOutputStream#out
  113.      */
  114.     public final void writeBoolean(boolean v) throws IOException {
  115.     out.write(v ? 1 : 0);
  116.     written++;
  117.     }
  118.  
  119.     /**
  120.      * Writes out a <code>byte</code> to the underlying output stream as 
  121.      * a 1-byte value. If no exception is thrown, the counter 
  122.      * <code>written</code> is incremented by <code>1</code>.
  123.      *
  124.      * @param      v   a <code>byte</code> value to be written.
  125.      * @exception  IOException  if an I/O error occurs.
  126.      * @see        java.io.FilterOutputStream#out
  127.      */
  128.     public final void writeByte(int v) throws IOException {
  129.     out.write(v);
  130.     written++;
  131.     }
  132.  
  133.     /**
  134.      * Writes a <code>short</code> to the underlying output stream as two
  135.      * bytes, high byte first. If no exception is thrown, the counter 
  136.      * <code>written</code> is incremented by <code>2</code>.
  137.      *
  138.      * @param      v   a <code>short</code> to be written.
  139.      * @exception  IOException  if an I/O error occurs.
  140.      * @see        java.io.FilterOutputStream#out
  141.      */
  142.     public final void writeShort(int v) throws IOException {
  143.     OutputStream out = this.out;
  144.     out.write((v >>> 8) & 0xFF);
  145.     out.write((v >>> 0) & 0xFF);
  146.     written += 2;
  147.     }
  148.  
  149.     /**
  150.      * Writes a <code>char</code> to the underlying output stream as a 
  151.      * 2-byte value, high byte first. If no exception is thrown, the 
  152.      * counter <code>written</code> is incremented by <code>2</code>.
  153.      *
  154.      * @param      v   a <code>char</code> value to be written.
  155.      * @exception  IOException  if an I/O error occurs.
  156.      * @see        java.io.FilterOutputStream#out
  157.      */
  158.     public final void writeChar(int v) throws IOException {
  159.     OutputStream out = this.out;
  160.     out.write((v >>> 8) & 0xFF);
  161.     out.write((v >>> 0) & 0xFF);
  162.     written += 2;
  163.     }
  164.  
  165.     /**
  166.      * Writes an <code>int</code> to the underlying output stream as four
  167.      * bytes, high byte first. If no exception is thrown, the counter 
  168.      * <code>written</code> is incremented by <code>4</code>.
  169.      *
  170.      * @param      v   an <code>int</code> to be written.
  171.      * @exception  IOException  if an I/O error occurs.
  172.      * @see        java.io.FilterOutputStream#out
  173.      */
  174.     public final void writeInt(int v) throws IOException {
  175.     OutputStream out = this.out;
  176.     out.write((v >>> 24) & 0xFF);
  177.     out.write((v >>> 16) & 0xFF);
  178.     out.write((v >>>  8) & 0xFF);
  179.     out.write((v >>>  0) & 0xFF);
  180.     written += 4;
  181.     }
  182.  
  183.     /**
  184.      * Writes a <code>long</code> to the underlying output stream as eight
  185.      * bytes, high byte first. In no exception is thrown, the counter 
  186.      * <code>written</code> is incremented by <code>8</code>.
  187.      *
  188.      * @param      v   a <code>long</code> to be written.
  189.      * @exception  IOException  if an I/O error occurs.
  190.      * @see        java.io.FilterOutputStream#out
  191.      */
  192.     public final void writeLong(long v) throws IOException {
  193.     OutputStream out = this.out;
  194.     out.write((int)(v >>> 56) & 0xFF);
  195.     out.write((int)(v >>> 48) & 0xFF);
  196.     out.write((int)(v >>> 40) & 0xFF);
  197.     out.write((int)(v >>> 32) & 0xFF);
  198.     out.write((int)(v >>> 24) & 0xFF);
  199.     out.write((int)(v >>> 16) & 0xFF);
  200.     out.write((int)(v >>>  8) & 0xFF);
  201.     out.write((int)(v >>>  0) & 0xFF);
  202.     written += 8;
  203.     }
  204.  
  205.     /**
  206.      * Converts the float argument to an <code>int</code> using the 
  207.      * <code>floatToIntBits</code> method in class <code>Float</code>, 
  208.      * and then writes that <code>int</code> value to the underlying 
  209.      * output stream as a 4-byte quantity, high byte first. If no 
  210.      * exception is thrown, the counter <code>written</code> is 
  211.      * incremented by <code>4</code>.
  212.      *
  213.      * @param      v   a <code>float</code> value to be written.
  214.      * @exception  IOException  if an I/O error occurs.
  215.      * @see        java.io.FilterOutputStream#out
  216.      * @see        java.lang.Float#floatToIntBits(float)
  217.      */
  218.     public final void writeFloat(float v) throws IOException {
  219.     writeInt(Float.floatToIntBits(v));
  220.     }
  221.  
  222.     /**
  223.      * Converts the double argument to a <code>long</code> using the 
  224.      * <code>doubleToLongBits</code> method in class <code>Double</code>, 
  225.      * and then writes that <code>long</code> value to the underlying 
  226.      * output stream as an 8-byte quantity, high byte first. If no 
  227.      * exception is thrown, the counter <code>written</code> is 
  228.      * incremented by <code>8</code>.
  229.      *
  230.      * @param      v   a <code>double</code> value to be written.
  231.      * @exception  IOException  if an I/O error occurs.
  232.      * @see        java.io.FilterOutputStream#out
  233.      * @see        java.lang.Double#doubleToLongBits(double)
  234.      */
  235.     public final void writeDouble(double v) throws IOException {
  236.     writeLong(Double.doubleToLongBits(v));
  237.     }
  238.  
  239.     /**
  240.      * Writes out the string to the underlying output stream as a 
  241.      * sequence of bytes. Each character in the string is written out, in 
  242.      * sequence, by discarding its high eight bits. If no exception is 
  243.      * thrown, the counter <code>written</code> is incremented by the 
  244.      * length of <code>s</code>.
  245.      *
  246.      * @param      s   a string of bytes to be written.
  247.      * @exception  IOException  if an I/O error occurs.
  248.      * @see        java.io.FilterOutputStream#out
  249.      */
  250.     public final void writeBytes(String s) throws IOException {
  251.     OutputStream out = this.out;
  252.     int len = s.length();
  253.     for (int i = 0 ; i < len ; i++) {
  254.         out.write((byte)s.charAt(i));
  255.     }
  256.     written += len;
  257.     }
  258.  
  259.     /**
  260.      * Writes a string to the underlying output stream as a sequence of 
  261.      * characters. Each character is written to the data output stream as 
  262.      * if by the <code>writeChar</code> method. If no exception is 
  263.      * thrown, the counter <code>written</code> is incremented by twice 
  264.      * the length of <code>s</code>.
  265.      *
  266.      * @param      s   a <code>String</code> value to be written.
  267.      * @exception  IOException  if an I/O error occurs.
  268.      * @see        java.io.DataOutputStream#writeChar(int)
  269.      * @see        java.io.FilterOutputStream#out
  270.      */
  271.     public final void writeChars(String s) throws IOException {
  272.     OutputStream out = this.out;
  273.     int len = s.length();
  274.     for (int i = 0 ; i < len ; i++) {
  275.         int v = s.charAt(i);
  276.         out.write((v >>> 8) & 0xFF);
  277.         out.write((v >>> 0) & 0xFF);
  278.     }
  279.     written += len * 2;
  280.     }
  281.  
  282.     /**
  283.      * Writes a string to the underlying output stream using UTF-8 
  284.      * encoding in a machine-independent manner. 
  285.      * <p>
  286.      * First, two bytes are written to the output stream as if by the 
  287.      * <code>writeShort</code> method giving the number of bytes to 
  288.      * follow. This value is the number of bytes actually written out, 
  289.      * not the length of the string. Following the length, each character 
  290.      * of the string is output, in sequence, using the UTF-8 encoding 
  291.      * for the character. If no exception is thrown, the counter 
  292.      * <code>written</code> is incremented by the total number of 
  293.      * bytes written to the output stream. This will be at least two 
  294.      * plus the length of <code>str</code>, and at most two plus 
  295.      * thrice the length of <code>str</code>.
  296.      *
  297.      * @param      str   a string to be written.
  298.      * @exception  IOException  if an I/O error occurs.
  299.      */
  300.     public final void writeUTF(String str) throws IOException {
  301.     OutputStream out = this.out;
  302.     int strlen = str.length();
  303.     int utflen = 0;
  304.  
  305.     for (int i = 0 ; i < strlen ; i++) {
  306.         int c = str.charAt(i);
  307.         if ((c >= 0x0001) && (c <= 0x007F)) {
  308.         utflen++;
  309.         } else if (c > 0x07FF) {
  310.         utflen += 3;
  311.         } else {
  312.         utflen += 2;
  313.         }
  314.     }
  315.  
  316.     if (utflen > 65535)
  317.         throw new UTFDataFormatException();          
  318.  
  319.     out.write((utflen >>> 8) & 0xFF);
  320.     out.write((utflen >>> 0) & 0xFF);
  321.     for (int i = 0 ; i < strlen ; i++) {
  322.         int c = str.charAt(i);
  323.         if ((c >= 0x0001) && (c <= 0x007F)) {
  324.         out.write(c);
  325.         } else if (c > 0x07FF) {
  326.         out.write(0xE0 | ((c >> 12) & 0x0F));
  327.         out.write(0x80 | ((c >>  6) & 0x3F));
  328.         out.write(0x80 | ((c >>  0) & 0x3F));
  329.         written += 2;
  330.         } else {
  331.         out.write(0xC0 | ((c >>  6) & 0x1F));
  332.         out.write(0x80 | ((c >>  0) & 0x3F));
  333.         written += 1;
  334.         }
  335.     }
  336.     written += strlen + 2;
  337.     }
  338.  
  339.     /**
  340.      * Returns the current value of the counter <code>written</code>, 
  341.      * the number of bytes written to this data output stream so far.
  342.      *
  343.      * @return  the value of the <code>written</code> field.
  344.      * @see     java.io.DataOutputStream#written
  345.      */
  346.     public final int size() {
  347.     return written;
  348.     }
  349. }
  350.