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 *      http://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.FilterOutputStream;
020import java.io.IOException;
021import java.io.OutputStream;
022
023import org.apache.commons.io.IOUtils;
024import org.apache.commons.io.build.AbstractStreamBuilder;
025
026/**
027 * OutputStream which breaks larger output blocks into chunks. Native code may need to copy the input array; if the write buffer is very large this can cause
028 * OOME.
029 * <p>
030 * To build an instance, see {@link Builder}
031 * </p>
032 *
033 * @see Builder
034 * @since 2.5
035 */
036public class ChunkedOutputStream extends FilterOutputStream {
037
038    // @formatter:off
039    /**
040     * Builds a new {@link UnsynchronizedByteArrayOutputStream}.
041     *
042     * <p>
043     * Using File IO:
044     * </p>
045     * <pre>{@code
046     * ChunkedOutputStream s = ChunkedOutputStream.builder()
047     *   .setPath("over/there.out")
048     *   .setBufferSize(8192)
049     *   .get();
050     * }
051     * </pre>
052     * <p>
053     * Using NIO Path:
054     * </p>
055     * <pre>{@code
056     * ChunkedOutputStream s = ChunkedOutputStream.builder()
057     *   .setPath("over/there.out")
058     *   .setBufferSize(8192)
059     *   .get();
060     * }
061     * </pre>
062     *
063     * @see #get()
064     * @since 2.13.0
065     */
066    // @formatter:on
067    public static class Builder extends AbstractStreamBuilder<ChunkedOutputStream, Builder> {
068
069        /**
070         * Constructs a new builder of {@link ChunkedOutputStream}.
071         */
072        public Builder() {
073            // empty
074        }
075
076        /**
077         * Builds a new {@link ChunkedOutputStream}.
078         * <p>
079         * This builder uses the following aspects:
080         * </p>
081         * <ul>
082         * <li>{@link #getOutputStream()} is the target aspect.</li>
083         * <li>{@link #getBufferSize()} is used for the chunk size.</li>
084         * </ul>
085         *
086         * @return a new instance.
087         * @throws IllegalStateException         if the {@code origin} is {@code null}.
088         * @throws UnsupportedOperationException if the origin cannot be converted to an {@link OutputStream}.
089         * @throws IOException                   if an I/O error occurs converting to an {@link OutputStream} using {@link #getOutputStream()}.
090         * @see #getOutputStream()
091         * @see #getBufferSize()
092         * @see #getUnchecked()
093         */
094        @Override
095        public ChunkedOutputStream get() throws IOException {
096            return new ChunkedOutputStream(getOutputStream(), getBufferSize());
097        }
098
099    }
100
101    /**
102     * Constructs a new {@link Builder}.
103     *
104     * @return a new {@link Builder}.
105     * @since 2.13.0
106     */
107    public static Builder builder() {
108        return new Builder();
109    }
110
111    /**
112     * The maximum chunk size to us when writing data arrays
113     */
114    private final int chunkSize;
115
116    /**
117     * Constructs a new stream that uses a chunk size of {@link IOUtils#DEFAULT_BUFFER_SIZE}.
118     *
119     * @param stream the stream to wrap
120     * @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}
121     */
122    @Deprecated
123    public ChunkedOutputStream(final OutputStream stream) {
124        this(stream, IOUtils.DEFAULT_BUFFER_SIZE);
125    }
126
127    /**
128     * Constructs a new stream that uses the specified chunk size.
129     *
130     * @param stream    the stream to wrap
131     * @param chunkSize the chunk size to use; must be a positive number.
132     * @throws IllegalArgumentException if the chunk size is &lt;= 0
133     * @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}
134     */
135    @Deprecated
136    public ChunkedOutputStream(final OutputStream stream, final int chunkSize) {
137        super(stream);
138        if (chunkSize <= 0) {
139            throw new IllegalArgumentException("chunkSize <= 0");
140        }
141        this.chunkSize = chunkSize;
142    }
143
144    int getChunkSize() {
145        return chunkSize;
146    }
147
148    /**
149     * Writes the data buffer in chunks to the underlying stream
150     *
151     * @param data      the data to write
152     * @param srcOffset the offset
153     * @param length    the length of data to write
154     * @throws IOException if an I/O error occurs.
155     */
156    @Override
157    public void write(final byte[] data, final int srcOffset, final int length) throws IOException {
158        int bytes = length;
159        int dstOffset = srcOffset;
160        while (bytes > 0) {
161            final int chunk = Math.min(bytes, chunkSize);
162            out.write(data, dstOffset, chunk);
163            bytes -= chunk;
164            dstOffset += chunk;
165        }
166    }
167
168}