Coverage Report - eu.simuline.octave.util.ReaderWriterPipeThread
 
Classes in this File Line Coverage Branch Coverage Complexity
ReaderWriterPipeThread
88%
39/44
66%
4/6
2.5
ReaderWriterPipeThread$1
100%
4/4
N/A
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  
 package eu.simuline.octave.util;
 17  
 
 18  
 import java.io.IOException;
 19  
 import java.io.Reader;
 20  
 import java.io.Writer;
 21  
 
 22  
 import eu.simuline.octave.exception.OctaveIOException;
 23  
 import eu.simuline.octave.exception.OctaveInterruptedException;
 24  
 
 25  
 import org.apache.commons.logging.LogFactory;
 26  
 import org.apache.commons.logging.Log;
 27  
 
 28  
 /**
 29  
  * A Thread that moves data from a Reader to a Writer. 
 30  
  * 
 31  
  * @author Kim Hansen
 32  
  */
 33  
 public final class ReaderWriterPipeThread extends Thread {
 34  
 
 35  2
     private static final Log LOG = LogFactory
 36  2
         .getLog(ReaderWriterPipeThread.class);
 37  
 
 38  
     @SuppressWarnings("checkstyle:magicnumber")
 39  2
     private static final char[] BUF = new char[4 * 1024];
 40  
  
 41  
     private final Reader reader;
 42  
 
 43  
     private Writer writer;
 44  
 
 45  
     /**
 46  
      * Will create a thread that reads from reader and writes to write 
 47  
      * until reader reaches EOF. 
 48  
      * Then the thread will close. 
 49  
      * Remember to join() this thread before closeing reader or writer.
 50  
      * 
 51  
      * @param reader
 52  
      * @param writer
 53  
      * @return Returns the new thread
 54  
      */
 55  
     public static ReaderWriterPipeThread instantiate(final Reader reader, 
 56  
                                                      final Writer writer) {
 57  106
         final ReaderWriterPipeThread readerWriterPipeThread = 
 58  
             new ReaderWriterPipeThread(reader, writer);
 59  212
         readerWriterPipeThread.setName(Thread.currentThread().getName() 
 60  
                                        + "-javaoctave-"
 61  
                                        + ReaderWriterPipeThread.class
 62  106
                                        .getSimpleName());
 63  106
         readerWriterPipeThread
 64  106
             .setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
 65  
                     public void uncaughtException(Thread th, Throwable ex) {
 66  4
                         System.out.println("Uncaught : " + ex + 
 67  2
                                            " of thread " + th.getName());
 68  2
                     }
 69  
                 });
 70  106
         readerWriterPipeThread.start();
 71  106
         return readerWriterPipeThread;
 72  
     }
 73  
 
 74  106
     private ReaderWriterPipeThread(final Reader reader, final Writer writer) {
 75  106
         this.reader = reader;
 76  106
         this.writer = writer;
 77  106
     }
 78  
 
 79  
     @Override
 80  
     public void run() {
 81  112
         while (!interrupted()) {
 82  
             int len;
 83  
             try {
 84  112
                 len = reader.read(BUF);
 85  2
             } catch (final IOException e) {
 86  2
                 LOG.error("Error when reading from reader", e);
 87  2
                 throw new OctaveIOException(e);
 88  106
             }
 89  106
             if (len == -1) {
 90  100
                 break;
 91  
             }
 92  
             try {
 93  6
                 synchronized (this) {
 94  6
                     if (writer != null) {
 95  6
                         writer.write(BUF, 0, len);
 96  6
                         writer.flush();
 97  
                     }
 98  6
                 }
 99  0
             } catch (final IOException e) {
 100  0
                 LOG.error("Error when writing to writer", e);
 101  0
                 throw new OctaveIOException(e);
 102  6
             }
 103  6
         }
 104  100
         LOG.debug("ReaderWriterPipeThread finished without error");
 105  100
     }
 106  
 
 107  
     /**
 108  
      * @param writer
 109  
      *    the writer to set
 110  
      */
 111  
     public void setWriter(final Writer writer) {
 112  2
         synchronized (this) {
 113  2
             this.writer = writer;
 114  2
         }
 115  2
     }
 116  
 
 117  
     /**
 118  
      * Close the thread.
 119  
      */
 120  
     public void close() {
 121  106
         interrupt();
 122  
         try {
 123  106
             join();
 124  0
         } catch (final InterruptedException e) {
 125  0
             throw new OctaveInterruptedException(e);
 126  106
         }
 127  106
     }
 128  
 
 129  
 }