ChunkedWriter.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.io.output;

  18. import java.io.FilterWriter;
  19. import java.io.IOException;
  20. import java.io.Writer;

  21. import org.apache.commons.io.IOUtils;

  22. /**
  23.  * Writer which breaks larger output blocks into chunks.
  24.  * Native code may need to copy the input array; if the write buffer
  25.  * is very large this can cause OOME.
  26.  *
  27.  * @since 2.5
  28.  */
  29. public class ChunkedWriter extends FilterWriter {

  30.     /**
  31.      * The default chunk size to use: {@value} bytes.
  32.      */
  33.     private static final int DEFAULT_CHUNK_SIZE = IOUtils.DEFAULT_BUFFER_SIZE;

  34.     /**
  35.      * The maximum chunk size to us when writing data arrays
  36.      */
  37.     private final int chunkSize;

  38.     /**
  39.      * Constructs a new writer that uses a chunk size of {@link #DEFAULT_CHUNK_SIZE}
  40.      * @param writer the writer to wrap
  41.      */
  42.     public ChunkedWriter(final Writer writer) {
  43.         this(writer, DEFAULT_CHUNK_SIZE);
  44.     }

  45.     /**
  46.      * Constructs a new writer that uses the specified chunk size.
  47.      *
  48.      * @param writer the writer to wrap
  49.      * @param chunkSize the chunk size to use; must be a positive number.
  50.      * @throws IllegalArgumentException if the chunk size is <= 0
  51.      */
  52.     public ChunkedWriter(final Writer writer, final int chunkSize) {
  53.        super(writer);
  54.        if (chunkSize <= 0) {
  55.            throw new IllegalArgumentException();
  56.        }
  57.        this.chunkSize = chunkSize;
  58.     }

  59.     /**
  60.      * Writes the data buffer in chunks to the underlying writer.
  61.      *
  62.      * @param data The data
  63.      * @param srcOffset the offset
  64.      * @param length the number of bytes to write
  65.      * @throws IOException upon error
  66.      */
  67.     @Override
  68.     public void write(final char[] data, final int srcOffset, final int length) throws IOException {
  69.         int bytes = length;
  70.         int dstOffset = srcOffset;
  71.         while (bytes > 0) {
  72.             final int chunk = Math.min(bytes, chunkSize);
  73.             out.write(data, dstOffset, chunk);
  74.             bytes -= chunk;
  75.             dstOffset += chunk;
  76.         }
  77.     }

  78. }