thufir

line of input logs, but that's it.

Sep 17th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.42 KB | None | 0 0
  1. package telnet;
  2.  
  3. import java.io.BufferedReader;
  4. import player.TelnetParser;
  5. import java.io.BufferedWriter;
  6. import java.io.File;
  7. import java.io.FileNotFoundException;
  8. import java.io.FileOutputStream;
  9. import java.io.FileWriter;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.io.InputStreamReader;
  13. import java.io.OutputStream;
  14. import java.io.PrintWriter;
  15. import java.util.Observable;
  16. import java.util.logging.Level;
  17. import java.util.logging.Logger;
  18. import org.apache.commons.io.output.TeeOutputStream;
  19.  
  20. public class InputOutput extends Observable {
  21.  
  22.     private static final Logger log = Logger.getLogger(InputOutput.class.getName());
  23.  
  24.     public InputOutput() {
  25.     }
  26.  
  27.     private void readFromConsole(final OutputStream outputStream) {
  28.         Thread read = new Thread() {
  29.  
  30.             @Override
  31.             public void run() {
  32.                 String line;
  33.                 byte[] bytes;
  34.                 BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
  35.                 while (true) {
  36.                     try {
  37.                         line = buffer.readLine();
  38.                         log.info(line);
  39.                         bytes = line.getBytes();
  40.                         outputStream.write(bytes);
  41.                     } catch (IOException ex) {
  42.                     }
  43.                 }
  44.             }
  45.         };
  46.         read.start();
  47.     }
  48.  
  49.     private void readInput(final InputStream inputStream) throws FileNotFoundException, IOException {
  50.         Thread readInput = new Thread() {
  51.  
  52.             @Override
  53.             public void run() {
  54.                 char ch = 0;
  55.                 int intVal = 0;
  56.                 StringBuilder sb = new StringBuilder();
  57.                 TelnetParser rx = new TelnetParser();
  58.  
  59.                 try {
  60.                     while ((intVal = inputStream.read()) != -1) {
  61.                         ch = (char) intVal;
  62.                         printToConsole(ch);
  63.                         //logToFile(ch);
  64.                         sb.append(ch);
  65.                         if (intVal == 13) {
  66.                             setChanged();
  67.                             notifyObservers(sb.toString());
  68.                             sb = new StringBuilder();
  69.                         }
  70.                     }
  71.                 } catch (IOException ex) {
  72.                     Logger.getLogger(InputOutput.class.getName()).log(Level.SEVERE, null, ex);
  73.                 }
  74.  
  75.  
  76.             }
  77.  
  78.             private void logToFile(char c) throws IOException {
  79.                 String fname = "weather.log";
  80.                 File f = new File(fname);
  81.                 f.createNewFile();
  82.                 try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fname, true)))) {
  83.                     out.print(c);
  84.                     out.flush();
  85.                 }
  86.             }
  87.  
  88.             private void printToConsole(char c) {
  89.                 System.out.print(c);
  90.             }
  91.         };
  92.         readInput.start();
  93.     }
  94.  
  95.     public void readWriteParse(final InputStream inputStream, final OutputStream outputStream) throws FileNotFoundException, IOException {
  96.         readFromConsole(outputStream);
  97.         readInput(inputStream);
  98.     }
  99.  
  100.     //                TeeOutputStream tee = new TeeOutputStream(inputStream, bis);  
  101.     private void tee(FileOutputStream fos) {
  102.         TeeOutputStream tee = new TeeOutputStream(System.out, fos);
  103.  
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment