View Javadoc
1   /*
2    * Copyright 2007, 2008 Ange Optimization ApS
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  /**
17   * @author Kim Hansen
18   */
19  package eu.simuline.octave.util;
20  
21  import java.io.IOException;
22  import java.io.Reader;
23  import java.io.Writer;
24  
25  /**
26   * Class for holding static utility functions for readers and writers: 
27   * copy from reader to writer. 
28   * 
29   * @author Kim Hansen
30   */
31  public final class IOUtils {
32  
33      private static final int BUFFER_SIZE = 8 * 1024;
34  
35      private IOUtils() {
36          throw new UnsupportedOperationException("Do not instantiate");
37      }
38  
39      /**
40       * @param reader
41       * @param writer
42       * @return The total chars copied
43       * @throws IOException
44       */
45      public static long copy(final Reader reader, 
46  			    final Writer writer) throws IOException {
47          final char[] buffer = new char[BUFFER_SIZE];
48          long total = 0;
49          while (true) {
50              final int charsRead = reader.read(buffer, 0, BUFFER_SIZE);
51              if (charsRead == -1) {
52                  break;
53              }
54              total += charsRead;
55              writer.write(buffer, 0, charsRead);
56          }
57          writer.flush();
58          return total;
59      }
60  
61  }