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.csv;
18  
19  import java.io.IOException;
20  import java.io.Reader;
21  import java.io.Writer;
22  import java.nio.Buffer;
23  import java.nio.CharBuffer;
24  
25  /** Copied from Apache Commons IO. */
26  final class IOUtils {
27  
28      /**
29       * The default buffer size ({@value}).
30       */
31      static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
32  
33      /**
34       * Represents the end-of-file (or stream).
35       */
36      private static final int EOF = -1;
37  
38      /**
39       * Copies chars from a large (over 2GB) {@code Reader} to an {@code Appendable}.
40       * <p>
41       * This method buffers the input internally, so there is no need to use a
42       * {@code BufferedReader}.
43       * </p>
44       * The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}.
45       *
46       * @param input the {@code Reader} to read from
47       * @param output the {@code Appendable} to append to
48       * @return the number of characters copied
49       * @throws NullPointerException if the input or output is null
50       * @throws IOException          if an I/O error occurs
51       * @since 2.7
52       */
53      static long copy(final Reader input, final Appendable output) throws IOException {
54          return copy(input, output, CharBuffer.allocate(DEFAULT_BUFFER_SIZE));
55      }
56  
57      /**
58       * Copies chars from a large (over 2GB) {@code Reader} to an {@code Appendable}.
59       * <p>
60       * This method uses the provided buffer, so there is no need to use a
61       * {@code BufferedReader}.
62       * </p>
63       *
64       * @param input the {@code Reader} to read from
65       * @param output the {@code Appendable} to write to
66       * @param buffer the buffer to be used for the copy
67       * @return the number of characters copied
68       * @throws NullPointerException if the input or output is null
69       * @throws IOException          if an I/O error occurs
70       * @since 2.7
71       */
72      static long copy(final Reader input, final Appendable output, final CharBuffer buffer) throws IOException {
73          long count = 0;
74          int n;
75          while (EOF != (n = input.read(buffer))) {
76              ((Buffer) buffer).flip();
77              output.append(buffer, 0, n);
78              count += n;
79          }
80          return count;
81      }
82  
83      /**
84       * Copies chars from a large (over 2GB) {@code Reader} to a {@code Writer}.
85       * <p>
86       * This method buffers the input internally, so there is no need to use a
87       * {@code BufferedReader}.
88       * </p>
89       * <p>
90       * The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}.
91       * </p>
92       *
93       * @param input the {@code Reader} to read from
94       * @param output the {@code Writer} to write to
95       * @return the number of characters copied
96       * @throws NullPointerException if the input or output is null
97       * @throws IOException          if an I/O error occurs
98       * @since 1.3
99       */
100     static long copyLarge(final Reader input, final Writer output) throws IOException {
101         return copyLarge(input, output, new char[DEFAULT_BUFFER_SIZE]);
102     }
103 
104     /**
105      * Copies chars from a large (over 2GB) {@code Reader} to a {@code Writer}.
106      * <p>
107      * This method uses the provided buffer, so there is no need to use a
108      * {@code BufferedReader}.
109      * </p>
110      *
111      * @param input the {@code Reader} to read from
112      * @param output the {@code Writer} to write to
113      * @param buffer the buffer to be used for the copy
114      * @return the number of characters copied
115      * @throws NullPointerException if the input or output is null
116      * @throws IOException          if an I/O error occurs
117      * @since 2.2
118      */
119     static long copyLarge(final Reader input, final Writer output, final char[] buffer) throws IOException {
120         long count = 0;
121         int n;
122         while (EOF != (n = input.read(buffer))) {
123             output.write(buffer, 0, n);
124             count += n;
125         }
126         return count;
127     }
128 
129     /**
130      * Throws the given throwable.
131      *
132      * @param <T> The throwable cast type.
133      * @param throwable The throwable to rethrow.
134      * @return nothing because we throw.
135      * @throws T Always thrown.
136      */
137     @SuppressWarnings("unchecked")
138     static <T extends Throwable> RuntimeException rethrow(final Throwable throwable) throws T {
139         throw (T) throwable;
140     }
141 
142     /** No instances. */
143     private IOUtils() {
144         // Noop
145     }
146 
147 }