Guest User

Synhronized Streams

a guest
Feb 16th, 2016
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.49 KB | None | 0 0
  1. public class SynchronizedInputStream extends InputStream {
  2.  
  3.     private static volatile boolean running = true;
  4.     private static final BlockingQueue<Integer> readQueue = new LinkedBlockingQueue<>();
  5.     private static final Thread [] catchers = new Thread[256];
  6.    
  7.     private static final SynchronizedInputStream INSTANCE = new SynchronizedInputStream();
  8.    
  9.    
  10.     public static SynchronizedInputStream get() {
  11.         return INSTANCE;
  12.     }
  13.    
  14.     private SynchronizedInputStream() {
  15.         for (int i = -128; i < 128; i++ ) {
  16.             catchers[ i + 128 ] = new Thread( new ByteCatcher( i ) );
  17.             catchers[ i + 128 ].start();
  18.         }
  19.     }
  20.    
  21.     @Override
  22.     public int read() throws IOException {
  23.         if (!running && readQueue.isEmpty()) {
  24.             return -1;
  25.         }
  26.         try {
  27.             return readQueue.take();
  28.         } catch (InterruptedException e) {
  29.             throw new IOException( "Read queue interrupted.", e );
  30.         }
  31.     }
  32.  
  33.     @Override
  34.     public void close() throws IOException {
  35.         running = false;
  36.         for (Thread t : catchers) {
  37.             t.interrupt();
  38.         }
  39.     }
  40.    
  41.     private static class ByteCatcher implements Runnable {
  42.         private final Integer value;
  43.        
  44.         private ByteCatcher( int i ) {
  45.             this.value = i;
  46.         }
  47.        
  48.         @Override
  49.         public void run() {
  50.             while (running) {
  51.                 synchronized (value) {
  52.                     try {
  53.                         value.wait();
  54.                         Integer unsigned = value + 128; // we're doing another unbox/box conversion here but what the hell
  55.                         readQueue.add( unsigned );
  56.                     } catch (InterruptedException e) {
  57.                         return;
  58.                     }
  59.                 }
  60.             }
  61.         }
  62.     }
  63. }
  64.  
  65. public class SynchronizedOutputStream extends OutputStream {
  66.  
  67.     private static final int TRANSMISSION_DELAY = 1;
  68.    
  69.     private static final SynchronizedOutputStream INSTANCE = new SynchronizedOutputStream();
  70.    
  71.     public static final SynchronizedOutputStream get() {
  72.         return INSTANCE;
  73.     }
  74.    
  75.     @Override
  76.     public void write(int arg) throws IOException {
  77.         Integer value = arg - 128;
  78.        
  79.         try {
  80.             Thread.sleep( TRANSMISSION_DELAY );
  81.         } catch (InterruptedException e) {
  82.         }
  83.         synchronized (value) {
  84.             value.notifyAll();
  85.         }
  86.     }
  87.  
  88.     @Override
  89.     public void close() throws IOException {
  90.         SynchronizedInputStream.get().close();
  91.     }
  92.  
  93. }
  94.  
  95. public class Main {
  96.  
  97.     public static void main(String[] args) throws IOException, InterruptedException {
  98.         new Thread() {
  99.             public void run() {
  100.                 final SynchronizedInputStream sis = SynchronizedInputStream.get();
  101.                 InputStreamReader isr = new InputStreamReader( sis );
  102.                 BufferedReader br = new BufferedReader( isr );
  103.                 try {
  104.                     System.out.println( br.readLine() );
  105.                 } catch (IOException e) {
  106.                     e.printStackTrace();
  107.                 }
  108.                
  109.             };
  110.         }.start();
  111.  
  112.         Thread.sleep( 1000 );
  113.        
  114.         SynchronizedOutputStream sos = SynchronizedOutputStream.get();
  115.  
  116.  
  117.         PrintWriter pw = new PrintWriter( sos, true );
  118.         pw.println( "Hello world!");
  119.         pw.flush();
  120.         pw.close();
  121.     }
  122.  
  123. }
Add Comment
Please, Sign In to add comment