    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
    023import org.apache.commons.io.IOUtils;
    024
    025/**
    026 * Writer 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 ChunkedWriter extends FilterWriter {
    033
    034    /**
    035     * The default chunk size to use, i.e. {@value} bytes.
    036     */
    037    private static final int DEFAULT_CHUNK_SIZE = IOUtils.DEFAULT_BUFFER_SIZE;
    038
    039    /**
    040     * The maximum chunk size to us when writing data arrays
    041     */
    042    private final int chunkSize;
    043
    044    /**
    045     * Creates a new writer that uses a chunk size of {@link #DEFAULT_CHUNK_SIZE}
    046     * @param writer the writer to wrap
    047     */
    048    public ChunkedWriter(final Writer writer) {
    049        this(writer, DEFAULT_CHUNK_SIZE);
    050    }
    051
    052    /**
    053     * Creates a new writer that uses the specified chunk size.
    054     *
    055     * @param writer the writer to wrap
    056     * @param chunkSize the chunk size to use; must be a positive number.
    057     * @throws IllegalArgumentException if the chunk size is &lt;= 0
    058     */
    059    public ChunkedWriter(final Writer writer, final int chunkSize) {
    060       super(writer);
    061       if (chunkSize <= 0) {
    062           throw new IllegalArgumentException();
    063       }
    064       this.chunkSize = chunkSize;
    065    }
    066
    067    /**
    068     * writes the data buffer in chunks to the underlying writer
    069     * @param data The data
    070     * @param srcOffset the offset
    071     * @param length the number of bytes to write
    072     *
    073     * @throws IOException upon error
    074     */
    075    @Override
    076    public void write(final char[] 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}