View Javadoc
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    *      https://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  
18  package org.apache.commons.io.output;
19  
20  import java.io.FilterWriter;
21  import java.io.IOException;
22  import java.io.Writer;
23  
24  import org.apache.commons.io.IOUtils;
25  
26  /**
27   * 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.
28   *
29   * @since 2.5
30   */
31  public class ChunkedWriter extends FilterWriter {
32  
33      /**
34       * The default chunk size to use: {@value} bytes.
35       */
36      private static final int DEFAULT_CHUNK_SIZE = IOUtils.DEFAULT_BUFFER_SIZE;
37  
38      /**
39       * The maximum chunk size to us when writing data arrays.
40       */
41      private final int chunkSize;
42  
43      /**
44       * Constructs a new writer that uses a chunk size of {@link #DEFAULT_CHUNK_SIZE}
45       *
46       * @param writer the writer to wrap.
47       */
48      public ChunkedWriter(final Writer writer) {
49          this(writer, DEFAULT_CHUNK_SIZE);
50      }
51  
52      /**
53       * Constructs a new writer that uses the specified chunk size.
54       *
55       * @param writer    the writer to wrap.
56       * @param chunkSize the chunk size to use; must be a positive number.
57       * @throws IllegalArgumentException if the chunk size is <= 0.
58       */
59      public ChunkedWriter(final Writer writer, final int chunkSize) {
60          super(writer);
61          if (chunkSize <= 0) {
62              throw new IllegalArgumentException();
63          }
64          this.chunkSize = chunkSize;
65      }
66  
67      /**
68       * Writes the data buffer in chunks to the underlying writer.
69       *
70       * @param data      The data.
71       * @param srcOffset the offset.
72       * @param length    the number of bytes to write.
73       * @throws NullPointerException if the data is {@code null}.
74       * @throws IndexOutOfBoundsException if {@code srcOffset} or {@code length} are negative,
75       *                                   or if {@code srcOffset + length} is greater than {@code data.length}.
76       * @throws IOException If an I/O error occurs.
77       */
78      @Override
79      public void write(final char[] data, final int srcOffset, final int length) throws IOException {
80          IOUtils.checkFromIndexSize(data, srcOffset, length);
81          int bytes = length;
82          int dstOffset = srcOffset;
83          while (bytes > 0) {
84              final int chunk = Math.min(bytes, chunkSize);
85              out.write(data, dstOffset, chunk);
86              bytes -= chunk;
87              dstOffset += chunk;
88          }
89      }
90  }