home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / DataOutputStream.java < prev    next >
Text File  |  1996-05-03  |  6KB  |  242 lines

  1. /*
  2.  * @(#)DataOutputStream.java    1.20 95/12/18 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.io;
  21.  
  22. /**
  23.  * This class lets you write primitive Java data types
  24.  * to a stream in a portable way. Primitive data types are well
  25.  * understood types with associated operations.  For example, an 
  26.  * Integer is considered to be a good primitive data type.
  27.  *
  28.  * The data can be converted back using a DataInputStream.
  29.  */
  30.  
  31. public
  32. class DataOutputStream extends FilterOutputStream implements DataOutput {
  33.     /**
  34.      * The number of bytes written so far.
  35.      */
  36.     protected int written;
  37.  
  38.     /**
  39.      * Creates a new DataOutputStream.
  40.      * @param out    the output stream
  41.      */
  42.     public DataOutputStream(OutputStream out) {
  43.     super(out);
  44.     }
  45.  
  46.     /**
  47.      * Writes a byte. Will block until the byte is actually
  48.      * written.
  49.      * @param b the byte to be written
  50.      * @exception IOException If an I/O error has occurred.
  51.      */
  52.     public synchronized void write(int b) throws IOException {
  53.     out.write(b);
  54.     written++;
  55.     }
  56.  
  57.     /**
  58.      * Writes a sub array of bytes.  
  59.      * @param b    the data to be written
  60.      * @param off    the start offset in the data
  61.      * @param len    the number of bytes that are written
  62.      * @exception IOException If an I/O error has occurred.
  63.      */
  64.     public synchronized void write(byte b[], int off, int len)
  65.     throws IOException
  66.     {
  67.     out.write(b, off, len);
  68.     written += len;
  69.     }
  70.  
  71.     /**
  72.      * Flushes the stream. This will write any buffered
  73.      * output bytes.
  74.      * @exception IOException If an I/O error has occurred.
  75.      */
  76.     public void flush() throws IOException {
  77.     out.flush();
  78.     }
  79.  
  80.     /**
  81.      * Writes a boolean.
  82.      * @param v the boolean to be written
  83.      */
  84.     public final void writeBoolean(boolean v) throws IOException {
  85.     out.write(v ? 1 : 0);
  86.     written++;
  87.     }
  88.  
  89.     /**
  90.      * Writes an 8 bit byte.
  91.      * @param v the byte value to be written
  92.      */
  93.     public final void writeByte(int v) throws IOException {
  94.     out.write(v);
  95.     written++;
  96.     }
  97.  
  98.     /**
  99.      * Writes a 16 bit short.
  100.      * @param v the short value to be written
  101.      */
  102.     public final void writeShort(int v) throws IOException {
  103.     OutputStream out = this.out;
  104.     out.write((v >>> 8) & 0xFF);
  105.     out.write((v >>> 0) & 0xFF);
  106.     written += 2;
  107.     }
  108.  
  109.     /**
  110.      * Writes a 16 bit char.
  111.      * @param v the char value to be written
  112.      */
  113.     public final void writeChar(int v) throws IOException {
  114.     OutputStream out = this.out;
  115.     out.write((v >>> 8) & 0xFF);
  116.     out.write((v >>> 0) & 0xFF);
  117.     written += 2;
  118.     }
  119.  
  120.     /**
  121.      * Writes a 32 bit int.
  122.      * @param v the integer value to be written
  123.      */
  124.     public final void writeInt(int v) throws IOException {
  125.     OutputStream out = this.out;
  126.     out.write((v >>> 24) & 0xFF);
  127.     out.write((v >>> 16) & 0xFF);
  128.     out.write((v >>>  8) & 0xFF);
  129.     out.write((v >>>  0) & 0xFF);
  130.     written += 4;
  131.     }
  132.  
  133.     /**
  134.      * Writes a 64 bit long.
  135.      * @param v the long value to be written
  136.      */
  137.     public final void writeLong(long v) throws IOException {
  138.     OutputStream out = this.out;
  139.     out.write((int)(v >>> 56) & 0xFF);
  140.     out.write((int)(v >>> 48) & 0xFF);
  141.     out.write((int)(v >>> 40) & 0xFF);
  142.     out.write((int)(v >>> 32) & 0xFF);
  143.     out.write((int)(v >>> 24) & 0xFF);
  144.     out.write((int)(v >>> 16) & 0xFF);
  145.     out.write((int)(v >>>  8) & 0xFF);
  146.     out.write((int)(v >>>  0) & 0xFF);
  147.     written += 8;
  148.     }
  149.  
  150.     /**
  151.      * Writes a 32 bit float.
  152.      * @param v the float value to be written
  153.      */
  154.     public final void writeFloat(float v) throws IOException {
  155.     writeInt(Float.floatToIntBits(v));
  156.     }
  157.  
  158.     /**
  159.      * Writes a 64 bit double.
  160.      * @param v the double value to be written
  161.      */
  162.     public final void writeDouble(double v) throws IOException {
  163.     writeLong(Double.doubleToLongBits(v));
  164.     }
  165.  
  166.     /**
  167.      * Writes a String as a sequence of bytes.
  168.      * @param s the String of bytes to be written
  169.      */
  170.     public final void writeBytes(String s) throws IOException {
  171.     OutputStream out = this.out;
  172.     int len = s.length();
  173.     for (int i = 0 ; i < len ; i++) {
  174.         out.write((byte)s.charAt(i));
  175.     }
  176.     written += len;
  177.     }
  178.  
  179.     /**
  180.      * Writes a String as a sequence of chars.
  181.      * @param s the String of chars to be written
  182.      */
  183.     public final void writeChars(String s) throws IOException {
  184.     OutputStream out = this.out;
  185.     int len = s.length();
  186.     for (int i = 0 ; i < len ; i++) {
  187.         int v = s.charAt(i);
  188.         out.write((v >>> 8) & 0xFF);
  189.         out.write((v >>> 0) & 0xFF);
  190.     }
  191.     written += len * 2;
  192.     }
  193.  
  194.     /**
  195.      * Writes a String in UTF format.
  196.      * @param str the String in UTF format
  197.      */
  198.     public final void writeUTF(String str) throws IOException {
  199.     OutputStream out = this.out;
  200.     int strlen = str.length();
  201.     int utflen = 0;
  202.  
  203.     for (int i = 0 ; i < strlen ; i++) {
  204.         int c = str.charAt(i);
  205.         if ((c >= 0x0001) && (c <= 0x007F)) {
  206.         utflen++;
  207.         } else if (c > 0x07FF) {
  208.         utflen += 3;
  209.         } else {
  210.         utflen += 2;
  211.         }
  212.     }
  213.  
  214.     out.write((utflen >>> 8) & 0xFF);
  215.     out.write((utflen >>> 0) & 0xFF);
  216.     for (int i = 0 ; i < strlen ; i++) {
  217.         int c = str.charAt(i);
  218.         if ((c >= 0x0001) && (c <= 0x007F)) {
  219.         out.write(c);
  220.         } else if (c > 0x07FF) {
  221.         out.write(0xE0 | ((c >> 12) & 0x0F));
  222.         out.write(0x80 | ((c >>  6) & 0x3F));
  223.         out.write(0x80 | ((c >>  0) & 0x3F));
  224.         written += 2;
  225.         } else {
  226.         out.write(0xC0 | ((c >>  6) & 0x1F));
  227.         out.write(0x80 | ((c >>  0) & 0x3F));
  228.         written += 1;
  229.         }
  230.     }
  231.     written += strlen + 2;
  232.     }
  233.  
  234.     /**
  235.      * Returns the number of bytes written.
  236.      * @return    the number of bytes written thus far.
  237.      */
  238.     public final int size() {
  239.     return written;
  240.     }
  241. }
  242.