pydicom.filebase.DicomBytesIO

class pydicom.filebase.DicomBytesIO(initial_bytes: bytes | bytearray | memoryview = b'')[source]

Wrapper for io.BytesIO to simplify encoding/decoding DICOM datasets.

__init__(initial_bytes: bytes | bytearray | memoryview = b'') None[source]

Create a new DicomBytesIO instance.

Parameters:

buffer (bytes | bytearray | memoryview, optional) – The buffer to write to or read from, default is an empty buffer.

Methods

__init__([initial_bytes])

Create a new DicomBytesIO instance.

close(*args, **kwargs)

Close the buffer (if possible)

read([size])

Read up to size bytes from the buffer and return them.

read_UL()

Return a UL value read from the buffer.

read_US()

Return a US value read from the buffer.

read_exact(length[, nr_retries])

Return length bytes read from the buffer.

read_tag()

Return a DICOM tag value read from the buffer.

seek(offset[, whence])

Change the buffer position to the given byte offset, relative to the position indicated by whence and return the new absolute position.

tell()

Return the current stream position of the buffer

write(b, /)

Write the bytes-like object b to the buffer and return the number of bytes written.

write_UL(val)

Write a UL value to the buffer.

write_US(val)

Write a US value to the buffer.

write_tag(tag)

Write a DICOM tag to the buffer.

Attributes

is_implicit_VR

Get/set the VR mode for encoding/decoding.

is_little_endian

Get/set the endianness for encoding/decoding, True for little endian and False for big endian.

name

Return the value of the parent's name attribute, or None if no such attribute.

parent

Return the buffer object being wrapped.

close(*args: Any, **kwargs: Any) Any[source]

Close the buffer (if possible)

property is_implicit_VR: bool

Get/set the VR mode for encoding/decoding. True for implicit VR and False for explicit VR.

property is_little_endian: bool

Get/set the endianness for encoding/decoding, True for little endian and False for big endian.

property name: str | None

Return the value of the parent’s name attribute, or None if no such attribute.

property parent: ReadableBuffer | WriteableBuffer

Return the buffer object being wrapped.

read(size: int = -1, /) bytes[source]

Read up to size bytes from the buffer and return them. If size is unspecified, all bytes until EOF are returned.

Fewer than size bytes may be returned if the operating system call returns fewer than size bytes.

read_UL() int[source]

Return a UL value read from the buffer.

read_US() int[source]

Return a US value read from the buffer.

read_exact(length: int, nr_retries: int = 3) bytes[source]

Return length bytes read from the buffer.

Parameters:
  • length (int) – The number of bytes to be read. If None (default) then read all the bytes available.

  • nr_retries (int, optional) – The number of tries to read data when the number of bytes read from the buffer is less than length. Default 3.

Returns:

The read data.

Return type:

bytes

Raises:

EOFError – If unable to read length bytes.

read_tag() tuple[int, int][source]

Return a DICOM tag value read from the buffer.

seek(offset: int, whence: int = 0, /) int[source]

Change the buffer position to the given byte offset, relative to the position indicated by whence and return the new absolute position.

tell() int[source]

Return the current stream position of the buffer

write(b: bytes | bytearray | memoryview, /) int[source]

Write the bytes-like object b to the buffer and return the number of bytes written.

write_UL(val: int) None[source]

Write a UL value to the buffer.

write_US(val: int) None[source]

Write a US value to the buffer.

write_tag(tag: int) None[source]

Write a DICOM tag to the buffer.