# ParsableBitArray

Artifact: [androidx.media3:media3-common](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/common/util/ParsableBitArray.java+class:androidx.media3.common.util.ParsableBitArray)

*** ** * ** ***


```
@UnstableApi
@CheckReturnValue
public final class ParsableBitArray
```

<br />

*** ** * ** ***

Wraps a byte array, providing methods that allow it to be read as a bitstream.

## Summary

| ### Public fields |
|---|---|
| `byte[]` | `https://developer.android.com/reference/androidx/media3/common/util/ParsableBitArray#data()` |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/media3/common/util/ParsableBitArray#ParsableBitArray()()` Creates a new instance that initially has no backing data. |
| `https://developer.android.com/reference/androidx/media3/common/util/ParsableBitArray#ParsableBitArray(byte[])(byte[] data)` Creates a new instance that wraps an existing array. |
| `https://developer.android.com/reference/androidx/media3/common/util/ParsableBitArray#ParsableBitArray(byte[],int)(byte[] data, int limit)` Creates a new instance that wraps an existing array. |

| ### Public methods |
|---|---|
| `int` | `https://developer.android.com/reference/androidx/media3/common/util/ParsableBitArray#bitsLeft()()` Returns the number of bits yet to be read. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/ParsableBitArray#byteAlign()()` Aligns the position to the next byte boundary. |
| `int` | `https://developer.android.com/reference/androidx/media3/common/util/ParsableBitArray#getBytePosition()()` Returns the current byte offset. |
| `int` | `https://developer.android.com/reference/androidx/media3/common/util/ParsableBitArray#getPosition()()` Returns the current bit offset. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/ParsableBitArray#putInt(int,int)(int value, int numBits)` Overwrites `numBits` from this array using the `numBits` least significant bits from `value`. |
| `boolean` | `https://developer.android.com/reference/androidx/media3/common/util/ParsableBitArray#readBit()()` Reads a single bit. |
| `int` | `https://developer.android.com/reference/androidx/media3/common/util/ParsableBitArray#readBits(int)(int numBits)` Reads up to 32 bits. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/ParsableBitArray#readBits(byte[],int,int)(byte[] buffer, int offset, int numBits)` Reads `numBits` bits into `buffer`. |
| `long` | `https://developer.android.com/reference/androidx/media3/common/util/ParsableBitArray#readBitsToLong(int)(int numBits)` Reads up to 64 bits. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/ParsableBitArray#readBytes(byte[],int,int)(byte[] buffer, int offset, int length)` Reads the next `length` bytes into `buffer`. |
| `https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/media3/common/util/ParsableBitArray#readBytesAsString(int)(int length)` Reads the next `length` bytes as a UTF-8 string. |
| `https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/media3/common/util/ParsableBitArray#readBytesAsString(int,java.nio.charset.Charset)(int length, https://developer.android.com/reference/java/nio/charset/Charset.html charset)` Reads the next `length` bytes as a string encoded in `https://developer.android.com/reference/java/nio/charset/Charset.html`. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/ParsableBitArray#reset(byte[])(byte[] data)` Updates the instance to wrap `data`, and resets the position to zero. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/ParsableBitArray#reset(androidx.media3.common.util.ParsableByteArray)(https://developer.android.com/reference/androidx/media3/common/util/ParsableByteArray parsableByteArray)` Sets this instance's data, position and limit to match the provided `parsableByteArray`. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/ParsableBitArray#reset(byte[],int)(byte[] data, int limit)` Updates the instance to wrap `data`, and resets the position to zero. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/ParsableBitArray#setPosition(int)(int position)` Sets the current bit offset. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/ParsableBitArray#skipBit()()` Skips a single bit. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/ParsableBitArray#skipBits(int)(int numBits)` Skips bits and moves current reading position forward. |
| `void` | `https://developer.android.com/reference/androidx/media3/common/util/ParsableBitArray#skipBytes(int)(int length)` Skips the next `length` bytes. |

## Public fields

### data

```
public byte[] data
```

## Public constructors

### ParsableBitArray

```
public ParsableBitArray()
```

Creates a new instance that initially has no backing data.

### ParsableBitArray

```
public ParsableBitArray(byte[] data)
```

Creates a new instance that wraps an existing array.

| Parameters |
|---|---|
| `byte[] data` | The data to wrap. |

### ParsableBitArray

```
public ParsableBitArray(byte[] data, int limit)
```

Creates a new instance that wraps an existing array.

| Parameters |
|---|---|
| `byte[] data` | The data to wrap. |
| `int limit` | The limit in bytes. |

## Public methods

### bitsLeft

```
public int bitsLeft()
```

Returns the number of bits yet to be read.

### byteAlign

```
public void byteAlign()
```

Aligns the position to the next byte boundary. Does nothing if the position is already aligned.

### getBytePosition

```
public int getBytePosition()
```

Returns the current byte offset. Must only be called when the position is byte aligned.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | If the position isn't byte aligned. |

### getPosition

```
public int getPosition()
```

Returns the current bit offset.

### putInt

```
public void putInt(int value, int numBits)
```

Overwrites `numBits` from this array using the `numBits` least significant bits from `value`. Bits are written in order from most significant to least significant. The read position is advanced by `numBits`.

| Parameters |
|---|---|
| `int value` | The integer whose `numBits` least significant bits are written into `https://developer.android.com/reference/androidx/media3/common/util/ParsableBitArray#data()`. |
| `int numBits` | The number of bits to write. |

### readBit

```
public boolean readBit()
```

Reads a single bit.

| Returns |
|---|---|
| `boolean` | Whether the bit is set. |

### readBits

```
public int readBits(int numBits)
```

Reads up to 32 bits.

| Parameters |
|---|---|
| `int numBits` | The number of bits to read. |

| Returns |
|---|---|
| `int` | An integer whose bottom `numBits` bits hold the read data. |

### readBits

```
public void readBits(byte[] buffer, int offset, int numBits)
```

Reads `numBits` bits into `buffer`.

| Parameters |
|---|---|
| `byte[] buffer` | The array into which the read data should be written. The trailing `numBits % 8` bits are written into the most significant bits of the last modified `buffer` byte. The remaining ones are unmodified. |
| `int offset` | The offset in `buffer` at which the read data should be written. |
| `int numBits` | The number of bits to read. |

### readBitsToLong

```
public long readBitsToLong(int numBits)
```

Reads up to 64 bits.

| Parameters |
|---|---|
| `int numBits` | The number of bits to read. |

| Returns |
|---|---|
| `long` | A long whose bottom `numBits` bits hold the read data. |

### readBytes

```
public void readBytes(byte[] buffer, int offset, int length)
```

Reads the next `length` bytes into `buffer`. Must only be called when the position is byte aligned.

| Parameters |
|---|---|
| `byte[] buffer` | The array into which the read data should be written. |
| `int offset` | The offset in `buffer` at which the read data should be written. |
| `int length` | The number of bytes to read. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | If the position isn't byte aligned. |

| See also |
|---|---|
| `https://developer.android.com/reference/java/lang/System.html#arraycopy(java.lang.Object, int, java.lang.Object, int, int)` |   |

### readBytesAsString

```
public String readBytesAsString(int length)
```

Reads the next `length` bytes as a UTF-8 string. Must only be called when the position is byte aligned.

| Parameters |
|---|---|
| `int length` | The number of bytes to read. |

| Returns |
|---|---|
| `https://developer.android.com/reference/java/lang/String.html` | The string encoded by the bytes in UTF-8. |

### readBytesAsString

```
public String readBytesAsString(int length, Charset charset)
```

Reads the next `length` bytes as a string encoded in `https://developer.android.com/reference/java/nio/charset/Charset.html`. Must only be called when the position is byte aligned.

| Parameters |
|---|---|
| `int length` | The number of bytes to read. |
| `https://developer.android.com/reference/java/nio/charset/Charset.html charset` | The character set of the encoded characters. |

| Returns |
|---|---|
| `https://developer.android.com/reference/java/lang/String.html` | The string encoded by the bytes in the specified character set. |

### reset

```
public void reset(byte[] data)
```

Updates the instance to wrap `data`, and resets the position to zero.

| Parameters |
|---|---|
| `byte[] data` | The array to wrap. |

### reset

```
public void reset(ParsableByteArray parsableByteArray)
```

Sets this instance's data, position and limit to match the provided `parsableByteArray`. Any modifications to the underlying data array will be visible in both instances

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/util/ParsableByteArray parsableByteArray` | The `https://developer.android.com/reference/androidx/media3/common/util/ParsableByteArray`. |

### reset

```
public void reset(byte[] data, int limit)
```

Updates the instance to wrap `data`, and resets the position to zero.

| Parameters |
|---|---|
| `byte[] data` | The array to wrap. |
| `int limit` | The limit in bytes. |

### setPosition

```
public void setPosition(int position)
```

Sets the current bit offset.

| Parameters |
|---|---|
| `int position` | The position to set. |

### skipBit

```
public void skipBit()
```

Skips a single bit.

### skipBits

```
public void skipBits(int numBits)
```

Skips bits and moves current reading position forward.

| Parameters |
|---|---|
| `int numBits` | The number of bits to skip. |

### skipBytes

```
public void skipBytes(int length)
```

Skips the next `length` bytes. Must only be called when the position is byte aligned.

| Parameters |
|---|---|
| `int length` | The number of bytes to read. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | If the position isn't byte aligned. |