Class StreamId
Stream id utilities.
A stream id is made of a progressive value and 2 least significant bits that indicate whether the stream is unidirectional (bit 1) and whether the stream is server-initiated (bit 0).
Furthermore, stream id are encoded using VarLenInt
, which steals
the 2 most significant bits, leaving 60 bits for the progressive value,
whose max value is captured by constant MAX_PROGRESSIVE
.
The encoded representation is therefore the following:
6 0
3 0
XX------------------------------------------------------------US
Legend:
X : VarLenInt encoding bit
U : uni/bi directional bit
S : client/server bit
- : progressive bit
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final long
The max value of the progressive part of a stream id. -
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
exceedsMaxProgressive
(long streamId) Tests whether the given stream id exceeds themax progressive value
.static boolean
isBidirectional
(long streamId) Tests whether the given stream id is bidirectional.static boolean
isLocal
(long streamId, boolean client) Returns whether a stream id is local with respect to the givenclient
parameter.static long
newStreamId
(long progressive, boolean bidirectional, boolean client) Generates and returns a new stream id, or -1 if the stream id cannot be generated.static long
progressive
(long streamId) Returns the progressive value of the stream id, that is progressive bits shifted by two bits to the right.static int
type
(long streamId) Returns the encoded stream type, that is the value of the two least significant bits of the stream id.
-
Field Details
-
MAX_PROGRESSIVE
public static final long MAX_PROGRESSIVEThe max value of the progressive part of a stream id.
- See Also:
-
-
Method Details
-
type
public static int type(long streamId) Returns the encoded stream type, that is the value of the two least significant bits of the stream id.
Returns:
- 0 - for bidirectional client stream ids
- 1 - for bidirectional server stream ids
- 2 - for unidirectional client stream ids
- 3 - for unidirectional server stream ids
- Parameters:
streamId
- the stream id to extract the type from- Returns:
- the encoded stream type
-
progressive
public static long progressive(long streamId) Returns the progressive value of the stream id, that is progressive bits shifted by two bits to the right.
For example, for unidirectional client (type 0b10) stream id
0b10110
returns0b101 == 5
.- Parameters:
streamId
- the stream id to extract the progressive from- Returns:
- the progressive value of the stream id
- See Also:
-
exceedsMaxProgressive
public static boolean exceedsMaxProgressive(long streamId) Tests whether the given stream id exceeds the
max progressive value
.- Parameters:
streamId
- the stream id to test- Returns:
- whether the stream id exceeds the max progressive value
- See Also:
-
isBidirectional
public static boolean isBidirectional(long streamId) Tests whether the given stream id is bidirectional.
- Parameters:
streamId
- the stream id to test- Returns:
- whether the stream id is bidirectional
-
newStreamId
public static long newStreamId(long progressive, boolean bidirectional, boolean client) Generates and returns a new stream id, or -1 if the stream id cannot be generated.
Stream ids are typically encoded via
VarLenInt
(which steals the 2 msb), and encode uni/bi directional and client/server (which steals the 2 lsb).Therefore the progressive number only has 60 bits available, or 260-1 values, from 0 to 1152921504606846975; trying to generate a stream id with a larger progressive value results in
-1
to be returned by this method.- Parameters:
progressive
- the stream id progressive numberbidirectional
- whether the stream is bidirectionalclient
- whether the stream is client-initiated- Returns:
- an encoded stream id, or -1 if the stream id cannot be generated
-
isLocal
public static boolean isLocal(long streamId, boolean client) Returns whether a stream id is local with respect to the given
client
parameter.- Parameters:
streamId
- the stream id to testclient
- the side asking for locality- Returns:
- whether the stream id is local to the given side
-