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