| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ByteArrayOutputStream |
|
| 2.75;2.75 |
| 1 | /* | |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
| 3 | * contributor license agreements. See the NOTICE file distributed with | |
| 4 | * this work for additional information regarding copyright ownership. | |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
| 6 | * (the "License"); you may not use this file except in compliance with | |
| 7 | * the License. You may obtain a copy of the License at | |
| 8 | * | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 10 | * | |
| 11 | * Unless required by applicable law or agreed to in writing, software | |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 14 | * See the License for the specific language governing permissions and | |
| 15 | * limitations under the License. | |
| 16 | */ | |
| 17 | package org.apache.commons.io.output; | |
| 18 | ||
| 19 | import java.io.ByteArrayInputStream; | |
| 20 | import java.io.IOException; | |
| 21 | import java.io.InputStream; | |
| 22 | import java.io.OutputStream; | |
| 23 | import java.io.SequenceInputStream; | |
| 24 | import java.io.UnsupportedEncodingException; | |
| 25 | import java.nio.charset.Charset; | |
| 26 | import java.util.ArrayList; | |
| 27 | import java.util.Collections; | |
| 28 | import java.util.List; | |
| 29 | ||
| 30 | import org.apache.commons.io.input.ClosedInputStream; | |
| 31 | ||
| 32 | /** | |
| 33 | * This class implements an output stream in which the data is | |
| 34 | * written into a byte array. The buffer automatically grows as data | |
| 35 | * is written to it. | |
| 36 | * <p> | |
| 37 | * The data can be retrieved using <code>toByteArray()</code> and | |
| 38 | * <code>toString()</code>. | |
| 39 | * <p> | |
| 40 | * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in | |
| 41 | * this class can be called after the stream has been closed without | |
| 42 | * generating an <tt>IOException</tt>. | |
| 43 | * <p> | |
| 44 | * This is an alternative implementation of the {@link java.io.ByteArrayOutputStream} | |
| 45 | * class. The original implementation only allocates 32 bytes at the beginning. | |
| 46 | * As this class is designed for heavy duty it starts at 1024 bytes. In contrast | |
| 47 | * to the original it doesn't reallocate the whole memory block but allocates | |
| 48 | * additional buffers. This way no buffers need to be garbage collected and | |
| 49 | * the contents don't have to be copied to the new buffer. This class is | |
| 50 | * designed to behave exactly like the original. The only exception is the | |
| 51 | * deprecated toString(int) method that has been ignored. | |
| 52 | * | |
| 53 | * @version $Id: ByteArrayOutputStream.java 1471767 2013-04-24 23:24:19Z sebb $ | |
| 54 | */ | |
| 55 | public class ByteArrayOutputStream extends OutputStream { | |
| 56 | ||
| 57 | /** A singleton empty byte array. */ | |
| 58 | 12 | private static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; |
| 59 | ||
| 60 | /** The list of buffers, which grows and never reduces. */ | |
| 61 | 220 | private final List<byte[]> buffers = new ArrayList<byte[]>(); |
| 62 | /** The index of the current buffer. */ | |
| 63 | private int currentBufferIndex; | |
| 64 | /** The total count of bytes in all the filled buffers. */ | |
| 65 | private int filledBufferSum; | |
| 66 | /** The current buffer. */ | |
| 67 | private byte[] currentBuffer; | |
| 68 | /** The total count of bytes written. */ | |
| 69 | private int count; | |
| 70 | /** Flag to indicate if the buffers can be reused after reset */ | |
| 71 | 220 | private boolean reuseBuffers = true; |
| 72 | ||
| 73 | /** | |
| 74 | * Creates a new byte array output stream. The buffer capacity is | |
| 75 | * initially 1024 bytes, though its size increases if necessary. | |
| 76 | */ | |
| 77 | public ByteArrayOutputStream() { | |
| 78 | 218 | this(1024); |
| 79 | 218 | } |
| 80 | ||
| 81 | /** | |
| 82 | * Creates a new byte array output stream, with a buffer capacity of | |
| 83 | * the specified size, in bytes. | |
| 84 | * | |
| 85 | * @param size the initial size | |
| 86 | * @throws IllegalArgumentException if size is negative | |
| 87 | */ | |
| 88 | 220 | public ByteArrayOutputStream(final int size) { |
| 89 | 220 | if (size < 0) { |
| 90 | 0 | throw new IllegalArgumentException( |
| 91 | "Negative initial size: " + size); | |
| 92 | } | |
| 93 | 220 | synchronized (this) { |
| 94 | 220 | needNewBuffer(size); |
| 95 | 220 | } |
| 96 | 220 | } |
| 97 | ||
| 98 | /** | |
| 99 | * Makes a new buffer available either by allocating | |
| 100 | * a new one or re-cycling an existing one. | |
| 101 | * | |
| 102 | * @param newcount the size of the buffer if one is created | |
| 103 | */ | |
| 104 | private void needNewBuffer(final int newcount) { | |
| 105 | 486 | if (currentBufferIndex < buffers.size() - 1) { |
| 106 | //Recycling old buffer | |
| 107 | 3 | filledBufferSum += currentBuffer.length; |
| 108 | ||
| 109 | 3 | currentBufferIndex++; |
| 110 | 3 | currentBuffer = buffers.get(currentBufferIndex); |
| 111 | } else { | |
| 112 | //Creating new buffer | |
| 113 | int newBufferSize; | |
| 114 | 483 | if (currentBuffer == null) { |
| 115 | 221 | newBufferSize = newcount; |
| 116 | 221 | filledBufferSum = 0; |
| 117 | } else { | |
| 118 | 262 | newBufferSize = Math.max( |
| 119 | currentBuffer.length << 1, | |
| 120 | newcount - filledBufferSum); | |
| 121 | 262 | filledBufferSum += currentBuffer.length; |
| 122 | } | |
| 123 | ||
| 124 | 483 | currentBufferIndex++; |
| 125 | 483 | currentBuffer = new byte[newBufferSize]; |
| 126 | 483 | buffers.add(currentBuffer); |
| 127 | } | |
| 128 | 486 | } |
| 129 | ||
| 130 | /** | |
| 131 | * Write the bytes to byte array. | |
| 132 | * @param b the bytes to write | |
| 133 | * @param off The start offset | |
| 134 | * @param len The number of bytes to write | |
| 135 | */ | |
| 136 | @Override | |
| 137 | public void write(final byte[] b, final int off, final int len) { | |
| 138 | 8669 | if ((off < 0) |
| 139 | || (off > b.length) | |
| 140 | || (len < 0) | |
| 141 | || ((off + len) > b.length) | |
| 142 | || ((off + len) < 0)) { | |
| 143 | 0 | throw new IndexOutOfBoundsException(); |
| 144 | 8669 | } else if (len == 0) { |
| 145 | 3 | return; |
| 146 | } | |
| 147 | 8666 | synchronized (this) { |
| 148 | 8666 | final int newcount = count + len; |
| 149 | 8666 | int remaining = len; |
| 150 | 8666 | int inBufferPos = count - filledBufferSum; |
| 151 | 17419 | while (remaining > 0) { |
| 152 | 8753 | final int part = Math.min(remaining, currentBuffer.length - inBufferPos); |
| 153 | 8753 | System.arraycopy(b, off + len - remaining, currentBuffer, inBufferPos, part); |
| 154 | 8753 | remaining -= part; |
| 155 | 8753 | if (remaining > 0) { |
| 156 | 87 | needNewBuffer(newcount); |
| 157 | 87 | inBufferPos = 0; |
| 158 | } | |
| 159 | 8753 | } |
| 160 | 8666 | count = newcount; |
| 161 | 8666 | } |
| 162 | 8666 | } |
| 163 | ||
| 164 | /** | |
| 165 | * Write a byte to byte array. | |
| 166 | * @param b the byte to write | |
| 167 | */ | |
| 168 | @Override | |
| 169 | public synchronized void write(final int b) { | |
| 170 | 356507 | int inBufferPos = count - filledBufferSum; |
| 171 | 356507 | if (inBufferPos == currentBuffer.length) { |
| 172 | 174 | needNewBuffer(count + 1); |
| 173 | 174 | inBufferPos = 0; |
| 174 | } | |
| 175 | 356507 | currentBuffer[inBufferPos] = (byte) b; |
| 176 | 356507 | count++; |
| 177 | 356507 | } |
| 178 | ||
| 179 | /** | |
| 180 | * Writes the entire contents of the specified input stream to this | |
| 181 | * byte stream. Bytes from the input stream are read directly into the | |
| 182 | * internal buffers of this streams. | |
| 183 | * | |
| 184 | * @param in the input stream to read from | |
| 185 | * @return total number of bytes read from the input stream | |
| 186 | * (and written to this stream) | |
| 187 | * @throws IOException if an I/O error occurs while reading the input stream | |
| 188 | * @since 1.4 | |
| 189 | */ | |
| 190 | public synchronized int write(final InputStream in) throws IOException { | |
| 191 | 2 | int readCount = 0; |
| 192 | 2 | int inBufferPos = count - filledBufferSum; |
| 193 | 2 | int n = in.read(currentBuffer, inBufferPos, currentBuffer.length - inBufferPos); |
| 194 | 8 | while (n != -1) { |
| 195 | 6 | readCount += n; |
| 196 | 6 | inBufferPos += n; |
| 197 | 6 | count += n; |
| 198 | 6 | if (inBufferPos == currentBuffer.length) { |
| 199 | 4 | needNewBuffer(currentBuffer.length); |
| 200 | 4 | inBufferPos = 0; |
| 201 | } | |
| 202 | 6 | n = in.read(currentBuffer, inBufferPos, currentBuffer.length - inBufferPos); |
| 203 | } | |
| 204 | 2 | return readCount; |
| 205 | } | |
| 206 | ||
| 207 | /** | |
| 208 | * Return the current size of the byte array. | |
| 209 | * @return the current size of the byte array | |
| 210 | */ | |
| 211 | public synchronized int size() { | |
| 212 | 64 | return count; |
| 213 | } | |
| 214 | ||
| 215 | /** | |
| 216 | * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in | |
| 217 | * this class can be called after the stream has been closed without | |
| 218 | * generating an <tt>IOException</tt>. | |
| 219 | * | |
| 220 | * @throws IOException never (this method should not declare this exception | |
| 221 | * but it has to now due to backwards compatibility) | |
| 222 | */ | |
| 223 | @Override | |
| 224 | public void close() throws IOException { | |
| 225 | //nop | |
| 226 | 7 | } |
| 227 | ||
| 228 | /** | |
| 229 | * @see java.io.ByteArrayOutputStream#reset() | |
| 230 | */ | |
| 231 | public synchronized void reset() { | |
| 232 | 3 | count = 0; |
| 233 | 3 | filledBufferSum = 0; |
| 234 | 3 | currentBufferIndex = 0; |
| 235 | 3 | if (reuseBuffers) { |
| 236 | 2 | currentBuffer = buffers.get(currentBufferIndex); |
| 237 | } else { | |
| 238 | //Throw away old buffers | |
| 239 | 1 | currentBuffer = null; |
| 240 | 1 | int size = buffers.get(0).length; |
| 241 | 1 | buffers.clear(); |
| 242 | 1 | needNewBuffer(size); |
| 243 | 1 | reuseBuffers = true; |
| 244 | } | |
| 245 | 3 | } |
| 246 | ||
| 247 | /** | |
| 248 | * Writes the entire contents of this byte stream to the | |
| 249 | * specified output stream. | |
| 250 | * | |
| 251 | * @param out the output stream to write to | |
| 252 | * @throws IOException if an I/O error occurs, such as if the stream is closed | |
| 253 | * @see java.io.ByteArrayOutputStream#writeTo(OutputStream) | |
| 254 | */ | |
| 255 | public synchronized void writeTo(final OutputStream out) throws IOException { | |
| 256 | 7 | int remaining = count; |
| 257 | 7 | for (final byte[] buf : buffers) { |
| 258 | 9 | final int c = Math.min(buf.length, remaining); |
| 259 | 9 | out.write(buf, 0, c); |
| 260 | 9 | remaining -= c; |
| 261 | 9 | if (remaining == 0) { |
| 262 | 7 | break; |
| 263 | } | |
| 264 | 2 | } |
| 265 | 7 | } |
| 266 | ||
| 267 | /** | |
| 268 | * Fetches entire contents of an <code>InputStream</code> and represent | |
| 269 | * same data as result InputStream. | |
| 270 | * <p> | |
| 271 | * This method is useful where, | |
| 272 | * <ul> | |
| 273 | * <li>Source InputStream is slow.</li> | |
| 274 | * <li>It has network resources associated, so we cannot keep it open for | |
| 275 | * long time.</li> | |
| 276 | * <li>It has network timeout associated.</li> | |
| 277 | * </ul> | |
| 278 | * It can be used in favor of {@link #toByteArray()}, since it | |
| 279 | * avoids unnecessary allocation and copy of byte[].<br> | |
| 280 | * This method buffers the input internally, so there is no need to use a | |
| 281 | * <code>BufferedInputStream</code>. | |
| 282 | * | |
| 283 | * @param input Stream to be fully buffered. | |
| 284 | * @return A fully buffered stream. | |
| 285 | * @throws IOException if an I/O error occurs | |
| 286 | * @since 2.0 | |
| 287 | */ | |
| 288 | public static InputStream toBufferedInputStream(final InputStream input) | |
| 289 | throws IOException { | |
| 290 | // It does not matter if a ByteArrayOutputStream is not closed as close() is a no-op | |
| 291 | @SuppressWarnings("resource") | |
| 292 | 1 | final ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 293 | 1 | output.write(input); |
| 294 | 1 | return output.toInputStream(); |
| 295 | } | |
| 296 | ||
| 297 | /** | |
| 298 | * Gets the current contents of this byte stream as a Input Stream. The | |
| 299 | * returned stream is backed by buffers of <code>this</code> stream, | |
| 300 | * avoiding memory allocation and copy, thus saving space and time.<br> | |
| 301 | * | |
| 302 | * @return the current contents of this output stream. | |
| 303 | * @see java.io.ByteArrayOutputStream#toByteArray() | |
| 304 | * @see #reset() | |
| 305 | * @since 2.5 | |
| 306 | */ | |
| 307 | public synchronized InputStream toInputStream() { | |
| 308 | 5 | int remaining = count; |
| 309 | 5 | if (remaining == 0) { |
| 310 | 0 | return new ClosedInputStream(); |
| 311 | } | |
| 312 | 5 | final List<ByteArrayInputStream> list = new ArrayList<ByteArrayInputStream>(buffers.size()); |
| 313 | 5 | for (final byte[] buf : buffers) { |
| 314 | 16 | final int c = Math.min(buf.length, remaining); |
| 315 | 16 | list.add(new ByteArrayInputStream(buf, 0, c)); |
| 316 | 16 | remaining -= c; |
| 317 | 16 | if (remaining == 0) { |
| 318 | 5 | break; |
| 319 | } | |
| 320 | 11 | } |
| 321 | 5 | reuseBuffers = false; |
| 322 | 5 | return new SequenceInputStream(Collections.enumeration(list)); |
| 323 | } | |
| 324 | ||
| 325 | /** | |
| 326 | * Gets the curent contents of this byte stream as a byte array. | |
| 327 | * The result is independent of this stream. | |
| 328 | * | |
| 329 | * @return the current contents of this output stream, as a byte array | |
| 330 | * @see java.io.ByteArrayOutputStream#toByteArray() | |
| 331 | */ | |
| 332 | public synchronized byte[] toByteArray() { | |
| 333 | 191 | int remaining = count; |
| 334 | 191 | if (remaining == 0) { |
| 335 | 3 | return EMPTY_BYTE_ARRAY; |
| 336 | } | |
| 337 | 188 | final byte newbuf[] = new byte[remaining]; |
| 338 | 188 | int pos = 0; |
| 339 | 188 | for (final byte[] buf : buffers) { |
| 340 | 448 | final int c = Math.min(buf.length, remaining); |
| 341 | 448 | System.arraycopy(buf, 0, newbuf, pos, c); |
| 342 | 448 | pos += c; |
| 343 | 448 | remaining -= c; |
| 344 | 448 | if (remaining == 0) { |
| 345 | 188 | break; |
| 346 | } | |
| 347 | 260 | } |
| 348 | 188 | return newbuf; |
| 349 | } | |
| 350 | ||
| 351 | /** | |
| 352 | * Gets the curent contents of this byte stream as a string | |
| 353 | * using the platform default charset. | |
| 354 | * @return the contents of the byte array as a String | |
| 355 | * @see java.io.ByteArrayOutputStream#toString() | |
| 356 | * @deprecated 2.5 use {@link #toString(String)} instead | |
| 357 | */ | |
| 358 | @Override | |
| 359 | @Deprecated | |
| 360 | public String toString() { | |
| 361 | // make explicit the use of the default charset | |
| 362 | 6 | return new String(toByteArray(), Charset.defaultCharset()); |
| 363 | } | |
| 364 | ||
| 365 | /** | |
| 366 | * Gets the curent contents of this byte stream as a string | |
| 367 | * using the specified encoding. | |
| 368 | * | |
| 369 | * @param enc the name of the character encoding | |
| 370 | * @return the string converted from the byte array | |
| 371 | * @throws UnsupportedEncodingException if the encoding is not supported | |
| 372 | * @see java.io.ByteArrayOutputStream#toString(String) | |
| 373 | */ | |
| 374 | public String toString(final String enc) throws UnsupportedEncodingException { | |
| 375 | 2 | return new String(toByteArray(), enc); |
| 376 | } | |
| 377 | ||
| 378 | /** | |
| 379 | * Gets the curent contents of this byte stream as a string | |
| 380 | * using the specified encoding. | |
| 381 | * | |
| 382 | * @param charset the character encoding | |
| 383 | * @return the string converted from the byte array | |
| 384 | * @see java.io.ByteArrayOutputStream#toString(String) | |
| 385 | * @since 2.5 | |
| 386 | */ | |
| 387 | public String toString(final Charset charset) { | |
| 388 | 4 | return new String(toByteArray(), charset); |
| 389 | } | |
| 390 | ||
| 391 | } |