Input and Output Stream Pipe in Java

后端 未结 7 1735
闹比i
闹比i 2020-12-17 21:18

Does anyone have any good suggestions for creating a Pipe object in Java which is both an InputStream and and OutputStream since Java does not have multiple inherit

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-17 21:52

    Better to use Pipe or ArrayBlockingQueue, I recommend you not to use PipedInput/OutputStream as they have a bad practice even you can see in the link below that they have asked to be deprecated as it causes many issues.

    https://bugs.openjdk.java.net/browse/JDK-8223048

    For the BlockingQueue and Pipe here a simple example of that

    Pipe:

    Pipe pipe = Pipe.open();
    Pipe.SinkChannel sinkChannel = pipe.sink();
    String newData = "New String to write to file..." + System.currentTimeMillis();
    
    ByteBuffer buf = ByteBuffer.allocate(48);
    buf.clear();
    buf.put(newData.getBytes());
    
    buf.flip();
    
    while(buf.hasRemaining()) {
        sinkChannel.write(buf);
    }
    Pipe.SourceChannel sourceChannel = pipe.source();
    ByteBuffer buf = ByteBuffer.allocate(48);
    
    int bytesRead = inChannel.read(buf);
    

    Reference: http://tutorials.jenkov.com/java-nio/pipe.html

    BlockingQueue:

    //Shared class used by threads
    public class Buffer {
        // ArrayBlockingQueue
        private BlockingQueue blockingQueue = new ArrayBlockingQueue(1);
    
        public void get() {
            // retrieve from ArrayBlockingQueue
            try {
                System.out.println("Consumer received - " + blockingQueue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
        public void put(int data) {
            try {
                // putting in ArrayBlockingQueue
                blockingQueue.put(data);
                System.out.println("Producer produced - " + data);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void main(String[] args) {
            // Starting two threads
            ExecutorService executorService = null;
            try {
                Buffer buffer = new Buffer();
                executorService = Executors.newFixedThreadPool(2);
                executorService.execute(new Producer(buffer));
                executorService.execute(new Consumer(buffer));
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                if(executorService != null) {
                    executorService.shutdown();
                }
            }
        }
    
    public class Consumer implements Runnable {
        private Buffer buffer;
    
        public Consumer(Buffer buffer) {
            this.buffer = buffer;
        }
    
        @Override
        public void run() {
            while (true) {
                try {
                    buffer.get();
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    public class Producer implements Runnable {
        private Buffer buffer;
    
        public Producer(Buffer buffer) {
            this.buffer = buffer;
        }
    
        @Override
        public void run() {
            while (true) {
                Random random = new Random();
                int data = random.nextInt(1000);
                buffer.put(data);
            }
        }
    }
    
    

    Reference: https://github.com/kishanjavatrainer/ArrayBlockingQueueDemo/tree/master/ArrayBlockingQueueDemo

提交回复
热议问题