package com.amazonaws.lex.twilio.sample.streaming; import java.io.IOException; import java.io.InputStream; import java.io.UncheckedIOException; import java.util.Optional; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; /* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: MIT-0 * * Permission is hereby granted, free of charge, to any person obtaining a copy of this * software and associated documentation files (the "Software"), to deal in the Software * without restriction, including without limitation the rights to use, copy, modify, * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ public class AudioResponse extends InputStream { // Used to convert byte, which is signed in Java, to positive integer (unsigned) private static final int UNSIGNED_BYTE_MASK = 0xFF; private static final long POLL_INTERVAL_MS = 10; private final LinkedBlockingQueue byteQueue = new LinkedBlockingQueue<>(); private volatile boolean closed; @Override public int read() throws IOException { try { Optional maybeInt; while (true) { maybeInt = Optional.ofNullable(this.byteQueue.poll(POLL_INTERVAL_MS, TimeUnit.MILLISECONDS)); // If we get an integer from the queue, return it. if (maybeInt.isPresent()) { return maybeInt.get(); } // If the stream is closed & there is nothing queued up, return -1. if (this.closed) { return -1; } } } catch (InterruptedException e) { throw new IOException(e); } } /** * Writes data into the stream to be offered on future read() calls. */ public void write(byte[] byteArray) { // Don't write into the stream if it is closed already. if (this.closed) { throw new UncheckedIOException(new IOException("Stream already closed when attempting to write into it.")); } for (byte b : byteArray) { this.byteQueue.add(b & UNSIGNED_BYTE_MASK); } } @Override public void close() throws IOException { this.closed = true; super.close(); } }