class Poco::BinaryReader

Overview

This class reads basic types (and std::vectors thereof) in binary form into an input stream. Moreā€¦

#include <BinaryReader.h>

class BinaryReader
{
public:
    // enums

    enum StreamByteOrder;

    // construction

    BinaryReader(
        std::istream& istr,
        StreamByteOrder byteOrder = NATIVE_BYTE_ORDER
        );

    BinaryReader(
        std::istream& istr,
        TextEncoding& encoding,
        StreamByteOrder byteOrder = NATIVE_BYTE_ORDER
        );

    // methods

    BinaryReader&
    operator>>(bool& value);

    BinaryReader&
    operator>>(char& value);

    BinaryReader&
    operator>>(unsigned char& value);

    BinaryReader&
    operator>>(signed char& value);

    BinaryReader&
    operator>>(short& value);

    BinaryReader&
    operator>>(unsigned short& value);

    BinaryReader&
    operator>>(int& value);

    BinaryReader&
    operator>>(unsigned int& value);

    BinaryReader&
    operator>>(long& value);

    BinaryReader&
    operator>>(unsigned long& value);

    BinaryReader&
    operator>>(float& value);

    BinaryReader&
    operator>>(double& value);

    BinaryReader&
    operator>>(std::string& value);

    template <typename T>
    BinaryReader&
    operator>>(std::vector<T>& value);

    void
    read7BitEncoded(UInt32& value);

    void
    readRaw(
        std::streamsize length,
        std::string& value
        );

    void
    readRaw(
        char* buffer,
        std::streamsize length
        );

    void
    readBOM();

    bool
    good();

    bool
    fail();

    bool
    bad();

    bool
    eof();

    std::istream&
    stream() const;

    StreamByteOrder
    byteOrder() const;

    void
    setExceptions(std::ios_base::iostate st = (std::istream::failbit|std::istream::badbit));

    std::streamsize
    available() const;
};

// direct descendants

template <typename T>
class BasicMemoryBinaryReader;

Detailed Documentation

This class reads basic types (and std::vectors thereof) in binary form into an input stream.

It provides an extractor-based interface similar to istream. The reader also supports automatic conversion from big-endian (network byte order) to little-endian and vice-versa. Use a BinaryWriter to create a stream suitable for a BinaryReader.

Construction

BinaryReader(
    std::istream& istr,
    StreamByteOrder byteOrder = NATIVE_BYTE_ORDER
    )

Creates the BinaryReader.

BinaryReader(
    std::istream& istr,
    TextEncoding& encoding,
    StreamByteOrder byteOrder = NATIVE_BYTE_ORDER
    )

Creates the BinaryReader using the given TextEncoding.

Strings will be converted from the specified encoding to the currently set global encoding (see Poco::TextEncoding::global()).

Methods

void
read7BitEncoded(UInt32& value)

Reads a 32-bit unsigned integer in compressed format.

See BinaryWriter::write7BitEncoded() for a description of the compression algorithm.

void
readRaw(
    std::streamsize length,
    std::string& value
    )

Reads length bytes of raw data into value.

void
readRaw(
    char* buffer,
    std::streamsize length
    )

Reads length bytes of raw data into buffer.

void
readBOM()

Reads a byte-order mark from the stream and configures the reader for the encountered byte order.

A byte-order mark is a 16-bit integer with a value of 0xFEFF, written in host byte order.

bool
good()

Returns _istr.good();.

bool
fail()

Returns _istr.fail();.

bool
bad()

Returns _istr.bad();.

bool
eof()

Returns _istr.eof();.

std::istream&
stream() const

Returns the underlying stream.

StreamByteOrder
byteOrder() const

Returns the byte-order used by the reader, which is either BIG_ENDIAN_BYTE_ORDER or LITTLE_ENDIAN_BYTE_ORDER.

void
setExceptions(std::ios_base::iostate st = (std::istream::failbit|std::istream::badbit))

Sets the stream to throw exception on specified state (default failbit and badbit);.

std::streamsize
available() const

Returns the number of available bytes in the stream.