Coverage Report - eu.simuline.octave.util.IOUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
IOUtils
83%
10/12
100%
2/2
2.5
 
 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  0
     private IOUtils() {
 36  0
         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  2748
         final char[] buffer = new char[BUFFER_SIZE];
 48  2748
         long total = 0;
 49  
         while (true) {
 50  5496
             final int charsRead = reader.read(buffer, 0, BUFFER_SIZE);
 51  5486
             if (charsRead == -1) {
 52  2736
                 break;
 53  
             }
 54  2750
             total += charsRead;
 55  2750
             writer.write(buffer, 0, charsRead);
 56  2750
         }
 57  2736
         writer.flush();
 58  2736
         return total;
 59  
     }
 60  
 
 61  
 }