package ru.mail.teamcity.ssh.task;

import org.atmosphere.cpr.AtmosphereResponse;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * User: g.chernyshev
 * Date: 29/04/15
 * Time: 00:27
 */
public class ShellOutputProcessor implements Runnable {

    InputStream outFromChannel;
    AtmosphereResponse response;
    private volatile boolean running = true;


    public ShellOutputProcessor(InputStream outFromChannel, AtmosphereResponse response) {
        this.outFromChannel = outFromChannel;
        this.response = response;
    }

    public void run() {
        System.out.println("Starting thread " + this);
        InputStreamReader isr = new InputStreamReader(outFromChannel);
        BufferedReader reader = new BufferedReader(isr, 4096);


        char[] chars = new char[1024];
        synchronized (isr) { // TODO -- is this redundant?
            try {
                int count;
                while ((count = isr.read(chars)) != -1) {
//            while ((count = reader.read(chars)) != -1) {
                    if (!running) {
                        break;
                    }
                    String chunk = new String(chars, 0, count);
                    response.write(chunk);
                    System.out.println("Count: " + count);
                    System.out.println("Chunk: " + chunk);
                    Thread.sleep(50);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void terminate() {
        running = false;
    }
}

@@ -80,39 +80,8 @@
 e;%0A%0A
-import java.io.BufferedReader;%0A
 impo
@@ -135,42 +135,8 @@
 eam;
-%0Aimport java.io.InputStreamReader;
 %0A%0A/*
@@ -626,253 +626,82 @@
 s);%0A
- 
+%0A
        
-InputStreamReader isr = new InputStreamReader(outFromChannel);%0A        BufferedReader reader = new BufferedReader(isr, 4096);%0A%0A%0A        char%5B%5D chars = new char%5B1024%5D;%0A        synchronized (isr) %7B // TODO -- is this redundant?%0A   
+ byte%5B%5D buff = new byte%5B1024%5D; // TODO hardcoded constant%0A
         
  try
@@ -700,19 +700,14 @@
     
- 
 try %7B%0A
-    
     
@@ -729,36 +729,32 @@
 nt;%0A            
-    
 while ((count = 
@@ -757,81 +757,32 @@
 t = 
-isr.read(chars)) != -1) %7B%0A//            while ((count = reader
+outFromChannel
 .read(
-chars
+buff
 )) !
@@ -785,28 +785,24 @@
 )) != -1) %7B%0A
-    
             
@@ -845,23 +845,15 @@
     
-    
 break;%0A
-    
     
@@ -858,36 +858,34 @@
               %7D%0A
-    
+//
                 
@@ -914,13 +914,12 @@
 ing(
-chars
+buff
 , 0,
@@ -947,39 +947,136 @@
     
-    response.write(chunk);%0A    
+String chunk = printc(buff, 0, count);%0A                //response.write(chunk);%0A                response.write(buff, 0, count);%0A
     
@@ -1134,36 +1134,32 @@
                 
-  
-  
 System.out.print
@@ -1193,24 +1193,26 @@
         
+%7D%0A
         
 Thread.s
@@ -1207,226 +1207,914 @@
     
-Thread.sleep(50);%0A            
+%7D catch (IOException e) %7B%0A            e.printStackTrace();%0A
     
-%7D%0A
     
+%7D%0A
     
+%7D%0A%0A
     
-%7D catch (IOException e) %7B%0A                e.printStackTrace();%0A            %7D catch (InterruptedException e) %7B%0A                e.printStackTrace();%0A            %7D%0A        %7D
+private static char%5B%5D hex2char = %220123456789ABCDEF%22.toCharArray();%0A%0A    public static String printc(byte%5B%5D buffer) %7B%0A        return printc(buffer, 0, buffer.length);%0A    %7D%0A%0A    public static String printc(byte%5B%5D buffer, int offset, int count) %7B%0A        StringBuilder builder = new StringBuilder(4 * count);%0A        int limit = offset + count;%0A        for (int i = offset; i %3C limit; i++) %7B%0A            int b = buffer%5Bi%5D;%0A            if (b %3C 0x20) %7B%0A                builder.append(%22%5C%5Cx%22);%0A                builder.append(hex2char%5B(b & 0xF0) %3E%3E 4%5D);%0A                builder.append(hex2char%5B(b & 0xF)%5D);%0A            %7D else %7B%0A                if (b == '%22' %7C%7C b == '%5C%5C') %7B%0A                    builder.append('%5C%5C');%0A                %7D%0A                builder.append((char) b);%0A            %7D%0A        %7D%0A        return builder.toString();
 %0A   

Test realisation