Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.pyx4j.utils.service;
  
  import java.util.List;
  import java.util.Vector;
  
 
 public class StreamRelayer extends StopableRunnable implements RunnableThreadEndListener {
 
     private static final Logger log = Logger.getLogger(StreamRelayer.class);
     
     private InputStream in;
 
     private OutputStream out;
     
     private final List<ThreadEndListenerlisteners = new Vector<ThreadEndListener>();
     
     public boolean dataDebug = false;
     
     private boolean dumpInput = false;
     
     private StringBuffer dump;
     
     public StreamRelayer(String nameInputStream inOutputStream outThreadEndListener listener) {
         super. = name;
         this. = in;
         this. = out;
         addListener(listener);
          = false;
     }
     
     public void setDumpInput() {
          = true;
          = new StringBuffer();
     }
     
     public void addListener(ThreadEndListener listener) {
         if (listener != null) {
             this..add(listener);
         }
     }
     
     /* (non-Javadoc)
      * @see java.lang.Runnable#run()
      */
     public void execute() {
         byte onebyte[] = new byte[1];
         long transmited = 0;
         try {
             while (!this.) {
                 try {
                     if (.read(onebyte) < 0) {
                         break;
                     } else {
                         if () {
                             .debug( + " pass " + onebyte + " [" + new String(onebyte, 0, 1, "8859_2") + "]");
                         }
                         if () {
                             .append(new String(onebyte, 0, 1, "8859_2"));
                         }
                         .write(onebyte, 0, 1);
                         transmited++;
                     }
                 } catch (SocketException e) {
                     if (!"socket closed".equals(e.getMessage())) {
                         .error("tunneling " +  + " error"e);    
                     } else {
                         .debug("tunneling " +  + " socket closed");  
                     }
                     break;
                 } catch (IOException e) {
                     .error("tunneling " +  + " error"e);
                     break;
                 }
             }
             try {
                 .flush();
             } catch (IOException ignore) {
             }
             .debug("tunnel " +  + " ends, transmited " + transmited + " byte(s)");
             if () {
                 .debug("dump:" + .toString());
             }
         } finally {
             stop();
             notifyListeners();
             close();
         }
     }
     
     public void close() {
         //log.debug("close from", new Throwable("close from"));
     	IOUtils.closeQuietly();
    	IOUtils.closeQuietly();
        this. = null;
        this. = null;
    }

    
Notify Listeners only one time.
    private void notifyListeners() {
        if (this. != null) {
            for (ThreadEndListener l : this.) {
                l.threadFinished(this);
            }
        }
        this..clear();
    }
    /* (non-Javadoc)
     * @see org.unilore.proxy.tunnel.ThreadEndListener#threadFinished(com.di.utils.StopableRunnable)
     */
    public void threadFinished(StopableRunnable r) {
        stop();
    }
New to GrepCode? Check out our FAQ X