home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / io / DataOutputStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  3.4 KB  |  144 lines

  1. package java.io;
  2.  
  3. public class DataOutputStream extends FilterOutputStream implements DataOutput {
  4.    protected int written;
  5.  
  6.    public DataOutputStream(OutputStream out) {
  7.       super(out);
  8.    }
  9.  
  10.    public synchronized void write(int b) throws IOException {
  11.       super.out.write(b);
  12.       ++this.written;
  13.    }
  14.  
  15.    public synchronized void write(byte[] b, int off, int len) throws IOException {
  16.       super.out.write(b, off, len);
  17.       this.written += len;
  18.    }
  19.  
  20.    public void flush() throws IOException {
  21.       super.out.flush();
  22.    }
  23.  
  24.    public final void writeBoolean(boolean v) throws IOException {
  25.       super.out.write(v ? 1 : 0);
  26.       ++this.written;
  27.    }
  28.  
  29.    public final void writeByte(int v) throws IOException {
  30.       super.out.write(v);
  31.       ++this.written;
  32.    }
  33.  
  34.    public final void writeShort(int v) throws IOException {
  35.       OutputStream out = super.out;
  36.       out.write(v >>> 8 & 255);
  37.       out.write(v & 255);
  38.       this.written += 2;
  39.    }
  40.  
  41.    public final void writeChar(int v) throws IOException {
  42.       OutputStream out = super.out;
  43.       out.write(v >>> 8 & 255);
  44.       out.write(v & 255);
  45.       this.written += 2;
  46.    }
  47.  
  48.    public final void writeInt(int v) throws IOException {
  49.       OutputStream out = super.out;
  50.       out.write(v >>> 24 & 255);
  51.       out.write(v >>> 16 & 255);
  52.       out.write(v >>> 8 & 255);
  53.       out.write(v & 255);
  54.       this.written += 4;
  55.    }
  56.  
  57.    public final void writeLong(long v) throws IOException {
  58.       OutputStream out = super.out;
  59.       out.write((int)(v >>> 56) & 255);
  60.       out.write((int)(v >>> 48) & 255);
  61.       out.write((int)(v >>> 40) & 255);
  62.       out.write((int)(v >>> 32) & 255);
  63.       out.write((int)(v >>> 24) & 255);
  64.       out.write((int)(v >>> 16) & 255);
  65.       out.write((int)(v >>> 8) & 255);
  66.       out.write((int)v & 255);
  67.       this.written += 8;
  68.    }
  69.  
  70.    public final void writeFloat(float v) throws IOException {
  71.       this.writeInt(Float.floatToIntBits(v));
  72.    }
  73.  
  74.    public final void writeDouble(double v) throws IOException {
  75.       this.writeLong(Double.doubleToLongBits(v));
  76.    }
  77.  
  78.    public final void writeBytes(String s) throws IOException {
  79.       OutputStream out = super.out;
  80.       int len = s.length();
  81.  
  82.       for(int i = 0; i < len; ++i) {
  83.          out.write((byte)s.charAt(i));
  84.       }
  85.  
  86.       this.written += len;
  87.    }
  88.  
  89.    public final void writeChars(String s) throws IOException {
  90.       OutputStream out = super.out;
  91.       int len = s.length();
  92.  
  93.       for(int i = 0; i < len; ++i) {
  94.          int v = s.charAt(i);
  95.          out.write(v >>> 8 & 255);
  96.          out.write(v & 255);
  97.       }
  98.  
  99.       this.written += len * 2;
  100.    }
  101.  
  102.    public final void writeUTF(String str) throws IOException {
  103.       OutputStream out = super.out;
  104.       int strlen = str.length();
  105.       int utflen = 0;
  106.  
  107.       for(int i = 0; i < strlen; ++i) {
  108.          int c = str.charAt(i);
  109.          if (c >= 1 && c <= 127) {
  110.             ++utflen;
  111.          } else if (c > 2047) {
  112.             utflen += 3;
  113.          } else {
  114.             utflen += 2;
  115.          }
  116.       }
  117.  
  118.       out.write(utflen >>> 8 & 255);
  119.       out.write(utflen & 255);
  120.  
  121.       for(int i = 0; i < strlen; ++i) {
  122.          int c = str.charAt(i);
  123.          if (c >= 1 && c <= 127) {
  124.             out.write(c);
  125.          } else if (c > 2047) {
  126.             out.write(224 | c >> 12 & 15);
  127.             out.write(128 | c >> 6 & 63);
  128.             out.write(128 | c & 63);
  129.             this.written += 2;
  130.          } else {
  131.             out.write(192 | c >> 6 & 31);
  132.             out.write(128 | c & 63);
  133.             ++this.written;
  134.          }
  135.       }
  136.  
  137.       this.written += strlen + 2;
  138.    }
  139.  
  140.    public final int size() {
  141.       return this.written;
  142.    }
  143. }
  144.