001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      https://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.io.output;
018
019import java.io.BufferedInputStream;
020import java.io.IOException;
021import java.io.InputStream;
022import java.io.OutputStream;
023
024import org.apache.commons.io.build.AbstractOrigin;
025import org.apache.commons.io.build.AbstractStreamBuilder;
026import org.apache.commons.io.function.Uncheck;
027import org.apache.commons.io.input.UnsynchronizedByteArrayInputStream;
028
029/**
030 * Implements a version of {@link AbstractByteArrayOutputStream} <strong>without</strong> any concurrent thread safety.
031 * <p>
032 * To build an instance, use {@link Builder}.
033 * </p>
034 *
035 * @see Builder
036 * @since 2.7
037 */
038//@NotThreadSafe
039public final class UnsynchronizedByteArrayOutputStream extends AbstractByteArrayOutputStream<UnsynchronizedByteArrayOutputStream> {
040
041    // @formatter:off
042    /**
043     * Builds a new {@link UnsynchronizedByteArrayOutputStream}.
044     *
045     * <p>
046     * Using File IO:
047     * </p>
048     * <pre>{@code
049     * UnsynchronizedByteArrayOutputStream s = UnsynchronizedByteArrayOutputStream.builder()
050     *   .setBufferSize(8192)
051     *   .get();}
052     * </pre>
053     * <p>
054     * Using NIO Path:
055     * </p>
056     * <pre>{@code
057     * UnsynchronizedByteArrayOutputStream s = UnsynchronizedByteArrayOutputStream.builder()
058     *   .setBufferSize(8192)
059     *   .get();}
060     * </pre>
061     *
062     * @see #get()
063     */
064    // @formatter:on
065    public static class Builder extends AbstractStreamBuilder<UnsynchronizedByteArrayOutputStream, Builder> {
066
067        /**
068         * Constructs a new builder of {@link UnsynchronizedByteArrayOutputStream}.
069         */
070        public Builder() {
071            // empty
072        }
073
074        /**
075         * Builds a new {@link UnsynchronizedByteArrayOutputStream}.
076         *
077         * <p>
078         * This builder uses the following aspects:
079         * </p>
080         * <ul>
081         * <li>{@link #getBufferSize()}</li>
082         * </ul>
083         *
084         * @return a new instance.
085         * @see AbstractOrigin#getByteArray()
086         * @see #getUnchecked()
087         */
088        @Override
089        public UnsynchronizedByteArrayOutputStream get() {
090            return new UnsynchronizedByteArrayOutputStream(this);
091        }
092
093    }
094
095    /**
096     * Constructs a new {@link Builder}.
097     *
098     * @return a new {@link Builder}.
099     */
100    public static Builder builder() {
101        return new Builder();
102    }
103
104    /**
105     * Fetches entire contents of an {@link InputStream} and represent same data as result InputStream.
106     * <p>
107     * This method is useful where,
108     * </p>
109     * <ul>
110     * <li>Source InputStream is slow.</li>
111     * <li>It has network resources associated, so we cannot keep it open for long time.</li>
112     * <li>It has network timeout associated.</li>
113     * </ul>
114     * It can be used in favor of {@link #toByteArray()}, since it avoids unnecessary allocation and copy of byte[].<br>
115     * This method buffers the input internally, so there is no need to use a {@link BufferedInputStream}.
116     *
117     * @param input Stream to be fully buffered.
118     * @return A fully buffered stream.
119     * @throws IOException if an I/O error occurs.
120     */
121    public static InputStream toBufferedInputStream(final InputStream input) throws IOException {
122        return toBufferedInputStream(input, DEFAULT_SIZE);
123    }
124
125    /**
126     * Fetches entire contents of an {@link InputStream} and represent same data as result InputStream.
127     * <p>
128     * This method is useful where,
129     * </p>
130     * <ul>
131     * <li>Source InputStream is slow.</li>
132     * <li>It has network resources associated, so we cannot keep it open for long time.</li>
133     * <li>It has network timeout associated.</li>
134     * </ul>
135     * It can be used in favor of {@link #toByteArray()}, since it avoids unnecessary allocation and copy of byte[].<br>
136     * This method buffers the input internally, so there is no need to use a {@link BufferedInputStream}.
137     *
138     * @param input Stream to be fully buffered.
139     * @param size the initial buffer size
140     * @return A fully buffered stream.
141     * @throws IOException if an I/O error occurs.
142     */
143    public static InputStream toBufferedInputStream(final InputStream input, final int size) throws IOException {
144        // It does not matter if a ByteArrayOutputStream is not closed as close() is a no-op
145        try (UnsynchronizedByteArrayOutputStream output = builder().setBufferSize(size).get()) {
146            output.write(input);
147            return output.toInputStream();
148        }
149    }
150
151    /**
152     * Constructs a new byte array output stream. The buffer capacity is initially
153     *
154     * {@value AbstractByteArrayOutputStream#DEFAULT_SIZE} bytes, though its size increases if necessary.
155     * @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}.
156     */
157    @Deprecated
158    public UnsynchronizedByteArrayOutputStream() {
159        this(DEFAULT_SIZE);
160    }
161
162    private UnsynchronizedByteArrayOutputStream(final Builder builder) {
163        this(builder.getBufferSize());
164    }
165
166    /**
167     * Constructs a new byte array output stream, with a buffer capacity of the specified size, in bytes.
168     *
169     * @param size the initial size
170     * @throws IllegalArgumentException if size is negative
171     * @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}. Will be private in 3.0.0.
172     */
173    @Deprecated
174    public UnsynchronizedByteArrayOutputStream(final int size) {
175        if (size < 0) {
176            throw new IllegalArgumentException("Negative initial size: " + size);
177        }
178        needNewBuffer(size);
179    }
180
181    /**
182     * @see java.io.ByteArrayOutputStream#reset()
183     */
184    @Override
185    public void reset() {
186        resetImpl();
187    }
188
189    @Override
190    public int size() {
191        return count;
192    }
193
194    @Override
195    public byte[] toByteArray() {
196        return toByteArrayImpl();
197    }
198
199    @Override
200    public InputStream toInputStream() {
201        // @formatter:off
202        return toInputStream((buffer, offset, length) -> Uncheck
203                .get(() -> UnsynchronizedByteArrayInputStream.builder()
204                        .setByteArray(buffer)
205                        .setOffset(offset)
206                        .setLength(length)
207                        .get()));
208        // @formatter:on
209    }
210
211    @Override
212    public void write(final byte[] b, final int off, final int len) {
213        if (off < 0 || off > b.length || len < 0 || off + len > b.length || off + len < 0) {
214            throw new IndexOutOfBoundsException(String.format("offset=%,d, length=%,d", off, len));
215        }
216        if (len == 0) {
217            return;
218        }
219        writeImpl(b, off, len);
220    }
221
222    @Override
223    public int write(final InputStream in) throws IOException {
224        return writeImpl(in);
225    }
226
227    @Override
228    public void write(final int b) {
229        writeImpl(b);
230    }
231
232    @Override
233    public void writeTo(final OutputStream out) throws IOException {
234        writeToImpl(out);
235    }
236}