home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / java / io / DataOutputStream.java < prev    next >
Encoding:
Java Source  |  1997-01-27  |  7.0 KB  |  273 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.     private boolean isLocalised = false;
  34.     /**
  35.      * The number of bytes written so far.
  36.      */
  37.     protected int written;
  38.  
  39.     /**
  40.      * Creates a new DataOutputStream.
  41.      * @param out    the output stream
  42.      */
  43.     public DataOutputStream(OutputStream out) {
  44.     super(out);
  45.     }
  46.  
  47.     /**
  48.      * Writes a byte. Will block until the byte is actually
  49.      * written.
  50.      * @param b the byte to be written
  51.      * @exception IOException If an I/O error has occurred.
  52.      */
  53.     public synchronized void write(int b) throws IOException {
  54.     out.write(b);
  55.     written++;
  56.     }
  57.  
  58.     /**
  59.      * Writes a sub array of bytes.  
  60.      * @param b    the data to be written
  61.      * @param off    the start offset in the data
  62.      * @param len    the number of bytes that are written
  63.      * @exception IOException If an I/O error has occurred.
  64.      */
  65.     public synchronized void write(byte b[], int off, int len)
  66.     throws IOException
  67.     {
  68.     out.write(b, off, len);
  69.     written += len;
  70.     }
  71.  
  72.     /**
  73.      * Flushes the stream. This will write any buffered
  74.      * output bytes.
  75.      * @exception IOException If an I/O error has occurred.
  76.      */
  77.     public void flush() throws IOException {
  78.     out.flush();
  79.     }
  80.  
  81.     /**
  82.      * Writes a boolean.
  83.      * @param v the boolean to be written
  84.      */
  85.     public final void writeBoolean(boolean v) throws IOException {
  86.     out.write(v ? 1 : 0);
  87.     written++;
  88.     }
  89.  
  90.     /**
  91.      * Writes an 8 bit byte.
  92.      * @param v the byte value to be written
  93.      */
  94.     public final void writeByte(int v) throws IOException {
  95.     out.write(v);
  96.     written++;
  97.     }
  98.  
  99.     /**
  100.      * Writes a 16 bit short.
  101.      * @param v the short value to be written
  102.      */
  103.     public final void writeShort(int v) throws IOException {
  104.     OutputStream out = this.out;
  105.     out.write((v >>> 8) & 0xFF);
  106.     out.write((v >>> 0) & 0xFF);
  107.     written += 2;
  108.     }
  109.  
  110.     /**
  111.      * Writes a 16 bit char.
  112.      * @param v the char value to be written
  113.      */
  114.     public final void writeChar(int v) throws IOException {
  115.     OutputStream out = this.out;
  116.     if( isLocalised )
  117.     {
  118.         char[] C = new char[1];
  119.         C[0] = (char)v;
  120.         byte[] B = com.ms.lang.SystemX.JavaStringToLocalString(C);
  121.         out.write(B);
  122.         written += B.length;
  123.     }
  124.     else
  125.     {
  126.         out.write((v >>> 8) & 0xFF);
  127.         out.write((v >>> 0) & 0xFF);
  128.         written += 2;
  129.     }
  130.     }
  131.  
  132.     /**
  133.      * Writes a 32 bit int.
  134.      * @param v the integer value to be written
  135.      */
  136.     public final void writeInt(int v) throws IOException {
  137.     OutputStream out = this.out;
  138.     out.write((v >>> 24) & 0xFF);
  139.     out.write((v >>> 16) & 0xFF);
  140.     out.write((v >>>  8) & 0xFF);
  141.     out.write((v >>>  0) & 0xFF);
  142.     written += 4;
  143.     }
  144.  
  145.     /**
  146.      * Writes a 64 bit long.
  147.      * @param v the long value to be written
  148.      */
  149.     public final void writeLong(long v) throws IOException {
  150.     OutputStream out = this.out;
  151.     out.write((int)(v >>> 56) & 0xFF);
  152.     out.write((int)(v >>> 48) & 0xFF);
  153.     out.write((int)(v >>> 40) & 0xFF);
  154.     out.write((int)(v >>> 32) & 0xFF);
  155.     out.write((int)(v >>> 24) & 0xFF);
  156.     out.write((int)(v >>> 16) & 0xFF);
  157.     out.write((int)(v >>>  8) & 0xFF);
  158.     out.write((int)(v >>>  0) & 0xFF);
  159.     written += 8;
  160.     }
  161.  
  162.     /**
  163.      * Writes a 32 bit float.
  164.      * @param v the float value to be written
  165.      */
  166.     public final void writeFloat(float v) throws IOException {
  167.     writeInt(Float.floatToIntBits(v));
  168.     }
  169.  
  170.     /**
  171.      * Writes a 64 bit double.
  172.      * @param v the double value to be written
  173.      */
  174.     public final void writeDouble(double v) throws IOException {
  175.     writeLong(Double.doubleToLongBits(v));
  176.     }
  177.  
  178.     /**
  179.      * Writes a String as a sequence of bytes.
  180.      * @param s the String of bytes to be written
  181.      */
  182.     public final void writeBytes(String s) throws IOException {
  183.     OutputStream out = this.out;
  184.     if( isLocalised )
  185.     {
  186.         byte B[] = com.ms.lang.SystemX.JavaStringToLocalString(s.toCharArray());
  187.         out.write(B, 0, B.length);
  188.         written += B.length;
  189.     }
  190.     else
  191.     {
  192.         int len = s.length();
  193.         for (int i = 0 ; i < len ; i++) 
  194.         {
  195.             out.write((byte)s.charAt(i));
  196.         }
  197.         written += len;
  198.     }
  199.     }
  200.  
  201.     /**
  202.      * Writes a String as a sequence of chars.
  203.      * @param s the String of chars to be written
  204.      */
  205.     public final void writeChars(String s) throws IOException {
  206.     OutputStream out = this.out;
  207.     if( isLocalised )
  208.     {
  209.         byte B[] = com.ms.lang.SystemX.JavaStringToLocalString(s.toCharArray());
  210.         out.write(B, 0, B.length);
  211.         written += B.length;
  212.     }
  213.     else
  214.     {
  215.     int len = s.length();
  216.     for (int i = 0 ; i < len ; i++) {
  217.         int v = s.charAt(i);
  218.         out.write((v >>> 8) & 0xFF);
  219.         out.write((v >>> 0) & 0xFF);
  220.     }
  221.     written += len * 2;
  222.     }
  223.     }
  224.  
  225.     /**
  226.      * Writes a String in UTF format.
  227.      * @param str the String in UTF format
  228.      */
  229.     public final void writeUTF(String str) throws IOException {
  230.     OutputStream out = this.out;
  231.     int strlen = str.length();
  232.     int utflen = 0;
  233.  
  234.     for (int i = 0 ; i < strlen ; i++) {
  235.         int c = str.charAt(i);
  236.         if ((c >= 0x0001) && (c <= 0x007F)) {
  237.         utflen++;
  238.         } else if (c > 0x07FF) {
  239.         utflen += 3;
  240.         } else {
  241.         utflen += 2;
  242.         }
  243.     }
  244.  
  245.     out.write((utflen >>> 8) & 0xFF);
  246.     out.write((utflen >>> 0) & 0xFF);
  247.     for (int i = 0 ; i < strlen ; i++) {
  248.         int c = str.charAt(i);
  249.         if ((c >= 0x0001) && (c <= 0x007F)) {
  250.         out.write(c);
  251.         } else if (c > 0x07FF) {
  252.         out.write(0xE0 | ((c >> 12) & 0x0F));
  253.         out.write(0x80 | ((c >>  6) & 0x3F));
  254.         out.write(0x80 | ((c >>  0) & 0x3F));
  255.         written += 2;
  256.         } else {
  257.         out.write(0xC0 | ((c >>  6) & 0x1F));
  258.         out.write(0x80 | ((c >>  0) & 0x3F));
  259.         written += 1;
  260.         }
  261.     }
  262.     written += strlen + 2;
  263.     }
  264.  
  265.     /**
  266.      * Returns the number of bytes written.
  267.      * @return    the number of bytes written thus far.
  268.      */
  269.     public final int size() {
  270.     return written;
  271.     }
  272. }
  273.