thufir

scanner

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