001/*
002 * Copyright 2002-2015 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.util;
018
019import java.io.ByteArrayOutputStream;
020
021/**
022 * An extension of {@link java.io.ByteArrayOutputStream} that:
023 * <ul>
024 * <li>has public {@link org.springframework.util.ResizableByteArrayOutputStream#grow(int)}
025 * and {@link org.springframework.util.ResizableByteArrayOutputStream#resize(int)} methods
026 * to get more control over the size of the internal buffer</li>
027 * <li>has a higher initial capacity (256) by default</li>
028 * </ul>
029 *
030 * <p>As of 4.2, this class has been superseded by {@link FastByteArrayOutputStream}
031 * for Spring's internal use where no assignability to {@link ByteArrayOutputStream}
032 * is needed (since {@link FastByteArrayOutputStream} is more efficient with buffer
033 * resize management but doesn't extend the standard {@link ByteArrayOutputStream}).
034 *
035 * @author Brian Clozel
036 * @author Juergen Hoeller
037 * @since 4.0.3
038 * @see #resize
039 * @see FastByteArrayOutputStream
040 */
041public class ResizableByteArrayOutputStream extends ByteArrayOutputStream {
042
043        private static final int DEFAULT_INITIAL_CAPACITY = 256;
044
045
046        /**
047         * Create a new <code>ResizableByteArrayOutputStream</code>
048         * with the default initial capacity of 256 bytes.
049         */
050        public ResizableByteArrayOutputStream() {
051                super(DEFAULT_INITIAL_CAPACITY);
052        }
053
054        /**
055         * Create a new <code>ResizableByteArrayOutputStream</code>
056         * with the specified initial capacity.
057         * @param initialCapacity the initial buffer size in bytes
058         */
059        public ResizableByteArrayOutputStream(int initialCapacity) {
060                super(initialCapacity);
061        }
062
063
064        /**
065         * Resize the internal buffer size to a specified capacity.
066         * @param targetCapacity the desired size of the buffer
067         * @throws IllegalArgumentException if the given capacity is smaller than
068         * the actual size of the content stored in the buffer already
069         * @see ResizableByteArrayOutputStream#size()
070         */
071        public synchronized void resize(int targetCapacity) {
072                Assert.isTrue(targetCapacity >= this.count, "New capacity must not be smaller than current size");
073                byte[] resizedBuffer = new byte[targetCapacity];
074                System.arraycopy(this.buf, 0, resizedBuffer, 0, this.count);
075                this.buf = resizedBuffer;
076        }
077
078        /**
079         * Grow the internal buffer size.
080         * @param additionalCapacity the number of bytes to add to the current buffer size
081         * @see ResizableByteArrayOutputStream#size()
082         */
083        public synchronized void grow(int additionalCapacity) {
084                Assert.isTrue(additionalCapacity >= 0, "Additional capacity must be 0 or higher");
085                if (this.count + additionalCapacity > this.buf.length) {
086                        int newCapacity = Math.max(this.buf.length * 2, this.count + additionalCapacity);
087                        resize(newCapacity);
088                }
089        }
090
091        /**
092         * Return the current size of this stream's internal buffer.
093         */
094        public synchronized int capacity() {
095                return this.buf.length;
096        }
097
098}