Package io.github.mahmoudimus.quic
Class QuicStream
java.lang.Object
io.github.mahmoudimus.quic.QuicStream
- All Implemented Interfaces:
Closeable,AutoCloseable
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
QuicStreamExceptionif the peer reset the stream. reset(long)abandons sending (RESET_STREAM);stopSending(long)asks the peer to stop (STOP_SENDING); writes then fail with aQuicStreamException.
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 TypeMethodDescriptionlongWaits 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.voidclose()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.longid()The stream ID (RFC 9000 section 2.1).booleanWhether the stream is bidirectional.booleanisLocal()Whether this side opened the stream.booleanpeerDone()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.booleanServer: whether any of the stream's data arrived in 0-RTT packets.voidreset(long errorCode) Abandons sending (RESET_STREAM): unsent and unacknowledged data is discarded.voidstopSending(long errorCode) Asks the peer to stop sending (STOP_SENDING); data not yet read is discarded.toString()
-
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 forthe 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
The connection.- Returns:
- the connection
-
getInputStream
The stream's data from the peer.- Returns:
- the input stream
- Throws:
IllegalStateException- for a unidirectional stream this side opened
-
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
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
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
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 waitingIllegalStateException- 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
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:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- if the connection is gone
-
toString
-