FSF GCC merge 02/23/03
[official-gcc.git] / libjava / java / io / DataOutputStream.java
blob887761df356241c08bc53d182a59366a5519bdbb
1 // DataOutputStream.java - Output filter that implements DataOutput
3 /* Copyright (C) 1998, 1999 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 package java.io;
13 /**
14 * @author Tom Tromey <tromey@cygnus.com>
15 * @date September 24, 1998
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.
23 public class DataOutputStream extends FilterOutputStream implements DataOutput
25 public DataOutputStream (OutputStream out)
27 super (out);
28 written = 0;
31 public void flush () throws IOException
33 out.flush();
36 public final int size ()
38 return written;
41 public synchronized void write (int b) throws IOException
43 out.write(b);
44 ++written;
47 public synchronized void write (byte[] b, int off, int len)
48 throws IOException, NullPointerException, IndexOutOfBoundsException
50 out.write(b, off, len);
51 written += len;
54 public final void writeBoolean (boolean v) throws IOException
56 write (v ? 1 : 0);
59 public final void writeByte (int v) throws IOException
61 write (v & 0xff);
64 public final void writeShort (int v) throws IOException
66 write ((byte) (0xff & (v >> 8)));
67 write ((byte) (0xff & v));
70 public final void writeChar (int v) throws IOException
72 write ((byte) (0xff & (v >> 8)));
73 write ((byte) (0xff & v));
76 public final void writeInt (int v) throws IOException
78 write ((byte) (0xff & (v >> 24)));
79 write ((byte) (0xff & (v >> 16)));
80 write ((byte) (0xff & (v >> 8)));
81 write ((byte) (0xff & v));
84 public final void writeLong (long v) throws IOException
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));
96 public final void writeFloat (float v) throws IOException
98 writeInt (Float.floatToIntBits(v));
101 public final void writeDouble (double v) throws IOException
103 writeLong (Double.doubleToLongBits(v));
106 public final void writeBytes (String s) throws IOException
108 int len = s.length();
109 for (int i = 0; i < len; ++i)
110 writeByte (s.charAt(i));
113 public final void writeChars (String s) throws IOException
115 int len = s.length();
116 for (int i = 0; i < len; ++i)
117 writeChar (s.charAt(i));
120 public final void writeUTF (String s) throws IOException
122 int len = s.length();
123 int sum = 0;
125 for (int i = 0; i < len && sum <= 65535; ++i)
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;
136 if (sum > 65535)
137 throw new UTFDataFormatException ();
139 writeShort (sum);
141 for (int i = 0; i < len; ++i)
143 char c = s.charAt(i);
144 if (c >= '\u0001' && c <= '\u007f')
145 write (c);
146 else if (c == '\u0000' || (c >= '\u0080' && c <= '\u07ff'))
148 write (0xc0 | (0x1f & (c >> 6)));
149 write (0x80 | (0x3f & c));
151 else
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));
163 // Number of bytes written so far.
164 protected int written;