QueueOutputStream.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.io.output;

  18. import java.io.IOException;
  19. import java.io.InterruptedIOException;
  20. import java.io.OutputStream;
  21. import java.io.PipedInputStream;
  22. import java.io.PipedOutputStream;
  23. import java.util.Objects;
  24. import java.util.concurrent.BlockingQueue;
  25. import java.util.concurrent.LinkedBlockingQueue;

  26. import org.apache.commons.io.input.QueueInputStream;

  27. /**
  28.  * Simple alternative to JDK {@link PipedOutputStream}; queue input stream provides what's written in queue
  29.  * output stream.
  30.  * <p>
  31.  * Example usage:
  32.  * </p>
  33.  *
  34.  * <pre>
  35.  * QueueOutputStream outputStream = new QueueOutputStream();
  36.  * QueueInputStream inputStream = outputStream.newPipeInputStream();
  37.  *
  38.  * outputStream.write("hello world".getBytes(UTF_8));
  39.  * inputStream.read();
  40.  * </pre>
  41.  *
  42.  * Unlike JDK {@link PipedInputStream} and {@link PipedOutputStream}, queue input/output streams may be used safely in a
  43.  * single thread or multiple threads. Also, unlike JDK classes, no special meaning is attached to initial or current
  44.  * thread. Instances can be used longer after initial threads exited.
  45.  * <p>
  46.  * Closing a {@link QueueOutputStream} has no effect. The methods in this class can be called after the stream has been
  47.  * closed without generating an {@link IOException}.
  48.  * </p>
  49.  *
  50.  * @see QueueInputStream
  51.  * @since 2.9.0
  52.  */
  53. public class QueueOutputStream extends OutputStream {

  54.     private final BlockingQueue<Integer> blockingQueue;

  55.     /**
  56.      * Constructs a new instance with no limit to internal buffer size.
  57.      */
  58.     public QueueOutputStream() {
  59.         this(new LinkedBlockingQueue<>());
  60.     }

  61.     /**
  62.      * Constructs a new instance with given buffer.
  63.      *
  64.      * @param blockingQueue backing queue for the stream
  65.      */
  66.     public QueueOutputStream(final BlockingQueue<Integer> blockingQueue) {
  67.         this.blockingQueue = Objects.requireNonNull(blockingQueue, "blockingQueue");
  68.     }

  69.     /**
  70.      * Constructs a new QueueInputStream instance connected to this. Writes to this output stream will be visible to the
  71.      * input stream.
  72.      *
  73.      * @return QueueInputStream connected to this stream
  74.      */
  75.     public QueueInputStream newQueueInputStream() {
  76.         return QueueInputStream.builder().setBlockingQueue(blockingQueue).get();
  77.     }

  78.     /**
  79.      * Writes a single byte.
  80.      *
  81.      * @throws InterruptedIOException if the thread is interrupted while writing to the queue.
  82.      */
  83.     @Override
  84.     public void write(final int b) throws InterruptedIOException {
  85.         try {
  86.             blockingQueue.put(0xFF & b);
  87.         } catch (final InterruptedException e) {
  88.             Thread.currentThread().interrupt();
  89.             final InterruptedIOException interruptedIoException = new InterruptedIOException();
  90.             interruptedIoException.initCause(e);
  91.             throw interruptedIoException;
  92.         }
  93.     }
  94. }