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    *      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  
19  import java.io.FilterWriter;
20  import java.io.IOException;
21  import java.io.Writer;
22  
23  import org.apache.commons.io.IOUtils;
24  
25  /**
26   * Writer which breaks larger output blocks into chunks.
27   * Native code may need to copy the input array; if the write buffer
28   * is very large this can cause OOME.
29   *
30   * @since 2.5
31   */
32  public class ChunkedWriter extends FilterWriter {
33  
34      /**
35       * The default chunk size to use, i.e. {@value} bytes.
36       */
37      private static final int DEFAULT_CHUNK_SIZE = IOUtils.DEFAULT_BUFFER_SIZE;
38  
39      /**
40       * The maximum chunk size to us when writing data arrays
41       */
42      private final int chunkSize;
43  
44      /**
45       * Constructs a new writer that uses a chunk size of {@link #DEFAULT_CHUNK_SIZE}
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       *
74       * @throws IOException upon error
75       */
76      @Override
77      public void write(final char[] data, final int srcOffset, final int length) throws IOException {
78          int bytes = length;
79          int dstOffset = srcOffset;
80          while (bytes > 0) {
81              final int chunk = Math.min(bytes, chunkSize);
82              out.write(data, dstOffset, chunk);
83              bytes -= chunk;
84              dstOffset += chunk;
85          }
86      }
87  
88  }