TSDuck v3.38-3696
MPEG Transport Stream Toolkit
Loading...
Searching...
No Matches
ts::ByteBlock Class Reference

Definition of a generic block of bytes. More...

#include <tsByteBlock.h>

Inheritance diagram for ts::ByteBlock:
Collaboration diagram for ts::ByteBlock:

Public Types

using ByteVector = std::vector< uint8_t >
 Explicit name of superclass, std::vector on uint8_t.
 

Public Member Functions

 ByteBlock (const char *str)
 Constructor from a C string.
 
 ByteBlock (const void *data, size_type size)
 Constructor from a data block.
 
 ByteBlock (size_type size, uint8_t value)
 Constructor, initialized with bytes of specified value.
 
 ByteBlock (size_type size=0)
 Default constructor.
 
 ByteBlock (std::initializer_list< uint8_t > init)
 Constructor from an initializer list.
 
void append (const ByteBlock &bb)
 Append a byte block to a byte block.
 
void append (const std::string &s)
 Append a string to a byte block.
 
void append (const void *data, size_type size)
 Append raw data to a byte block.
 
template<typename INT , typename std::enable_if< std::is_integral< INT >::value >::type * = nullptr>
void append (INT i)
 Append an integer in big endian representation at the end.
 
std::istream & append (std::istream &strm, size_t maxSize=std::numeric_limits< size_t >::max())
 Read a byte block from standard streams and append to existing content (binary mode).
 
void append (uint8_t i, size_type size)
 Append n bytes with value i.
 
void appendBCD (uint32_t value, size_t bcd_count, bool left_justified=true, uint8_t pad_nibble=0)
 Append an integer in Binary Coded Decimal (BCD) representation at the end.
 
bool appendFromFile (const UString &fileName, size_t maxSize=std::numeric_limits< size_t >::max(), Report *report=nullptr)
 Read a byte block from a binary file and append to existing content.
 
void appendInt16 (int16_t i)
 Append an integer in big endian representation at the end.
 
void appendInt16BE (int16_t i)
 Append an integer in big endian representation at the end.
 
void appendInt16LE (int16_t i)
 Append an integer in little endian representation at the end.
 
void appendInt24 (int32_t i)
 Append an integer in big endian representation at the end.
 
void appendInt24BE (int32_t i)
 Append an integer in big endian representation at the end.
 
void appendInt24LE (int32_t i)
 Append an integer in little endian representation at the end.
 
void appendInt32 (int32_t i)
 Append an integer in big endian representation at the end.
 
void appendInt32BE (int32_t i)
 Append an integer in big endian representation at the end.
 
void appendInt32LE (int32_t i)
 Append an integer in little endian representation at the end.
 
void appendInt64 (int64_t i)
 Append an integer in big endian representation at the end.
 
void appendInt64BE (int64_t i)
 Append an integer in big endian representation at the end.
 
void appendInt64LE (int64_t i)
 Append an integer in little endian representation at the end.
 
void appendInt8 (int8_t i)
 Append an integer in big endian representation at the end.
 
bool appendToFile (const UString &fileName, Report *report=nullptr) const
 Save a byte block to a binary file, append to existing file content.
 
void appendUInt16 (uint16_t i)
 Append an integer in big endian representation at the end.
 
void appendUInt16BE (uint16_t i)
 Append an integer in big endian representation at the end.
 
void appendUInt16LE (uint16_t i)
 Append an integer in little endian representation at the end.
 
void appendUInt24 (uint32_t i)
 Append an integer in big endian representation at the end.
 
void appendUInt24BE (uint32_t i)
 Append an integer in big endian representation at the end.
 
void appendUInt24LE (uint32_t i)
 Append an integer in little endian representation at the end.
 
void appendUInt32 (uint32_t i)
 Append an integer in big endian representation at the end.
 
void appendUInt32BE (uint32_t i)
 Append an integer in big endian representation at the end.
 
void appendUInt32LE (uint32_t i)
 Append an integer in little endian representation at the end.
 
void appendUInt40 (uint64_t i)
 Append an integer in big endian representation at the end.
 
void appendUInt40BE (uint64_t i)
 Append an integer in big endian representation at the end.
 
void appendUInt40LE (uint64_t i)
 Append an integer in little endian representation at the end.
 
void appendUInt48 (uint64_t i)
 Append an integer in big endian representation at the end.
 
void appendUInt48BE (uint64_t i)
 Append an integer in big endian representation at the end.
 
void appendUInt48LE (uint64_t i)
 Append an integer in little endian representation at the end.
 
void appendUInt64 (uint64_t i)
 Append an integer in big endian representation at the end.
 
void appendUInt64BE (uint64_t i)
 Append an integer in big endian representation at the end.
 
void appendUInt64LE (uint64_t i)
 Append an integer in little endian representation at the end.
 
void appendUInt8 (uint8_t i)
 Append an integer in big endian representation at the end.
 
void appendUTF8 (const UString &s)
 Append a unicode string in UTF-8 representation to a byte block.
 
void appendUTF8WithByteLength (const UString &s)
 Append a unicode string in UTF-8 representation to a byte block with one-byte preceding length.
 
void copy (const void *data, size_type size)
 Replace the content of a byte block.
 
uint8_t * enlarge (size_type n)
 Increase size return pointer to new area at end of block.
 
void erase (size_type first, size_type size)
 Remove 'size' elements at index 'first'.
 
size_type find (uint8_t value, size_type start=0)
 Find the first occurence of a byte value in a byte block.
 
bool loadFromFile (const UString &fileName, size_t maxSize=std::numeric_limits< size_t >::max(), Report *report=nullptr)
 Read a byte block from a binary file.
 
std::istream & read (std::istream &strm, size_t maxSize=std::numeric_limits< size_t >::max())
 Read a byte block from standard streams (binary mode).
 
bool saveToFile (const UString &fileName, Report *report=nullptr) const
 Save a byte block to a binary file.
 
std::ostream & write (std::ostream &strm) const
 Write a byte to standard streams (binary mode).
 

Detailed Description

Definition of a generic block of bytes.

This is a subclass of std::vector on uint8_t.

Constructor & Destructor Documentation

◆ ByteBlock() [1/5]

ts::ByteBlock::ByteBlock ( size_type  size = 0)
explicit

Default constructor.

Parameters
[in]sizeInitial size in bytes of the block.

◆ ByteBlock() [2/5]

ts::ByteBlock::ByteBlock ( size_type  size,
uint8_t  value 
)

Constructor, initialized with bytes of specified value.

Parameters
[in]sizeInitial size in bytes of the block.
[in]valueValue of each byte.

◆ ByteBlock() [3/5]

ts::ByteBlock::ByteBlock ( const void *  data,
size_type  size 
)

Constructor from a data block.

Parameters
[in]dataAddress of area to copy.
[in]sizeInitial size of the block.

◆ ByteBlock() [4/5]

ts::ByteBlock::ByteBlock ( const char *  str)

Constructor from a C string.

Parameters
[in]strAddress of a nul-terminated string. The content of the byte block is the content of the string, excluding the terminating nul character.

◆ ByteBlock() [5/5]

ts::ByteBlock::ByteBlock ( std::initializer_list< uint8_t >  init)

Constructor from an initializer list.

Parameters
[in]initInitializer list.

Member Function Documentation

◆ find()

size_type ts::ByteBlock::find ( uint8_t  value,
size_type  start = 0 
)

Find the first occurence of a byte value in a byte block.

Parameters
[in]valueThe byte value to search.
[in]startIndex where to start (at the beginning by default).
Returns
The index of the first occurence of value in the byte block or NPOS if not found.

◆ copy()

void ts::ByteBlock::copy ( const void *  data,
size_type  size 
)

Replace the content of a byte block.

Parameters
[in]dataAddress of the new area to copy.
[in]sizeSize of the area to copy.

◆ erase()

void ts::ByteBlock::erase ( size_type  first,
size_type  size 
)

Remove 'size' elements at index 'first'.

The STL equivalent uses iterators, not indices.

Parameters
[in]firstIndex of the first byte to erase.
[in]sizeNumber of bytes to erase.

◆ enlarge()

uint8_t * ts::ByteBlock::enlarge ( size_type  n)

Increase size return pointer to new area at end of block.

Parameters
[in]nNumber of bytes to add at the end of the block.
Returns
Address of the new n-byte area at the end of the block.

◆ append() [1/6]

void ts::ByteBlock::append ( const void *  data,
size_type  size 
)
inline

Append raw data to a byte block.

Parameters
[in]dataAddress of the new area to append.
[in]sizeSize of the area to append.

◆ append() [2/6]

void ts::ByteBlock::append ( const ByteBlock bb)
inline

Append a byte block to a byte block.

Parameters
[in]bbByte block to append.

◆ append() [3/6]

void ts::ByteBlock::append ( const std::string &  s)
inline

Append a string to a byte block.

Parameters
[in]sString to append.

◆ appendUTF8()

void ts::ByteBlock::appendUTF8 ( const UString s)

Append a unicode string in UTF-8 representation to a byte block.

Parameters
[in]sString to append.

◆ appendUTF8WithByteLength()

void ts::ByteBlock::appendUTF8WithByteLength ( const UString s)

Append a unicode string in UTF-8 representation to a byte block with one-byte preceding length.

Parameters
[in]sString to append. UTF-8 representation is trunctated to 255 if necessary so that the length fits in one byte.

◆ append() [4/6]

void ts::ByteBlock::append ( uint8_t  i,
size_type  size 
)
inline

Append n bytes with value i.

Parameters
[in]iValue of each byte.
[in]sizeNumber of bytes to append.

◆ appendUInt8()

void ts::ByteBlock::appendUInt8 ( uint8_t  i)
inline

Append an integer in big endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendUInt16()

void ts::ByteBlock::appendUInt16 ( uint16_t  i)
inline

Append an integer in big endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendUInt24()

void ts::ByteBlock::appendUInt24 ( uint32_t  i)
inline

Append an integer in big endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendUInt32()

void ts::ByteBlock::appendUInt32 ( uint32_t  i)
inline

Append an integer in big endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendUInt40()

void ts::ByteBlock::appendUInt40 ( uint64_t  i)
inline

Append an integer in big endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendUInt48()

void ts::ByteBlock::appendUInt48 ( uint64_t  i)
inline

Append an integer in big endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendUInt64()

void ts::ByteBlock::appendUInt64 ( uint64_t  i)
inline

Append an integer in big endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendInt8()

void ts::ByteBlock::appendInt8 ( int8_t  i)
inline

Append an integer in big endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendInt16()

void ts::ByteBlock::appendInt16 ( int16_t  i)
inline

Append an integer in big endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendInt24()

void ts::ByteBlock::appendInt24 ( int32_t  i)
inline

Append an integer in big endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendInt32()

void ts::ByteBlock::appendInt32 ( int32_t  i)
inline

Append an integer in big endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendInt64()

void ts::ByteBlock::appendInt64 ( int64_t  i)
inline

Append an integer in big endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendUInt16BE()

void ts::ByteBlock::appendUInt16BE ( uint16_t  i)
inline

Append an integer in big endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendUInt24BE()

void ts::ByteBlock::appendUInt24BE ( uint32_t  i)
inline

Append an integer in big endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendUInt32BE()

void ts::ByteBlock::appendUInt32BE ( uint32_t  i)
inline

Append an integer in big endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendUInt40BE()

void ts::ByteBlock::appendUInt40BE ( uint64_t  i)
inline

Append an integer in big endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendUInt48BE()

void ts::ByteBlock::appendUInt48BE ( uint64_t  i)
inline

Append an integer in big endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendUInt64BE()

void ts::ByteBlock::appendUInt64BE ( uint64_t  i)
inline

Append an integer in big endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendInt16BE()

void ts::ByteBlock::appendInt16BE ( int16_t  i)
inline

Append an integer in big endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendInt24BE()

void ts::ByteBlock::appendInt24BE ( int32_t  i)
inline

Append an integer in big endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendInt32BE()

void ts::ByteBlock::appendInt32BE ( int32_t  i)
inline

Append an integer in big endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendInt64BE()

void ts::ByteBlock::appendInt64BE ( int64_t  i)
inline

Append an integer in big endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendUInt16LE()

void ts::ByteBlock::appendUInt16LE ( uint16_t  i)
inline

Append an integer in little endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendUInt24LE()

void ts::ByteBlock::appendUInt24LE ( uint32_t  i)
inline

Append an integer in little endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendUInt32LE()

void ts::ByteBlock::appendUInt32LE ( uint32_t  i)
inline

Append an integer in little endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendUInt40LE()

void ts::ByteBlock::appendUInt40LE ( uint64_t  i)
inline

Append an integer in little endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendUInt48LE()

void ts::ByteBlock::appendUInt48LE ( uint64_t  i)
inline

Append an integer in little endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendUInt64LE()

void ts::ByteBlock::appendUInt64LE ( uint64_t  i)
inline

Append an integer in little endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendInt16LE()

void ts::ByteBlock::appendInt16LE ( int16_t  i)
inline

Append an integer in little endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendInt24LE()

void ts::ByteBlock::appendInt24LE ( int32_t  i)
inline

Append an integer in little endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendInt32LE()

void ts::ByteBlock::appendInt32LE ( int32_t  i)
inline

Append an integer in little endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendInt64LE()

void ts::ByteBlock::appendInt64LE ( int64_t  i)
inline

Append an integer in little endian representation at the end.

Parameters
[in]iInteger value to serialize at the end of the block.

◆ append() [5/6]

template<typename INT , typename std::enable_if< std::is_integral< INT >::value >::type * = nullptr>
void ts::ByteBlock::append ( INT  i)
inline

Append an integer in big endian representation at the end.

Template variant.

Template Parameters
INTInteger type to serialize.
Parameters
[in]iInteger value to serialize at the end of the block.

◆ appendBCD()

void ts::ByteBlock::appendBCD ( uint32_t  value,
size_t  bcd_count,
bool  left_justified = true,
uint8_t  pad_nibble = 0 
)

Append an integer in Binary Coded Decimal (BCD) representation at the end.

Parameters
[in]valueInteger value to serialize at the end of the block.
[in]bcd_countNumber of BCD digits. Note that bcd_count can be even.
[in]left_justifiedWhen true (the default), the first BCD digit starts in the first half of the first byte. When false and bcd_count is odd, the first BCD digit starts in the second half of the first byte. This parameter is ignored when bcd_count is even.
[in]pad_nibbleA value in the range 0..15 to set in the unused nibble when bcd_count is odd. This is the first half of the first byte when left_justified is false. This is the second half of the last byte when left_justified is true. This parameter is ignored when bcd_count is even.

◆ loadFromFile()

bool ts::ByteBlock::loadFromFile ( const UString fileName,
size_t  maxSize = std::numeric_limits< size_t >::max(),
Report report = nullptr 
)

Read a byte block from a binary file.

Parameters
[in]fileNameInput file name.
[in]maxSizeMaximum size to read. By default, read up to end of file or error.
[in,out]reportIf not null, where to report errors.
Returns
True on success, false on error.

◆ appendFromFile()

bool ts::ByteBlock::appendFromFile ( const UString fileName,
size_t  maxSize = std::numeric_limits< size_t >::max(),
Report report = nullptr 
)

Read a byte block from a binary file and append to existing content.

Parameters
[in]fileNameInput file name.
[in]maxSizeMaximum size to read. By default, read up to end of file or error.
[in,out]reportIf not null, where to report errors.
Returns
True on success, false on error.

◆ saveToFile()

bool ts::ByteBlock::saveToFile ( const UString fileName,
Report report = nullptr 
) const

Save a byte block to a binary file.

Parameters
[in]fileNameOutput file name.
[in,out]reportIf not null, where to report errors.
Returns
True on success, false on error.

◆ appendToFile()

bool ts::ByteBlock::appendToFile ( const UString fileName,
Report report = nullptr 
) const

Save a byte block to a binary file, append to existing file content.

Parameters
[in]fileNameOutput file name.
[in,out]reportIf not null, where to report errors.
Returns
True on success, false on error.

◆ read()

std::istream & ts::ByteBlock::read ( std::istream &  strm,
size_t  maxSize = std::numeric_limits< size_t >::max() 
)

Read a byte block from standard streams (binary mode).

Parameters
[in,out]strmA standard stream in input mode.
[in]maxSizeMaximum size to read. By default, read up to end of file or error.
Returns
A reference to the strm object.

◆ append() [6/6]

std::istream & ts::ByteBlock::append ( std::istream &  strm,
size_t  maxSize = std::numeric_limits< size_t >::max() 
)

Read a byte block from standard streams and append to existing content (binary mode).

Parameters
[in,out]strmA standard stream in input mode.
[in]maxSizeMaximum size to read. By default, read up to end of file or error.
Returns
A reference to the strm object.

◆ write()

std::ostream & ts::ByteBlock::write ( std::ostream &  strm) const

Write a byte to standard streams (binary mode).

Parameters
[in,out]strmA standard stream in output mode.
Returns
A reference to the strm object.

The documentation for this class was generated from the following file: