    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;
    024
    025/**
    026 * OutputStream which breaks larger output blocks into chunks.
    027 * Native code may need to copy the input array; if the write buffer
    028 * is very large this can cause OOME.
    029 *
    030 * @since 2.5
    031 */
    032public class ChunkedOutputStream extends FilterOutputStream {
    033
    034    /**
    035     * The maximum chunk size to us when writing data arrays
    036     */
    037    private final int chunkSize;
    038
    039    /**
    040     * Creates a new stream that uses a chunk size of {@link IOUtils#DEFAULT_BUFFER_SIZE}.
    041     *
    042     * @param stream the stream to wrap
    043     */
    044    public ChunkedOutputStream(final OutputStream stream) {
    045        this(stream, IOUtils.DEFAULT_BUFFER_SIZE);
    046    }
    047
    048    /**
    049     * Creates a new stream that uses the specified chunk size.
    050     *
    051     * @param stream the stream to wrap
    052     * @param chunkSize the chunk size to use; must be a positive number.
    053     * @throws IllegalArgumentException if the chunk size is &lt;= 0
    054     */
    055    public ChunkedOutputStream(final OutputStream stream, final int chunkSize) {
    056       super(stream);
    057       if (chunkSize <= 0) {
    058           throw new IllegalArgumentException();
    059       }
    060       this.chunkSize = chunkSize;
    061    }
    062
    063    /**
    064     * Writes the data buffer in chunks to the underlying stream
    065     *
    066     * @param data the data to write
    067     * @param srcOffset the offset
    068     * @param length the length of data to write
    069     *
    070     * @throws IOException if an I/O error occurs.
    071     */
    072    @Override
    073    public void write(final byte[] data, final int srcOffset, final int length) throws IOException {
    074        int bytes = length;
    075        int dstOffset = srcOffset;
    076        while(bytes > 0) {
    077            final int chunk = Math.min(bytes, chunkSize);
    078            out.write(data, dstOffset, chunk);
    079            bytes -= chunk;
    080            dstOffset += chunk;
    081        }
    082    }
    083
    084}