    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.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 ChunkedOutputStream}.
    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(this);
    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 the specified chunk size.
    118     *
    119     * @param builder holds contruction data.
    120     * @throws IOException if an I/O error occurs.
    121     */
    122    @SuppressWarnings("resource") // caller closes.
    123    private ChunkedOutputStream(final Builder builder) throws IOException {
    124        super(builder.getOutputStream());
    125        final int bufferSize = builder.getBufferSize();
    126        if (bufferSize <= 0) {
    127            throw new IllegalArgumentException("chunkSize <= 0");
    128        }
    129        this.chunkSize = bufferSize;
    130    }
    131
    132    /**
    133     * Constructs a new stream that uses a chunk size of {@link IOUtils#DEFAULT_BUFFER_SIZE}.
    134     *
    135     * @param stream the stream to wrap.
    136     * @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}.
    137     */
    138    @Deprecated
    139    public ChunkedOutputStream(final OutputStream stream) {
    140        this(stream, IOUtils.DEFAULT_BUFFER_SIZE);
    141    }
    142
    143    /**
    144     * Constructs a new stream that uses the specified chunk size.
    145     *
    146     * @param stream    the stream to wrap.
    147     * @param chunkSize the chunk size to use; must be a positive number.
    148     * @throws IllegalArgumentException if the chunk size is &lt;= 0.
    149     * @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}.
    150     */
    151    @Deprecated
    152    public ChunkedOutputStream(final OutputStream stream, final int chunkSize) {
    153        super(stream);
    154        if (chunkSize <= 0) {
    155            throw new IllegalArgumentException("chunkSize <= 0");
    156        }
    157        this.chunkSize = chunkSize;
    158    }
    159
    160    /* Package-private for testing. */
    161    int getChunkSize() {
    162        return chunkSize;
    163    }
    164
    165    /**
    166     * Writes the data buffer in chunks to the underlying stream
    167     *
    168     * @param data      the data to write.
    169     * @param srcOffset the offset.
    170     * @param length    the length of data to write.
    171     * @throws NullPointerException if the data is {@code null}.
    172     * @throws IndexOutOfBoundsException if {@code srcOffset} or {@code length} are negative,
    173     *                                   or if {@code srcOffset + length} is greater than {@code data.length}.
    174     * @throws IOException if an I/O error occurs.
    175     */
    176    @Override
    177    public void write(final byte[] data, final int srcOffset, final int length) throws IOException {
    178        IOUtils.checkFromIndexSize(data, srcOffset, length);
    179        int bytes = length;
    180        int dstOffset = srcOffset;
    181        while (bytes > 0) {
    182            final int chunk = Math.min(bytes, chunkSize);
    183            out.write(data, dstOffset, chunk);
    184            bytes -= chunk;
    185            dstOffset += chunk;
    186        }
    187    }
    188
    189}