Coverage Report - net.sf.jmatchparser.util.StreamForwarder
 
Classes in this File Line Coverage Branch Coverage Complexity
StreamForwarder
47%
10/21
41%
5/12
4
 
 1  
 /*
 2  
  * Copyright (c) 2006 - 2011 Michael Schierl
 3  
  * 
 4  
  * All rights reserved.
 5  
  * 
 6  
  * Redistribution and use in source and binary forms, with or without
 7  
  * modification, are permitted provided that the following conditions
 8  
  * are met:
 9  
  * 
 10  
  * - Redistributions of source code must retain the above copyright notice,
 11  
  *   this list of conditions and the following disclaimer.
 12  
  *   
 13  
  * - Redistributions in binary form must reproduce the above copyright
 14  
  *   notice, this list of conditions and the following disclaimer in the
 15  
  *   documentation and/or other materials provided with the distribution.
 16  
  *   
 17  
  * - Neither name of the copyright holders nor the names of its
 18  
  *   contributors may be used to endorse or promote products derived from
 19  
  *   this software without specific prior written permission.
 20  
  *   
 21  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND THE CONTRIBUTORS
 22  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 23  
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 24  
  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 25  
  * HOLDERS OR THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 26  
  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 27  
  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 28  
  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 29  
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 30  
  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 31  
  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 32  
  */
 33  
 package net.sf.jmatchparser.util;
 34  
 
 35  
 import java.io.IOException;
 36  
 import java.io.InputStream;
 37  
 import java.io.OutputStream;
 38  
 import java.io.Reader;
 39  
 import java.io.Writer;
 40  
 
 41  
 /**
 42  
  * Utility class for forwarding data from an {@link InputStream} to an
 43  
  * {@link OutputStream} or from a {@link Reader} to a {@link Writer}.
 44  
  */
 45  0
 public class StreamForwarder extends Thread {
 46  
 
 47  
         /**
 48  
          * Forward all content of an {@link InputStream} to an {@link OutputStream}.
 49  
          * Both streams are closed afterwards.
 50  
          * 
 51  
          * @param in
 52  
          *            the input stream
 53  
          * @param out
 54  
          *            the output stream
 55  
          * @throws IOException
 56  
          *             if one of the streams throws an {@link IOException}
 57  
          */
 58  
         public static void forward(InputStream in, OutputStream out) throws IOException {
 59  
                 try {
 60  0
                         final byte[] buf = new byte[4096];
 61  
                         int length;
 62  0
                         while ((length = in.read(buf)) != -1) {
 63  0
                                 if (out != null) {
 64  0
                                         out.write(buf, 0, length);
 65  0
                                         if (in.available() == 0) {
 66  0
                                                 out.flush();
 67  
                                         }
 68  
                                 }
 69  
                         }
 70  
                 } finally {
 71  0
                         in.close();
 72  0
                         out.close();
 73  0
                 }
 74  0
         }
 75  
 
 76  
         /**
 77  
          * Forward all content of a {@link Reader} to a {@link Writer}. Both are
 78  
          * closed afterwards.
 79  
          * 
 80  
          * @param in
 81  
          *            the reader
 82  
          * @param out
 83  
          *            the writer
 84  
          * @throws IOException
 85  
          *             if either the reader or the writer throws an
 86  
          *             {@link IOException}
 87  
          */
 88  
         public static void forward(Reader in, Writer out) throws IOException {
 89  
                 try {
 90  3
                         final char[] buf = new char[4096];
 91  
                         int length;
 92  6
                         while ((length = in.read(buf)) != -1) {
 93  3
                                 if (out != null) {
 94  3
                                         out.write(buf, 0, length);
 95  3
                                         if (!in.ready()) {
 96  1
                                                 out.flush();
 97  
                                         }
 98  
                                 }
 99  
                         }
 100  
                 } finally {
 101  3
                         in.close();
 102  3
                         out.close();
 103  3
                 }
 104  3
         }
 105  
 }