PyByteBuffer

1.0.5 · maintenance · verified Sun Apr 12

PyByteBuffer is a Python library for manipulating byte buffers, drawing inspiration from Java's `java.nio.ByteBuffer`. It provides methods for writing and reading various data types (integers, strings, arrays, bytes) into a buffer with control over aspects like endianness and position. The current stable version is 1.0.5, released in late 2020, and it appears to be in a maintenance status, stable for its intended functionality.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a ByteBuffer, write different data types into it, reset its position, and then read the data back. It also highlights the behavior of `get()` versus `read()` methods concerning position advancement.

from PyByteBuffer import ByteBuffer

# Create a ByteBuffer with an initial capacity
bb = ByteBuffer(1024)

# Write various data types
bb.write(12345, 'int', 4)  # Write an int (4 bytes)
bb.write('Hello PyByteBuffer', 'str') # Write a string
bb.write(b'\x01\x02\x03', 'bytes') # Write raw bytes

# Reset position to read from the beginning
bb.position(0)

# Read the data back
int_val = bb.read('int', 4)
str_val = bb.read('str', 18) # Read 18 characters for 'Hello PyByteBuffer'
bytes_val = bb.read('bytes', 3)

print(f'Read int: {int_val}')
print(f'Read string: {str_val}')
print(f'Read bytes: {bytes_val}')

# Example of getting without advancing position
bb.position(0)
first_byte = bb.get()
print(f'First byte (get, position unchanged): {first_byte}')
print(f'Position after get(): {bb.position()}')
first_byte_again = bb.get()
print(f'First byte again (get, position unchanged): {first_byte_again}')

view raw JSON →