net.i2p.router.transport.udp

## Class SSU2Bitfield

- java.lang.Object
- - net.i2p.router.transport.udp.SSU2Bitfield

-     class SSU2Bitfield
      extends Object

  Container of a long array representing set and unset bits. When a bit higher than the current size + offset is set, the offset shifts up and the lowest set bits are lost. Also contains methods to convert to/from an ACK block. Locking: Most methods are synchronized here. Do not call methods with the PeerState2 lock held, chance of deadlock.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static interface` | `SSU2Bitfield.Callback` Callback interface for bitfield operations.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `SSU2Bitfield(int size, long offset)` Creates a new SSU2Bitfield that represents `size` unset bits.

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `forEachAndNot(SSU2Bitfield bf2, SSU2Bitfield.Callback cb)` Callback for all bits set in this bitfield but not set in bf2.
    `static SSU2Bitfield` | `fromACKBlock(long thru, int acnt, byte[] ranges, int rangeCount)`
    `boolean` | `get(long bit)` Return true if the bit is set or false if it is not.
    `long` | `getHighestSet()` Return the highest set bit, or -1 if none.
    `long` | `getOffset()`
    `boolean` | `set(long bit)` Sets the given bit to true.
    `int` | `size()`
    `SSU2Payload.AckBlock` | `toAckBlock(int maxRanges)`
    `String` | `toString()`
    `static String` | `toString(long thru, int acnt, byte[] ranges, int rangeCount)` Pretty print an ACK block

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### SSU2Bitfield

          public SSU2Bitfield(int size,
                              long offset)

      Creates a new SSU2Bitfield that represents `size` unset bits.

  - ### Method Detail

    - #### forEachAndNot

          public void forEachAndNot(SSU2Bitfield bf2,
                                    SSU2Bitfield.Callback cb)

      Callback for all bits set in this bitfield but not set in bf2. If this offset is greater than bf2's highest bit set, i.e. this bitfield is completely newer, calls back for all bits in this bitfield. If this highest bit set is less than than bf2's offset, i.e. this bitfield is completely older, the callback will not be called. Synchs on this and then on bf2. Usage: this is the received acks, bf2 is previously acked, callback for each newly acked.

    - #### fromACKBlock

          public static SSU2Bitfield fromACKBlock(long thru,
                                                  int acnt,
                                                  byte[] ranges,
                                                  int rangeCount)

      Parameters:
        `ranges` - may be null

    - #### get

          public boolean get(long bit)

      Return true if the bit is set or false if it is not.
      Throws:
        `IndexOutOfBoundsException` - if bit is smaller then zero

    - #### getHighestSet

          public long getHighestSet()

      Return the highest set bit, or -1 if none.

    - #### getOffset

          public long getOffset()

    - #### set

          public boolean set(long bit)
                      throws IndexOutOfBoundsException

      Sets the given bit to true. When a bit higher than the current size + offset is set, the offset shifts up and the lowest set bits are lost.
      Returns:
        previous value, true if previously set or unknown
      Throws:
        `IndexOutOfBoundsException` - if bit is smaller then zero OR if the shift is too big

    - #### size

          public int size()

    - #### toAckBlock

          public SSU2Payload.AckBlock toAckBlock(int maxRanges)

      Parameters:
        `maxRanges` - may be 0
      Returns:
        null if nothing is set

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

    - #### toString

          public static String toString(long thru,
                                        int acnt,
                                        byte[] ranges,
                                        int rangeCount)

      Pretty print an ACK block
      Parameters:
        `ranges` - may be null