    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 */
    017
    018package org.apache.commons.io.output;
    019
    020import java.io.FilterWriter;
    021import java.io.IOException;
    022import java.io.Writer;
    023
    024import org.apache.commons.io.IOUtils;
    025
    026/**
    027 * Writer 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 OOME.
    028 *
    029 * @since 2.5
    030 */
    031public class ChunkedWriter extends FilterWriter {
    032
    033    /**
    034     * The default chunk size to use: {@value} bytes.
    035     */
    036    private static final int DEFAULT_CHUNK_SIZE = IOUtils.DEFAULT_BUFFER_SIZE;
    037
    038    /**
    039     * The maximum chunk size to us when writing data arrays.
    040     */
    041    private final int chunkSize;
    042
    043    /**
    044     * Constructs a new writer that uses a chunk size of {@link #DEFAULT_CHUNK_SIZE}
    045     *
    046     * @param writer the writer to wrap.
    047     */
    048    public ChunkedWriter(final Writer writer) {
    049        this(writer, DEFAULT_CHUNK_SIZE);
    050    }
    051
    052    /**
    053     * Constructs 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     *
    070     * @param data      The data.
    071     * @param srcOffset the offset.
    072     * @param length    the number of bytes to write.
    073     * @throws NullPointerException if the data is {@code null}.
    074     * @throws IndexOutOfBoundsException if {@code srcOffset} or {@code length} are negative,
    075     *                                   or if {@code srcOffset + length} is greater than {@code data.length}.
    076     * @throws IOException If an I/O error occurs.
    077     */
    078    @Override
    079    public void write(final char[] data, final int srcOffset, final int length) throws IOException {
    080        IOUtils.checkFromIndexSize(data, srcOffset, length);
    081        int bytes = length;
    082        int dstOffset = srcOffset;
    083        while (bytes > 0) {
    084            final int chunk = Math.min(bytes, chunkSize);
    085            out.write(data, dstOffset, chunk);
    086            bytes -= chunk;
    087            dstOffset += chunk;
    088        }
    089    }
    090}