    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.FilterWriter;
    020import java.io.IOException;
    021import java.io.Writer;
    022
    023/**
    024 * Writer 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 ChunkedWriter extends FilterWriter {
    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 writer that uses the specified chunk size.
    044     *
    045     * @param writer the writer 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 ChunkedWriter(final Writer writer, final int chunkSize) {
    050       super(writer);
    051       if (chunkSize <= 0) {
    052           throw new IllegalArgumentException();
    053       }
    054       this.chunkSize = chunkSize;
    055    }
    056
    057    /**
    058     * Creates a new writer that uses a chunk size of {@link #DEFAULT_CHUNK_SIZE}
    059     * @param writer the writer to wrap
    060     */
    061    public ChunkedWriter(final Writer writer) {
    062        this(writer, DEFAULT_CHUNK_SIZE);
    063    }
    064
    065    /**
    066     * writes the data buffer in chunks to the underlying writer
    067     * @param data The data
    068     * @param srcOffset the offset
    069     * @param length the number of bytes to write
    070     *
    071     * @throws IOException upon error
    072     */
    073    @Override
    074    public void write(final char[] data, final int srcOffset, final int length) throws IOException {
    075        int bytes = length;
    076        int dstOffset = srcOffset;
    077        while(bytes > 0) {
    078            final int chunk = Math.min(bytes, chunkSize);
    079            out.write(data, dstOffset, chunk);
    080            bytes -= chunk;
    081            dstOffset += chunk;
    082        }
    083    }
    084
    085}