author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 58288 | 48e480e56aad |
permissions | -rw-r--r-- |
2 | 1 |
/* |
54309
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.io; |
|
27 |
||
28 |
/** |
|
29 |
* A data output stream lets an application write primitive Java data |
|
30 |
* types to an output stream in a portable way. An application can |
|
31 |
* then use a data input stream to read the data back in. |
|
32 |
* |
|
33 |
* @author unascribed |
|
34 |
* @see java.io.DataInputStream |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
5506
diff
changeset
|
35 |
* @since 1.0 |
2 | 36 |
*/ |
37 |
public |
|
38 |
class DataOutputStream extends FilterOutputStream implements DataOutput { |
|
39 |
/** |
|
40 |
* The number of bytes written to the data output stream so far. |
|
41 |
* If this counter overflows, it will be wrapped to Integer.MAX_VALUE. |
|
42 |
*/ |
|
43 |
protected int written; |
|
44 |
||
45 |
/** |
|
46 |
* bytearr is initialized on demand by writeUTF |
|
47 |
*/ |
|
48 |
private byte[] bytearr = null; |
|
49 |
||
50 |
/** |
|
51 |
* Creates a new data output stream to write data to the specified |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
52 |
* underlying output stream. The counter {@code written} is |
2 | 53 |
* set to zero. |
54 |
* |
|
55 |
* @param out the underlying output stream, to be saved for later |
|
56 |
* use. |
|
57 |
* @see java.io.FilterOutputStream#out |
|
58 |
*/ |
|
59 |
public DataOutputStream(OutputStream out) { |
|
60 |
super(out); |
|
61 |
} |
|
62 |
||
63 |
/** |
|
64 |
* Increases the written counter by the specified value |
|
65 |
* until it reaches Integer.MAX_VALUE. |
|
66 |
*/ |
|
67 |
private void incCount(int value) { |
|
68 |
int temp = written + value; |
|
69 |
if (temp < 0) { |
|
70 |
temp = Integer.MAX_VALUE; |
|
71 |
} |
|
72 |
written = temp; |
|
73 |
} |
|
74 |
||
75 |
/** |
|
76 |
* Writes the specified byte (the low eight bits of the argument |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
77 |
* {@code b}) to the underlying output stream. If no exception |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
78 |
* is thrown, the counter {@code written} is incremented by |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
79 |
* {@code 1}. |
2 | 80 |
* <p> |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
81 |
* Implements the {@code write} method of {@code OutputStream}. |
2 | 82 |
* |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
83 |
* @param b the {@code byte} to be written. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54309
diff
changeset
|
84 |
* @throws IOException if an I/O error occurs. |
2 | 85 |
* @see java.io.FilterOutputStream#out |
86 |
*/ |
|
87 |
public synchronized void write(int b) throws IOException { |
|
88 |
out.write(b); |
|
89 |
incCount(1); |
|
90 |
} |
|
91 |
||
92 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
93 |
* Writes {@code len} bytes from the specified byte array |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
94 |
* starting at offset {@code off} to the underlying output stream. |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
95 |
* If no exception is thrown, the counter {@code written} is |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
96 |
* incremented by {@code len}. |
2 | 97 |
* |
98 |
* @param b the data. |
|
99 |
* @param off the start offset in the data. |
|
100 |
* @param len the number of bytes to write. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54309
diff
changeset
|
101 |
* @throws IOException if an I/O error occurs. |
2 | 102 |
* @see java.io.FilterOutputStream#out |
103 |
*/ |
|
104 |
public synchronized void write(byte b[], int off, int len) |
|
105 |
throws IOException |
|
106 |
{ |
|
107 |
out.write(b, off, len); |
|
108 |
incCount(len); |
|
109 |
} |
|
110 |
||
111 |
/** |
|
112 |
* Flushes this data output stream. This forces any buffered output |
|
113 |
* bytes to be written out to the stream. |
|
114 |
* <p> |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
115 |
* The {@code flush} method of {@code DataOutputStream} |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
116 |
* calls the {@code flush} method of its underlying output stream. |
2 | 117 |
* |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54309
diff
changeset
|
118 |
* @throws IOException if an I/O error occurs. |
2 | 119 |
* @see java.io.FilterOutputStream#out |
120 |
* @see java.io.OutputStream#flush() |
|
121 |
*/ |
|
122 |
public void flush() throws IOException { |
|
123 |
out.flush(); |
|
124 |
} |
|
125 |
||
126 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
127 |
* Writes a {@code boolean} to the underlying output stream as |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
128 |
* a 1-byte value. The value {@code true} is written out as the |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
129 |
* value {@code (byte)1}; the value {@code false} is |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
130 |
* written out as the value {@code (byte)0}. If no exception is |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
131 |
* thrown, the counter {@code written} is incremented by |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
132 |
* {@code 1}. |
2 | 133 |
* |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
134 |
* @param v a {@code boolean} value to be written. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54309
diff
changeset
|
135 |
* @throws IOException if an I/O error occurs. |
2 | 136 |
* @see java.io.FilterOutputStream#out |
137 |
*/ |
|
138 |
public final void writeBoolean(boolean v) throws IOException { |
|
139 |
out.write(v ? 1 : 0); |
|
140 |
incCount(1); |
|
141 |
} |
|
142 |
||
143 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
144 |
* Writes out a {@code byte} to the underlying output stream as |
2 | 145 |
* a 1-byte value. If no exception is thrown, the counter |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
146 |
* {@code written} is incremented by {@code 1}. |
2 | 147 |
* |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
148 |
* @param v a {@code byte} value to be written. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54309
diff
changeset
|
149 |
* @throws IOException if an I/O error occurs. |
2 | 150 |
* @see java.io.FilterOutputStream#out |
151 |
*/ |
|
152 |
public final void writeByte(int v) throws IOException { |
|
153 |
out.write(v); |
|
154 |
incCount(1); |
|
155 |
} |
|
156 |
||
157 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
158 |
* Writes a {@code short} to the underlying output stream as two |
2 | 159 |
* bytes, high byte first. If no exception is thrown, the counter |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
160 |
* {@code written} is incremented by {@code 2}. |
2 | 161 |
* |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
162 |
* @param v a {@code short} to be written. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54309
diff
changeset
|
163 |
* @throws IOException if an I/O error occurs. |
2 | 164 |
* @see java.io.FilterOutputStream#out |
165 |
*/ |
|
166 |
public final void writeShort(int v) throws IOException { |
|
167 |
out.write((v >>> 8) & 0xFF); |
|
168 |
out.write((v >>> 0) & 0xFF); |
|
169 |
incCount(2); |
|
170 |
} |
|
171 |
||
172 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
173 |
* Writes a {@code char} to the underlying output stream as a |
2 | 174 |
* 2-byte value, high byte first. If no exception is thrown, the |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
175 |
* counter {@code written} is incremented by {@code 2}. |
2 | 176 |
* |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
177 |
* @param v a {@code char} value to be written. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54309
diff
changeset
|
178 |
* @throws IOException if an I/O error occurs. |
2 | 179 |
* @see java.io.FilterOutputStream#out |
180 |
*/ |
|
181 |
public final void writeChar(int v) throws IOException { |
|
182 |
out.write((v >>> 8) & 0xFF); |
|
183 |
out.write((v >>> 0) & 0xFF); |
|
184 |
incCount(2); |
|
185 |
} |
|
186 |
||
187 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
188 |
* Writes an {@code int} to the underlying output stream as four |
2 | 189 |
* bytes, high byte first. If no exception is thrown, the counter |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
190 |
* {@code written} is incremented by {@code 4}. |
2 | 191 |
* |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
192 |
* @param v an {@code int} to be written. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54309
diff
changeset
|
193 |
* @throws IOException if an I/O error occurs. |
2 | 194 |
* @see java.io.FilterOutputStream#out |
195 |
*/ |
|
196 |
public final void writeInt(int v) throws IOException { |
|
197 |
out.write((v >>> 24) & 0xFF); |
|
198 |
out.write((v >>> 16) & 0xFF); |
|
199 |
out.write((v >>> 8) & 0xFF); |
|
200 |
out.write((v >>> 0) & 0xFF); |
|
201 |
incCount(4); |
|
202 |
} |
|
203 |
||
204 |
private byte writeBuffer[] = new byte[8]; |
|
205 |
||
206 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
207 |
* Writes a {@code long} to the underlying output stream as eight |
2 | 208 |
* bytes, high byte first. In no exception is thrown, the counter |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
209 |
* {@code written} is incremented by {@code 8}. |
2 | 210 |
* |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
211 |
* @param v a {@code long} to be written. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54309
diff
changeset
|
212 |
* @throws IOException if an I/O error occurs. |
2 | 213 |
* @see java.io.FilterOutputStream#out |
214 |
*/ |
|
215 |
public final void writeLong(long v) throws IOException { |
|
216 |
writeBuffer[0] = (byte)(v >>> 56); |
|
217 |
writeBuffer[1] = (byte)(v >>> 48); |
|
218 |
writeBuffer[2] = (byte)(v >>> 40); |
|
219 |
writeBuffer[3] = (byte)(v >>> 32); |
|
220 |
writeBuffer[4] = (byte)(v >>> 24); |
|
221 |
writeBuffer[5] = (byte)(v >>> 16); |
|
222 |
writeBuffer[6] = (byte)(v >>> 8); |
|
223 |
writeBuffer[7] = (byte)(v >>> 0); |
|
224 |
out.write(writeBuffer, 0, 8); |
|
225 |
incCount(8); |
|
226 |
} |
|
227 |
||
228 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
229 |
* Converts the float argument to an {@code int} using the |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
230 |
* {@code floatToIntBits} method in class {@code Float}, |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
231 |
* and then writes that {@code int} value to the underlying |
2 | 232 |
* output stream as a 4-byte quantity, high byte first. If no |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
233 |
* exception is thrown, the counter {@code written} is |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
234 |
* incremented by {@code 4}. |
2 | 235 |
* |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
236 |
* @param v a {@code float} value to be written. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54309
diff
changeset
|
237 |
* @throws IOException if an I/O error occurs. |
2 | 238 |
* @see java.io.FilterOutputStream#out |
239 |
* @see java.lang.Float#floatToIntBits(float) |
|
240 |
*/ |
|
241 |
public final void writeFloat(float v) throws IOException { |
|
242 |
writeInt(Float.floatToIntBits(v)); |
|
243 |
} |
|
244 |
||
245 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
246 |
* Converts the double argument to a {@code long} using the |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
247 |
* {@code doubleToLongBits} method in class {@code Double}, |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
248 |
* and then writes that {@code long} value to the underlying |
2 | 249 |
* output stream as an 8-byte quantity, high byte first. If no |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
250 |
* exception is thrown, the counter {@code written} is |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
251 |
* incremented by {@code 8}. |
2 | 252 |
* |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
253 |
* @param v a {@code double} value to be written. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54309
diff
changeset
|
254 |
* @throws IOException if an I/O error occurs. |
2 | 255 |
* @see java.io.FilterOutputStream#out |
256 |
* @see java.lang.Double#doubleToLongBits(double) |
|
257 |
*/ |
|
258 |
public final void writeDouble(double v) throws IOException { |
|
259 |
writeLong(Double.doubleToLongBits(v)); |
|
260 |
} |
|
261 |
||
262 |
/** |
|
263 |
* Writes out the string to the underlying output stream as a |
|
264 |
* sequence of bytes. Each character in the string is written out, in |
|
265 |
* sequence, by discarding its high eight bits. If no exception is |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
266 |
* thrown, the counter {@code written} is incremented by the |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
267 |
* length of {@code s}. |
2 | 268 |
* |
269 |
* @param s a string of bytes to be written. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54309
diff
changeset
|
270 |
* @throws IOException if an I/O error occurs. |
2 | 271 |
* @see java.io.FilterOutputStream#out |
272 |
*/ |
|
273 |
public final void writeBytes(String s) throws IOException { |
|
274 |
int len = s.length(); |
|
275 |
for (int i = 0 ; i < len ; i++) { |
|
276 |
out.write((byte)s.charAt(i)); |
|
277 |
} |
|
278 |
incCount(len); |
|
279 |
} |
|
280 |
||
281 |
/** |
|
282 |
* Writes a string to the underlying output stream as a sequence of |
|
283 |
* characters. Each character is written to the data output stream as |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
284 |
* if by the {@code writeChar} method. If no exception is |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
285 |
* thrown, the counter {@code written} is incremented by twice |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
286 |
* the length of {@code s}. |
2 | 287 |
* |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
288 |
* @param s a {@code String} value to be written. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54309
diff
changeset
|
289 |
* @throws IOException if an I/O error occurs. |
2 | 290 |
* @see java.io.DataOutputStream#writeChar(int) |
291 |
* @see java.io.FilterOutputStream#out |
|
292 |
*/ |
|
293 |
public final void writeChars(String s) throws IOException { |
|
294 |
int len = s.length(); |
|
295 |
for (int i = 0 ; i < len ; i++) { |
|
296 |
int v = s.charAt(i); |
|
297 |
out.write((v >>> 8) & 0xFF); |
|
298 |
out.write((v >>> 0) & 0xFF); |
|
299 |
} |
|
300 |
incCount(len * 2); |
|
301 |
} |
|
302 |
||
303 |
/** |
|
304 |
* Writes a string to the underlying output stream using |
|
305 |
* <a href="DataInput.html#modified-utf-8">modified UTF-8</a> |
|
306 |
* encoding in a machine-independent manner. |
|
307 |
* <p> |
|
308 |
* First, two bytes are written to the output stream as if by the |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
309 |
* {@code writeShort} method giving the number of bytes to |
2 | 310 |
* follow. This value is the number of bytes actually written out, |
311 |
* not the length of the string. Following the length, each character |
|
312 |
* of the string is output, in sequence, using the modified UTF-8 encoding |
|
313 |
* for the character. If no exception is thrown, the counter |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
314 |
* {@code written} is incremented by the total number of |
2 | 315 |
* bytes written to the output stream. This will be at least two |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
316 |
* plus the length of {@code str}, and at most two plus |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
317 |
* thrice the length of {@code str}. |
2 | 318 |
* |
319 |
* @param str a string to be written. |
|
54309
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
320 |
* @throws UTFDataFormatException if the modified UTF-8 encoding of |
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
321 |
* {@code str} would exceed 65535 bytes in length |
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
322 |
* @throws IOException if some other I/O error occurs. |
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
323 |
* @see #writeChars(String) |
2 | 324 |
*/ |
325 |
public final void writeUTF(String str) throws IOException { |
|
326 |
writeUTF(str, this); |
|
327 |
} |
|
328 |
||
329 |
/** |
|
330 |
* Writes a string to the specified DataOutput using |
|
331 |
* <a href="DataInput.html#modified-utf-8">modified UTF-8</a> |
|
332 |
* encoding in a machine-independent manner. |
|
333 |
* <p> |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
334 |
* First, two bytes are written to out as if by the {@code writeShort} |
2 | 335 |
* method giving the number of bytes to follow. This value is the number of |
336 |
* bytes actually written out, not the length of the string. Following the |
|
337 |
* length, each character of the string is output, in sequence, using the |
|
338 |
* modified UTF-8 encoding for the character. If no exception is thrown, the |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
339 |
* counter {@code written} is incremented by the total number of |
2 | 340 |
* bytes written to the output stream. This will be at least two |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
341 |
* plus the length of {@code str}, and at most two plus |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
342 |
* thrice the length of {@code str}. |
2 | 343 |
* |
344 |
* @param str a string to be written. |
|
345 |
* @param out destination to write to |
|
346 |
* @return The number of bytes written out. |
|
54309
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
347 |
* @throws UTFDataFormatException if the modified UTF-8 encoding of |
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
348 |
* {@code str} would exceed 65535 bytes in length |
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
349 |
* @throws IOException if some other I/O error occurs. |
2 | 350 |
*/ |
351 |
static int writeUTF(String str, DataOutput out) throws IOException { |
|
54309
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
352 |
final int strlen = str.length(); |
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
353 |
int utflen = strlen; // optimized for ASCII |
2 | 354 |
|
355 |
for (int i = 0; i < strlen; i++) { |
|
54309
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
356 |
int c = str.charAt(i); |
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
357 |
if (c >= 0x80 || c == 0) |
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
358 |
utflen += (c >= 0x800) ? 2 : 1; |
2 | 359 |
} |
360 |
||
54309
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
361 |
if (utflen > 65535 || /* overflow */ utflen < strlen) |
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
362 |
throw new UTFDataFormatException(tooLongMsg(str, utflen)); |
2 | 363 |
|
54309
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
364 |
final byte[] bytearr; |
2 | 365 |
if (out instanceof DataOutputStream) { |
366 |
DataOutputStream dos = (DataOutputStream)out; |
|
54309
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
367 |
if (dos.bytearr == null || (dos.bytearr.length < (utflen + 2))) |
2 | 368 |
dos.bytearr = new byte[(utflen*2) + 2]; |
369 |
bytearr = dos.bytearr; |
|
370 |
} else { |
|
54309
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
371 |
bytearr = new byte[utflen + 2]; |
2 | 372 |
} |
373 |
||
54309
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
374 |
int count = 0; |
2 | 375 |
bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF); |
376 |
bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF); |
|
377 |
||
54309
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
378 |
int i = 0; |
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
379 |
for (i = 0; i < strlen; i++) { // optimized for initial run of ASCII |
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
380 |
int c = str.charAt(i); |
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
381 |
if (c >= 0x80 || c == 0) break; |
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
382 |
bytearr[count++] = (byte) c; |
2 | 383 |
} |
384 |
||
54309
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
385 |
for (; i < strlen; i++) { |
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
386 |
int c = str.charAt(i); |
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
387 |
if (c < 0x80 && c != 0) { |
2 | 388 |
bytearr[count++] = (byte) c; |
54309
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
389 |
} else if (c >= 0x800) { |
2 | 390 |
bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F)); |
391 |
bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F)); |
|
392 |
bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); |
|
393 |
} else { |
|
394 |
bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F)); |
|
395 |
bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); |
|
396 |
} |
|
397 |
} |
|
54309
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
398 |
out.write(bytearr, 0, utflen + 2); |
2 | 399 |
return utflen + 2; |
400 |
} |
|
401 |
||
54309
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
402 |
private static String tooLongMsg(String s, int bits32) { |
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
403 |
int slen = s.length(); |
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
404 |
String head = s.substring(0, 8); |
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
405 |
String tail = s.substring(slen - 8, slen); |
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
406 |
// handle int overflow with max 3x expansion |
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
407 |
long actualLength = (long)slen + Integer.toUnsignedLong(bits32 - slen); |
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
408 |
return "encoded string (" + head + "..." + tail + ") too long: " |
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
409 |
+ actualLength + " bytes"; |
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
410 |
} |
c4c225b49c5f
8219196: DataOutputStream.writeUTF may throw unexpected exceptions
bpb
parents:
47216
diff
changeset
|
411 |
|
2 | 412 |
/** |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
413 |
* Returns the current value of the counter {@code written}, |
2 | 414 |
* the number of bytes written to this data output stream so far. |
415 |
* If the counter overflows, it will be wrapped to Integer.MAX_VALUE. |
|
416 |
* |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
417 |
* @return the value of the {@code written} field. |
2 | 418 |
* @see java.io.DataOutputStream#written |
419 |
*/ |
|
420 |
public final int size() { |
|
421 |
return written; |
|
422 |
} |
|
423 |
} |