code: private void sendInputStream(InputStream inputStream) {
      byte[] buffer = new byte[ONE_KB];
      int read;
      try {
        // This method uses a blocking while loop to receive all contents of the underlying input stream.
        // AudioInputStreams, typically used for streaming microphone inputs return 0 only when the stream has been
        // closed. Elsewise AudioInputStream.read() blocks until enough audio frames are read.
        while (((read = inputStream.read(buffer)) > 0) && socketOpen) {
  
          // If OkHttp's WebSocket queue gets overwhelmed, it'll abruptly close the connection
          // (see: https://github.com/square/okhttp/issues/3317). This will ensure we wait until the coast is clear.
          while (socket.queueSize() > QUEUE_SIZE_LIMIT) {
            Thread.sleep(QUEUE_WAIT_MILLIS);
          }
  
          if (read == ONE_KB) {
            socket.send(ByteString.of(buffer));
          } else {
            socket.send(ByteString.of(Arrays.copyOfRange(buffer, 0, read)));
          }
        }
      } catch (IOException | InterruptedException e) {
        LOG.log(Level.SEVERE, e.getMessage(), e);
      } finally {
        try {
          inputStream.close();
        } catch (IOException e) {
          // do nothing - the InputStream may have already been closed externally.
        }
      }
    }
docs: Send input stream.
  
  @param inputStream the input stream