author | avstepan |
Thu, 06 Aug 2015 13:20:13 +0300 | |
changeset 32033 | bf24e33c7919 |
parent 26217 | e037c9104788 |
child 40410 | 5fd4a1f809f8 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
21334
diff
changeset
|
2 |
* Copyright (c) 1994, 2013, 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 |
import java.util.Arrays; |
|
29 |
||
30 |
/** |
|
31 |
* This class implements an output stream in which the data is |
|
32 |
* written into a byte array. The buffer automatically grows as data |
|
33 |
* is written to it. |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
26217
diff
changeset
|
34 |
* The data can be retrieved using {@code toByteArray()} and |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
26217
diff
changeset
|
35 |
* {@code toString()}. |
2 | 36 |
* <p> |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
26217
diff
changeset
|
37 |
* Closing a {@code ByteArrayOutputStream} has no effect. The methods in |
2 | 38 |
* this class can be called after the stream has been closed without |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
26217
diff
changeset
|
39 |
* generating an {@code IOException}. |
2 | 40 |
* |
41 |
* @author Arthur van Hoff |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
23010
diff
changeset
|
42 |
* @since 1.0 |
2 | 43 |
*/ |
44 |
||
45 |
public class ByteArrayOutputStream extends OutputStream { |
|
46 |
||
47 |
/** |
|
48 |
* The buffer where data is stored. |
|
49 |
*/ |
|
50 |
protected byte buf[]; |
|
51 |
||
52 |
/** |
|
53 |
* The number of valid bytes in the buffer. |
|
54 |
*/ |
|
55 |
protected int count; |
|
56 |
||
57 |
/** |
|
58 |
* Creates a new byte array output stream. The buffer capacity is |
|
59 |
* initially 32 bytes, though its size increases if necessary. |
|
60 |
*/ |
|
61 |
public ByteArrayOutputStream() { |
|
62 |
this(32); |
|
63 |
} |
|
64 |
||
65 |
/** |
|
66 |
* Creates a new byte array output stream, with a buffer capacity of |
|
67 |
* the specified size, in bytes. |
|
68 |
* |
|
69 |
* @param size the initial size. |
|
70 |
* @exception IllegalArgumentException if size is negative. |
|
71 |
*/ |
|
72 |
public ByteArrayOutputStream(int size) { |
|
73 |
if (size < 0) { |
|
74 |
throw new IllegalArgumentException("Negative initial size: " |
|
75 |
+ size); |
|
76 |
} |
|
77 |
buf = new byte[size]; |
|
78 |
} |
|
79 |
||
80 |
/** |
|
5466 | 81 |
* Increases the capacity if necessary to ensure that it can hold |
82 |
* at least the number of elements specified by the minimum |
|
83 |
* capacity argument. |
|
84 |
* |
|
85 |
* @param minCapacity the desired minimum capacity |
|
86 |
* @throws OutOfMemoryError if {@code minCapacity < 0}. This is |
|
87 |
* interpreted as a request for the unsatisfiably large capacity |
|
88 |
* {@code (long) Integer.MAX_VALUE + (minCapacity - Integer.MAX_VALUE)}. |
|
89 |
*/ |
|
90 |
private void ensureCapacity(int minCapacity) { |
|
91 |
// overflow-conscious code |
|
92 |
if (minCapacity - buf.length > 0) |
|
93 |
grow(minCapacity); |
|
94 |
} |
|
95 |
||
96 |
/** |
|
26217
e037c9104788
8055949: ByteArrayOutputStream capacity should be maximal array size permitted by VM
martin
parents:
25859
diff
changeset
|
97 |
* The maximum size of array to allocate. |
e037c9104788
8055949: ByteArrayOutputStream capacity should be maximal array size permitted by VM
martin
parents:
25859
diff
changeset
|
98 |
* Some VMs reserve some header words in an array. |
e037c9104788
8055949: ByteArrayOutputStream capacity should be maximal array size permitted by VM
martin
parents:
25859
diff
changeset
|
99 |
* Attempts to allocate larger arrays may result in |
e037c9104788
8055949: ByteArrayOutputStream capacity should be maximal array size permitted by VM
martin
parents:
25859
diff
changeset
|
100 |
* OutOfMemoryError: Requested array size exceeds VM limit |
e037c9104788
8055949: ByteArrayOutputStream capacity should be maximal array size permitted by VM
martin
parents:
25859
diff
changeset
|
101 |
*/ |
e037c9104788
8055949: ByteArrayOutputStream capacity should be maximal array size permitted by VM
martin
parents:
25859
diff
changeset
|
102 |
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; |
e037c9104788
8055949: ByteArrayOutputStream capacity should be maximal array size permitted by VM
martin
parents:
25859
diff
changeset
|
103 |
|
e037c9104788
8055949: ByteArrayOutputStream capacity should be maximal array size permitted by VM
martin
parents:
25859
diff
changeset
|
104 |
/** |
5466 | 105 |
* Increases the capacity to ensure that it can hold at least the |
106 |
* number of elements specified by the minimum capacity argument. |
|
107 |
* |
|
108 |
* @param minCapacity the desired minimum capacity |
|
109 |
*/ |
|
110 |
private void grow(int minCapacity) { |
|
111 |
// overflow-conscious code |
|
112 |
int oldCapacity = buf.length; |
|
113 |
int newCapacity = oldCapacity << 1; |
|
114 |
if (newCapacity - minCapacity < 0) |
|
115 |
newCapacity = minCapacity; |
|
26217
e037c9104788
8055949: ByteArrayOutputStream capacity should be maximal array size permitted by VM
martin
parents:
25859
diff
changeset
|
116 |
if (newCapacity - MAX_ARRAY_SIZE > 0) |
e037c9104788
8055949: ByteArrayOutputStream capacity should be maximal array size permitted by VM
martin
parents:
25859
diff
changeset
|
117 |
newCapacity = hugeCapacity(minCapacity); |
5466 | 118 |
buf = Arrays.copyOf(buf, newCapacity); |
119 |
} |
|
120 |
||
26217
e037c9104788
8055949: ByteArrayOutputStream capacity should be maximal array size permitted by VM
martin
parents:
25859
diff
changeset
|
121 |
private static int hugeCapacity(int minCapacity) { |
e037c9104788
8055949: ByteArrayOutputStream capacity should be maximal array size permitted by VM
martin
parents:
25859
diff
changeset
|
122 |
if (minCapacity < 0) // overflow |
e037c9104788
8055949: ByteArrayOutputStream capacity should be maximal array size permitted by VM
martin
parents:
25859
diff
changeset
|
123 |
throw new OutOfMemoryError(); |
e037c9104788
8055949: ByteArrayOutputStream capacity should be maximal array size permitted by VM
martin
parents:
25859
diff
changeset
|
124 |
return (minCapacity > MAX_ARRAY_SIZE) ? |
e037c9104788
8055949: ByteArrayOutputStream capacity should be maximal array size permitted by VM
martin
parents:
25859
diff
changeset
|
125 |
Integer.MAX_VALUE : |
e037c9104788
8055949: ByteArrayOutputStream capacity should be maximal array size permitted by VM
martin
parents:
25859
diff
changeset
|
126 |
MAX_ARRAY_SIZE; |
e037c9104788
8055949: ByteArrayOutputStream capacity should be maximal array size permitted by VM
martin
parents:
25859
diff
changeset
|
127 |
} |
e037c9104788
8055949: ByteArrayOutputStream capacity should be maximal array size permitted by VM
martin
parents:
25859
diff
changeset
|
128 |
|
5466 | 129 |
/** |
2 | 130 |
* Writes the specified byte to this byte array output stream. |
131 |
* |
|
132 |
* @param b the byte to be written. |
|
133 |
*/ |
|
134 |
public synchronized void write(int b) { |
|
5466 | 135 |
ensureCapacity(count + 1); |
136 |
buf[count] = (byte) b; |
|
137 |
count += 1; |
|
2 | 138 |
} |
139 |
||
140 |
/** |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
26217
diff
changeset
|
141 |
* Writes {@code len} bytes from the specified byte array |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
26217
diff
changeset
|
142 |
* starting at offset {@code off} to this byte array output stream. |
2 | 143 |
* |
144 |
* @param b the data. |
|
145 |
* @param off the start offset in the data. |
|
146 |
* @param len the number of bytes to write. |
|
147 |
*/ |
|
148 |
public synchronized void write(byte b[], int off, int len) { |
|
149 |
if ((off < 0) || (off > b.length) || (len < 0) || |
|
5466 | 150 |
((off + len) - b.length > 0)) { |
2 | 151 |
throw new IndexOutOfBoundsException(); |
152 |
} |
|
5466 | 153 |
ensureCapacity(count + len); |
2 | 154 |
System.arraycopy(b, off, buf, count, len); |
5466 | 155 |
count += len; |
2 | 156 |
} |
157 |
||
158 |
/** |
|
159 |
* Writes the complete contents of this byte array output stream to |
|
160 |
* the specified output stream argument, as if by calling the output |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
26217
diff
changeset
|
161 |
* stream's write method using {@code out.write(buf, 0, count)}. |
2 | 162 |
* |
163 |
* @param out the output stream to which to write the data. |
|
164 |
* @exception IOException if an I/O error occurs. |
|
165 |
*/ |
|
166 |
public synchronized void writeTo(OutputStream out) throws IOException { |
|
167 |
out.write(buf, 0, count); |
|
168 |
} |
|
169 |
||
170 |
/** |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
26217
diff
changeset
|
171 |
* Resets the {@code count} field of this byte array output |
2 | 172 |
* stream to zero, so that all currently accumulated output in the |
173 |
* output stream is discarded. The output stream can be used again, |
|
174 |
* reusing the already allocated buffer space. |
|
175 |
* |
|
176 |
* @see java.io.ByteArrayInputStream#count |
|
177 |
*/ |
|
178 |
public synchronized void reset() { |
|
179 |
count = 0; |
|
180 |
} |
|
181 |
||
182 |
/** |
|
183 |
* Creates a newly allocated byte array. Its size is the current |
|
184 |
* size of this output stream and the valid contents of the buffer |
|
185 |
* have been copied into it. |
|
186 |
* |
|
187 |
* @return the current contents of this output stream, as a byte array. |
|
188 |
* @see java.io.ByteArrayOutputStream#size() |
|
189 |
*/ |
|
190 |
public synchronized byte toByteArray()[] { |
|
191 |
return Arrays.copyOf(buf, count); |
|
192 |
} |
|
193 |
||
194 |
/** |
|
195 |
* Returns the current size of the buffer. |
|
196 |
* |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
26217
diff
changeset
|
197 |
* @return the value of the {@code count} field, which is the number |
2 | 198 |
* of valid bytes in this output stream. |
199 |
* @see java.io.ByteArrayOutputStream#count |
|
200 |
*/ |
|
201 |
public synchronized int size() { |
|
202 |
return count; |
|
203 |
} |
|
204 |
||
205 |
/** |
|
206 |
* Converts the buffer's contents into a string decoding bytes using the |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
26217
diff
changeset
|
207 |
* platform's default character set. The length of the new {@code String} |
2 | 208 |
* is a function of the character set, and hence may not be equal to the |
209 |
* size of the buffer. |
|
210 |
* |
|
211 |
* <p> This method always replaces malformed-input and unmappable-character |
|
212 |
* sequences with the default replacement string for the platform's |
|
213 |
* default character set. The {@linkplain java.nio.charset.CharsetDecoder} |
|
214 |
* class should be used when more control over the decoding process is |
|
215 |
* required. |
|
216 |
* |
|
217 |
* @return String decoded from the buffer's contents. |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
23010
diff
changeset
|
218 |
* @since 1.1 |
2 | 219 |
*/ |
220 |
public synchronized String toString() { |
|
221 |
return new String(buf, 0, count); |
|
222 |
} |
|
223 |
||
224 |
/** |
|
225 |
* Converts the buffer's contents into a string by decoding the bytes using |
|
13659
18f4e55bb34b
7193710: ByteArrayOutputStream Javadoc contains unclosed <code> element
dxu
parents:
7668
diff
changeset
|
226 |
* the named {@link java.nio.charset.Charset charset}. The length of the new |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
26217
diff
changeset
|
227 |
* {@code String} is a function of the charset, and hence may not be equal |
13659
18f4e55bb34b
7193710: ByteArrayOutputStream Javadoc contains unclosed <code> element
dxu
parents:
7668
diff
changeset
|
228 |
* to the length of the byte array. |
2 | 229 |
* |
230 |
* <p> This method always replaces malformed-input and unmappable-character |
|
231 |
* sequences with this charset's default replacement string. The {@link |
|
232 |
* java.nio.charset.CharsetDecoder} class should be used when more control |
|
233 |
* over the decoding process is required. |
|
234 |
* |
|
13659
18f4e55bb34b
7193710: ByteArrayOutputStream Javadoc contains unclosed <code> element
dxu
parents:
7668
diff
changeset
|
235 |
* @param charsetName the name of a supported |
18f4e55bb34b
7193710: ByteArrayOutputStream Javadoc contains unclosed <code> element
dxu
parents:
7668
diff
changeset
|
236 |
* {@link java.nio.charset.Charset charset} |
18f4e55bb34b
7193710: ByteArrayOutputStream Javadoc contains unclosed <code> element
dxu
parents:
7668
diff
changeset
|
237 |
* @return String decoded from the buffer's contents. |
2 | 238 |
* @exception UnsupportedEncodingException |
239 |
* If the named charset is not supported |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
23010
diff
changeset
|
240 |
* @since 1.1 |
2 | 241 |
*/ |
242 |
public synchronized String toString(String charsetName) |
|
243 |
throws UnsupportedEncodingException |
|
244 |
{ |
|
245 |
return new String(buf, 0, count, charsetName); |
|
246 |
} |
|
247 |
||
248 |
/** |
|
249 |
* Creates a newly allocated string. Its size is the current size of |
|
250 |
* the output stream and the valid contents of the buffer have been |
|
251 |
* copied into it. Each character <i>c</i> in the resulting string is |
|
252 |
* constructed from the corresponding element <i>b</i> in the byte |
|
253 |
* array such that: |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
26217
diff
changeset
|
254 |
* <blockquote><pre>{@code |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
26217
diff
changeset
|
255 |
* c == (char)(((hibyte & 0xff) << 8) | (b & 0xff)) |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
26217
diff
changeset
|
256 |
* }</pre></blockquote> |
2 | 257 |
* |
258 |
* @deprecated This method does not properly convert bytes into characters. |
|
259 |
* As of JDK 1.1, the preferred way to do this is via the |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
26217
diff
changeset
|
260 |
* {@code toString(String enc)} method, which takes an encoding-name |
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
26217
diff
changeset
|
261 |
* argument, or the {@code toString()} method, which uses the |
2 | 262 |
* platform's default character encoding. |
263 |
* |
|
264 |
* @param hibyte the high byte of each resulting Unicode character. |
|
265 |
* @return the current contents of the output stream, as a string. |
|
266 |
* @see java.io.ByteArrayOutputStream#size() |
|
267 |
* @see java.io.ByteArrayOutputStream#toString(String) |
|
268 |
* @see java.io.ByteArrayOutputStream#toString() |
|
269 |
*/ |
|
270 |
@Deprecated |
|
271 |
public synchronized String toString(int hibyte) { |
|
272 |
return new String(buf, hibyte, 0, count); |
|
273 |
} |
|
274 |
||
275 |
/** |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
26217
diff
changeset
|
276 |
* Closing a {@code ByteArrayOutputStream} has no effect. The methods in |
2 | 277 |
* this class can be called after the stream has been closed without |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
26217
diff
changeset
|
278 |
* generating an {@code IOException}. |
2 | 279 |
*/ |
280 |
public void close() throws IOException { |
|
281 |
} |
|
282 |
||
283 |
} |