| 1 | // DataOutputStream.java - Output filter that implements DataOutput
|
|---|
| 2 |
|
|---|
| 3 | /* Copyright (C) 1998, 1999 Free Software Foundation
|
|---|
| 4 |
|
|---|
| 5 | This file is part of libgcj.
|
|---|
| 6 |
|
|---|
| 7 | This software is copyrighted work licensed under the terms of the
|
|---|
| 8 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|---|
| 9 | details. */
|
|---|
| 10 |
|
|---|
| 11 | package java.io;
|
|---|
| 12 |
|
|---|
| 13 | /**
|
|---|
| 14 | * @author Tom Tromey <[email protected]>
|
|---|
| 15 | * @date September 24, 1998
|
|---|
| 16 | */
|
|---|
| 17 |
|
|---|
| 18 | /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
|
|---|
| 19 | * "The Java Language Specification", ISBN 0-201-63451-1
|
|---|
| 20 | * Status: Complete to version 1.1.
|
|---|
| 21 | */
|
|---|
| 22 |
|
|---|
| 23 | public class DataOutputStream extends FilterOutputStream implements DataOutput
|
|---|
| 24 | {
|
|---|
| 25 | public DataOutputStream (OutputStream out)
|
|---|
| 26 | {
|
|---|
| 27 | super (out);
|
|---|
| 28 | written = 0;
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | public void flush () throws IOException
|
|---|
| 32 | {
|
|---|
| 33 | out.flush();
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | public final int size ()
|
|---|
| 37 | {
|
|---|
| 38 | return written;
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | public synchronized void write (int b) throws IOException
|
|---|
| 42 | {
|
|---|
| 43 | out.write(b);
|
|---|
| 44 | ++written;
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | public synchronized void write (byte[] b, int off, int len)
|
|---|
| 48 | throws IOException, NullPointerException, IndexOutOfBoundsException
|
|---|
| 49 | {
|
|---|
| 50 | out.write(b, off, len);
|
|---|
| 51 | written += len;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | public final void writeBoolean (boolean v) throws IOException
|
|---|
| 55 | {
|
|---|
| 56 | write (v ? 1 : 0);
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | public final void writeByte (int v) throws IOException
|
|---|
| 60 | {
|
|---|
| 61 | write (v & 0xff);
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | public final void writeShort (int v) throws IOException
|
|---|
| 65 | {
|
|---|
| 66 | write ((byte) (0xff & (v >> 8)));
|
|---|
| 67 | write ((byte) (0xff & v));
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | public final void writeChar (int v) throws IOException
|
|---|
| 71 | {
|
|---|
| 72 | write ((byte) (0xff & (v >> 8)));
|
|---|
| 73 | write ((byte) (0xff & v));
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | public final void writeInt (int v) throws IOException
|
|---|
| 77 | {
|
|---|
| 78 | write ((byte) (0xff & (v >> 24)));
|
|---|
| 79 | write ((byte) (0xff & (v >> 16)));
|
|---|
| 80 | write ((byte) (0xff & (v >> 8)));
|
|---|
| 81 | write ((byte) (0xff & v));
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | public final void writeLong (long v) throws IOException
|
|---|
| 85 | {
|
|---|
| 86 | write ((byte) (0xff & (v >> 56)));
|
|---|
| 87 | write ((byte) (0xff & (v >> 48)));
|
|---|
| 88 | write ((byte) (0xff & (v >> 40)));
|
|---|
| 89 | write ((byte) (0xff & (v >> 32)));
|
|---|
| 90 | write ((byte) (0xff & (v >> 24)));
|
|---|
| 91 | write ((byte) (0xff & (v >> 16)));
|
|---|
| 92 | write ((byte) (0xff & (v >> 8)));
|
|---|
| 93 | write ((byte) (0xff & v));
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | public final void writeFloat (float v) throws IOException
|
|---|
| 97 | {
|
|---|
| 98 | writeInt (Float.floatToIntBits(v));
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | public final void writeDouble (double v) throws IOException
|
|---|
| 102 | {
|
|---|
| 103 | writeLong (Double.doubleToLongBits(v));
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | public final void writeBytes (String s) throws IOException
|
|---|
| 107 | {
|
|---|
| 108 | int len = s.length();
|
|---|
| 109 | for (int i = 0; i < len; ++i)
|
|---|
| 110 | writeByte (s.charAt(i));
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | public final void writeChars (String s) throws IOException
|
|---|
| 114 | {
|
|---|
| 115 | int len = s.length();
|
|---|
| 116 | for (int i = 0; i < len; ++i)
|
|---|
| 117 | writeChar (s.charAt(i));
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | public final void writeUTF (String s) throws IOException
|
|---|
| 121 | {
|
|---|
| 122 | int len = s.length();
|
|---|
| 123 | int sum = 0;
|
|---|
| 124 |
|
|---|
| 125 | for (int i = 0; i < len && sum <= 65535; ++i)
|
|---|
| 126 | {
|
|---|
| 127 | char c = s.charAt(i);
|
|---|
| 128 | if (c >= '\u0001' && c <= '\u007f')
|
|---|
| 129 | sum += 1;
|
|---|
| 130 | else if (c == '\u0000' || (c >= '\u0080' && c <= '\u07ff'))
|
|---|
| 131 | sum += 2;
|
|---|
| 132 | else
|
|---|
| 133 | sum += 3;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | if (sum > 65535)
|
|---|
| 137 | throw new UTFDataFormatException ();
|
|---|
| 138 |
|
|---|
| 139 | writeShort (sum);
|
|---|
| 140 |
|
|---|
| 141 | for (int i = 0; i < len; ++i)
|
|---|
| 142 | {
|
|---|
| 143 | char c = s.charAt(i);
|
|---|
| 144 | if (c >= '\u0001' && c <= '\u007f')
|
|---|
| 145 | write (c);
|
|---|
| 146 | else if (c == '\u0000' || (c >= '\u0080' && c <= '\u07ff'))
|
|---|
| 147 | {
|
|---|
| 148 | write (0xc0 | (0x1f & (c >> 6)));
|
|---|
| 149 | write (0x80 | (0x3f & c));
|
|---|
| 150 | }
|
|---|
| 151 | else
|
|---|
| 152 | {
|
|---|
| 153 | // JSL says the first byte should be or'd with 0xc0, but
|
|---|
| 154 | // that is a typo. Unicode says 0xe0, and that is what is
|
|---|
| 155 | // consistent with DataInputStream.
|
|---|
| 156 | write (0xe0 | (0x0f & (c >> 12)));
|
|---|
| 157 | write (0x80 | (0x3f & (c >> 6)));
|
|---|
| 158 | write (0x80 | (0x3f & c));
|
|---|
| 159 | }
|
|---|
| 160 | }
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | // Number of bytes written so far.
|
|---|
| 164 | protected int written;
|
|---|
| 165 | }
|
|---|