home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / DataOutputStream.java < prev    next >
Text File  |  1997-05-20  |  11KB  |  343 lines

  1. /*
  2.  * @(#)DataOutputStream.java    1.23 97/01/22
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.io;
  24.  
  25. /**
  26.  * A data input stream lets an application write primitive Java data 
  27.  * types to an output stream in a portable way. An application can 
  28.  * then use a data input stream to read the data back in. 
  29.  *
  30.  * @author  unascribed
  31.  * @version 1.23, 01/22/97
  32.  * @see     java.io.DataInputStream
  33.  * @since   JDK1.0
  34.  */
  35. public
  36. class DataOutputStream extends FilterOutputStream implements DataOutput {
  37.     /**
  38.      * The number of bytes written to the data output stream. 
  39.      *
  40.      * @since   JDK1.0
  41.      */
  42.     protected int written;
  43.  
  44.     /**
  45.      * Creates a new data output stream to write data to the specified 
  46.      * underlying output stream. 
  47.      *
  48.      * @param   out   the underlying output stream.
  49.      * @see     java.io.FilterOutputStream#out
  50.      * @since   JDK1.0
  51.      */
  52.     public DataOutputStream(OutputStream out) {
  53.     super(out);
  54.     }
  55.  
  56.     /**
  57.      * Writes the specified byte to the underlying output stream. 
  58.      *
  59.      * @param      b   the <code>byte</code> to be written.
  60.      * @exception  IOException  if an I/O error occurs.
  61.      * @see        java.io.FilterOutputStream#out
  62.      * @since      JDK1.0
  63.      */
  64.     public synchronized void write(int b) throws IOException {
  65.     out.write(b);
  66.     written++;
  67.     }
  68.  
  69.     /**
  70.      * Writes <code>len</code> bytes from the specified byte array 
  71.      * starting at offset <code>off</code> to the underlying output stream.
  72.      *
  73.      * @param      b     the data.
  74.      * @param      off   the start offset in the data.
  75.      * @param      len   the number of bytes to write.
  76.      * @exception  IOException  if an I/O error occurs.
  77.      * @see        java.io.FilterOutputStream#out
  78.      * @since      JDK1.0
  79.      */
  80.     public synchronized void write(byte b[], int off, int len)
  81.     throws IOException
  82.     {
  83.     out.write(b, off, len);
  84.     written += len;
  85.     }
  86.  
  87.     /**
  88.      * Flushes this data output stream. This forces any buffered output 
  89.      * bytes to be written out to the stream. 
  90.      * <p>
  91.      * The <code>flush</code> method of <code>DataOuputStream</code> 
  92.      * calls the <code>flush</code> method of its underlying output stream.
  93.      *
  94.      * @exception  IOException  if an I/O error occurs.
  95.      * @see        java.io.FilterOutputStream#out
  96.      * @see        java.io.OutputStream#flush()
  97.      * @since      JDK1.0
  98.      */
  99.     public void flush() throws IOException {
  100.     out.flush();
  101.     }
  102.  
  103.     /**
  104.      * Writes a <code>boolean</code> to the underlying output stream as 
  105.      * a 1-byte value. The value <code>true</code> is written out as the 
  106.      * value <code>(byte)1</code>; the value <code>false</code> is 
  107.      * written out as the value <code>(byte)0</code>.
  108.      *
  109.      * @param      v   a <code>boolean</code> value to be written.
  110.      * @exception  IOException  if an I/O error occurs.
  111.      * @see        java.io.FilterOutputStream#out
  112.      * @since      JDK1.0
  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. 
  122.      *
  123.      * @param      v   a <code>byte</code> value to be written.
  124.      * @exception  IOException  if an I/O error occurs.
  125.      * @see        java.io.FilterOutputStream#out
  126.      * @since      JDK1.0
  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. 
  136.      *
  137.      * @param      v   a <code>short</code> to be written.
  138.      * @exception  IOException  if an I/O error occurs.
  139.      * @see        java.io.FilterOutputStream#out
  140.      * @since      JDK1.0
  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. 
  152.      *
  153.      * @param      v   a <code>char</code> value to be written.
  154.      * @exception  IOException  if an I/O error occurs.
  155.      * @see        java.io.FilterOutputStream#out
  156.      * @since      JDK1.0
  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. 
  168.      *
  169.      * @param      v   an <code>int</code> to be written.
  170.      * @exception  IOException  if an I/O error occurs.
  171.      * @see        java.io.FilterOutputStream#out
  172.      * @since      JDK1.0
  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. 
  186.      *
  187.      * @param      v   a <code>long</code> to be written.
  188.      * @exception  IOException  if an I/O error occurs.
  189.      * @see        java.io.FilterOutputStream#out
  190.      * @since      JDK1.0
  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. 
  210.      *
  211.      * @param      v   a <code>float</code> value to be written.
  212.      * @exception  IOException  if an I/O error occurs.
  213.      * @see        java.io.FilterOutputStream#out
  214.      * @see        java.lang.Float#floatToIntBits(float)
  215.      * @since      JDK1.0
  216.      */
  217.     public final void writeFloat(float v) throws IOException {
  218.     writeInt(Float.floatToIntBits(v));
  219.     }
  220.  
  221.     /**
  222.      * Converts the double argument to a <code>long</code> using the 
  223.      * <code>doubleToLongBits</code> method in class <code>Double</code>, 
  224.      * and then writes that <code>long</code> value to the underlying 
  225.      * output stream as an 8-byte quantity, high byte first. 
  226.      *
  227.      * @param      v   a <code>double</code> value to be written.
  228.      * @exception  IOException  if an I/O error occurs.
  229.      * @see        java.io.FilterOutputStream#out
  230.      * @see        java.lang.Double#doubleToLongBits(double)
  231.      * @since      JDK1.0
  232.      */
  233.     public final void writeDouble(double v) throws IOException {
  234.     writeLong(Double.doubleToLongBits(v));
  235.     }
  236.  
  237.     /**
  238.      * Writes out the string to the underlying output stream as a 
  239.      * sequence of bytes. Each character in the string is written out, in 
  240.      * sequence, by discarding its high eight bits. 
  241.      *
  242.      * @param      s   a string of bytes to be written.
  243.      * @exception  IOException  if an I/O error occurs.
  244.      * @see        java.io.FilterOutputStream#out
  245.      * @since      JDK1.0
  246.      */
  247.     public final void writeBytes(String s) throws IOException {
  248.     OutputStream out = this.out;
  249.     int len = s.length();
  250.     for (int i = 0 ; i < len ; i++) {
  251.         out.write((byte)s.charAt(i));
  252.     }
  253.     written += len;
  254.     }
  255.  
  256.     /**
  257.      * Writes a string to the underlying output stream as a sequence of 
  258.      * characters. Each character is written to the data output stream as 
  259.      * if by the <code>writeChar</code> method. 
  260.      *
  261.      * @param      s   a <code>String</code> value to be written.
  262.      * @exception  IOException  if an I/O error occurs.
  263.      * @see        java.io.DataOutputStream#writeChar(int)
  264.      * @see        java.io.FilterOutputStream#out
  265.      * @since      JDK1.0
  266.      */
  267.     public final void writeChars(String s) throws IOException {
  268.     OutputStream out = this.out;
  269.     int len = s.length();
  270.     for (int i = 0 ; i < len ; i++) {
  271.         int v = s.charAt(i);
  272.         out.write((v >>> 8) & 0xFF);
  273.         out.write((v >>> 0) & 0xFF);
  274.     }
  275.     written += len * 2;
  276.     }
  277.  
  278.     /**
  279.      * Writes a string to the underlying output stream using UTF-8 
  280.      * encoding in a machine-independent manner. 
  281.      * <p>
  282.      * First, two bytes are written to the output stream as if by the 
  283.      * <code>writeShort</code> method giving the number of bytes to 
  284.      * follow. This value is the number of bytes actually written out, 
  285.      * not the length of the string. Following the length, each character 
  286.      * of the string is output, in sequence, using the UTF-8 encoding 
  287.      * for the character. 
  288.      *
  289.      * @param      str   a string to be written.
  290.      * @exception  IOException  if an I/O error occurs.
  291.      * @since      JDK1.0
  292.      */
  293.     public final void writeUTF(String str) throws IOException {
  294.     OutputStream out = this.out;
  295.     int strlen = str.length();
  296.     int utflen = 0;
  297.  
  298.     for (int i = 0 ; i < strlen ; i++) {
  299.         int c = str.charAt(i);
  300.         if ((c >= 0x0001) && (c <= 0x007F)) {
  301.         utflen++;
  302.         } else if (c > 0x07FF) {
  303.         utflen += 3;
  304.         } else {
  305.         utflen += 2;
  306.         }
  307.     }
  308.  
  309.     if (utflen > 65535)
  310.         throw new UTFDataFormatException();          
  311.  
  312.     out.write((utflen >>> 8) & 0xFF);
  313.     out.write((utflen >>> 0) & 0xFF);
  314.     for (int i = 0 ; i < strlen ; i++) {
  315.         int c = str.charAt(i);
  316.         if ((c >= 0x0001) && (c <= 0x007F)) {
  317.         out.write(c);
  318.         } else if (c > 0x07FF) {
  319.         out.write(0xE0 | ((c >> 12) & 0x0F));
  320.         out.write(0x80 | ((c >>  6) & 0x3F));
  321.         out.write(0x80 | ((c >>  0) & 0x3F));
  322.         written += 2;
  323.         } else {
  324.         out.write(0xC0 | ((c >>  6) & 0x1F));
  325.         out.write(0x80 | ((c >>  0) & 0x3F));
  326.         written += 1;
  327.         }
  328.     }
  329.     written += strlen + 2;
  330.     }
  331.  
  332.     /**
  333.      * Returns the number of bytes written to this data output stream.
  334.      *
  335.      * @return  the value of the <code>written</code> field.
  336.      * @see     java.io.DataOutputStream#written
  337.      * @since   JDK1.0
  338.      */
  339.     public final int size() {
  340.     return written;
  341.     }
  342. }
  343.