Class QuicStream

java.lang.Object
io.github.mahmoudimus.quic.QuicStream
All Implemented Interfaces:
Closeable, AutoCloseable

public final class QuicStream extends Object implements Closeable
One QUIC stream of a QuicConnection, with blocking InputStream and OutputStream views. A bidirectional stream has both; a unidirectional stream has only the side its direction allows: output on the side that opened it, input on the other.
  • The output stream copies what is written and blocks while flow control or the send window hold data back; close() sends the FIN.
  • The input stream blocks until data arrives and returns -1 after the FIN; it throws a QuicStreamException if the peer reset the stream.
  • reset(long) abandons sending (RESET_STREAM); stopSending(long) asks the peer to stop (STOP_SENDING); writes then fail with a QuicStreamException.

The views are safe to use from different threads, though one reader and one writer at a time is the sensible pattern.

  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Waits until the peer asks this side to stop sending (STOP_SENDING), which otherwise shows only when a write fails: for a relay that passes the request on while it is not writing.
    void
    Closes both directions: sends the FIN if the output is open, and STOP_SENDING (code 0) if the input was not read to its end.
    The connection.
    The stream's data from the peer.
    The stream's data to the peer.
    long
    id()
    The stream ID (RFC 9000 section 2.1).
    boolean
    Whether the stream is bidirectional.
    boolean
    Whether this side opened the stream.
    boolean
    Whether the peer is done with the stream, as far as this side has received, read or not: its FIN or a RESET_STREAM arrived, or it asked this side to stop sending (STOP_SENDING); or the connection is gone.
    boolean
    Server: whether any of the stream's data arrived in 0-RTT packets.
    void
    reset(long errorCode)
    Abandons sending (RESET_STREAM): unsent and unacknowledged data is discarded.
    void
    stopSending(long errorCode)
    Asks the peer to stop sending (STOP_SENDING); data not yet read is discarded.
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Method Details

    • id

      public long id()
      The stream ID (RFC 9000 section 2.1).
      Returns:
      the ID
    • isBidirectional

      public boolean isBidirectional()
      Whether the stream is bidirectional.
      Returns:
      true for a bidirectional stream
    • receivedEarlyData

      public boolean receivedEarlyData()
      Server: whether any of the stream's data arrived in 0-RTT packets. Such data may be a replay of an earlier connection's (RFC 8446 section 8); act on it only if that is safe, or wait for the handshake.
      Returns:
      true if the peer sent part of the stream as early data
    • isLocal

      public boolean isLocal()
      Whether this side opened the stream.
      Returns:
      true for a locally opened stream
    • connection

      public QuicConnection connection()
      The connection.
      Returns:
      the connection
    • getInputStream

      public InputStream getInputStream()
      The stream's data from the peer.
      Returns:
      the input stream
      Throws:
      IllegalStateException - for a unidirectional stream this side opened
    • getOutputStream

      public OutputStream getOutputStream()
      The stream's data to the peer.
      Returns:
      the output stream; closing it sends the FIN
      Throws:
      IllegalStateException - for a unidirectional stream the peer opened
    • reset

      public void reset(long errorCode) throws IOException
      Abandons sending (RESET_STREAM): unsent and unacknowledged data is discarded.
      Parameters:
      errorCode - the application error code
      Throws:
      IOException - if the connection is gone or the stream was already reset or finished and acknowledged
    • stopSending

      public void stopSending(long errorCode) throws IOException
      Asks the peer to stop sending (STOP_SENDING); data not yet read is discarded.
      Parameters:
      errorCode - the application error code
      Throws:
      IOException - if the connection is gone or the stream is already finished
    • awaitStopped

      public long awaitStopped() throws InterruptedIOException
      Waits until the peer asks this side to stop sending (STOP_SENDING), which otherwise shows only when a write fails: for a relay that passes the request on while it is not writing.
      Returns:
      the peer's error code, or -1 once the peer can no longer stop the stream: the output was finished or reset here, or the connection is gone
      Throws:
      InterruptedIOException - if interrupted while waiting
      IllegalStateException - for a unidirectional stream the peer opened
    • peerDone

      public boolean peerDone()
      Whether the peer is done with the stream, as far as this side has received, read or not: its FIN or a RESET_STREAM arrived, or it asked this side to stop sending (STOP_SENDING); or the connection is gone. The connection's frames are processed in the order they arrive, before any thread reads them: an application that reads each stream on a thread of its own learns here, on another stream's thread, what the peer sent on this one before what it is reading.
      Returns:
      true once the peer ended, reset or stopped the stream
    • close

      public void close() throws IOException
      Closes both directions: sends the FIN if the output is open, and STOP_SENDING (code 0) if the input was not read to its end.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException - if the connection is gone
    • toString

      public String toString()
      Overrides:
      toString in class Object