PByteStream Class Reference

Detailed Description

The PByteStream class is a generic byte stream (can be a memory stream or a file stream).

This class can be inherited.

Example: serialize/deserialise some data into a PByteArray

PResult Serialise(PByteStream& os, int i, const PString& s, const PDateTime& t)
{
// you could also write (without error check):
// os << i << s << t;
return PResult::C_OK;
}
PResult Deserialise(PByteStream& is, int& i, PString& s, PDateTime& t)
{
// you could also write (without error check):
// is >> i >> s >> t;
return PResult::C_OK;
}
...
PMemoryStream ms;
Serialise(ms, 4, "test", PDateTime("2014-04-05T23:58:59")).OrDie();
int i = 0;
ms.Rewind();
Deserialise(ms, i, s, t).OrDie();

Definition at line 69 of file PByteStream.h.

#include <PByteStream.h>

Constructor & Destructor Documentation

◆ PByteStream() [1/2]

PByteStream::PByteStream ( )

Construct an empty byte stream.

◆ PByteStream() [2/2]

PByteStream::PByteStream ( const PByteStream other)

Constructs a shared copy of other.

◆ ~PByteStream()

virtual PByteStream::~PByteStream ( )
virtual

Closes the stream if opened, then destroys this object.

Member Function Documentation

◆ CanRead()

bool PByteStream::CanRead ( ) const

Returns true if this stream supports reading, false otherwise.

◆ CanSeek()

bool PByteStream::CanSeek ( ) const

Returns true if this stream supports seeking, false otherwise.

◆ CanWrite()

bool PByteStream::CanWrite ( ) const

Returns true if this stream supports writing, false otherwise.

◆ Close()

PResult PByteStream::Close ( )

Closes this stream and releases any resources associated with.

Close() is automatically called when is stream is destroyed.

◆ Flush()

PResult PByteStream::Flush ( )

Clears all buffers for this stream and causes any buffered data to be written to the underlying device.

◆ GetPosition()

papillon::int64 PByteStream::GetPosition ( ) const

Returns the current position in this stream (if seeking is supported, 0 otherwise).

◆ HasSize()

bool PByteStream::HasSize ( ) const

Returns true if this stream has a predefined length, false otherwise.

◆ IsOpened()

PResult PByteStream::IsOpened ( ) const

Returns PResult::C_OK if the stream is ready opened, another result otherwise.

◆ operator<<()

template<typename T >
PByteStream& PByteStream::operator<< ( const T &  t)
inline

Serialises the specified object to this stream; throw an exception if failure.

This method works with any Papillon objects (PString, PDateTime, PProperties, etc.) and also with primitive data type (int8, int16, int32, int64, uint8, uint16, uint32, uint64, float, double).

Warning: this method logs error if any.

Definition at line 258 of file PByteStream.h.

◆ operator=()

PByteStream& PByteStream::operator= ( const PByteStream other)

Performs a shared copy of other to this object.

◆ operator==()

bool PByteStream::operator== ( const PByteStream other) const

Returns true if this object is the same instance than other, false otherwise.

◆ operator>>()

template<typename T >
PByteStream& PByteStream::operator>> ( T &  t)
inline

Deserialises the specified object from this stream.

This method works with any Papillon objects (PString, PDateTime, PProperties, etc.) and also with primitive data type (int8, int16, int32, int64, uint8, uint16, uint32, uint64, float, double).

Warning: this method logs error if any.

Definition at line 203 of file PByteStream.h.

◆ ReadByte()

PResult PByteStream::ReadByte ( papillon::int8 &  value)

Reads a byte from this stream.

Returns PResult::C_OK if success, another result otherwise (returns PResult::ErrorEndOfSteam() (error code is PResult::E_ERROR_END_OF_STREAM) if end of the stream has been reached)

◆ ReadBytes() [1/2]

PResult PByteStream::ReadBytes ( void *  destinationBuffer,
papillon::uint64  n 
)

UNSAFE: Reads up to n bytes from this stream and puts data into the specified buffer.

Returns PResult::C_OK if success, another result otherwise (returns PResult::ErrorEndOfSteam() (error code is PResult::E_ERROR_END_OF_STREAM) if end of the stream has been reached)

◆ ReadBytes() [2/2]

PResult PByteStream::ReadBytes ( PByteArray destinationArray,
papillon::uint64  position,
papillon::uint64  n 
)

Reads up to n bytes from this stream and puts data into the specified array starting at the specified position.

Returns PResult::C_OK if success, another result otherwise (returns PResult::ErrorEndOfSteam() (error code is PResult::E_ERROR_END_OF_STREAM) if end of the stream has been reached)

◆ ReadClassIdAndCheck()

static PResult PByteStream::ReadClassIdAndCheck ( PByteStream is,
classid  expectedClassId 
)
static

◆ ReadObject() [1/12]

template<typename T >
PResult PByteStream::ReadObject ( T &  t)
inline

Deserialises the specified object from this stream.

This method works with any Papillon objects (PString, PDateTime, PProperties, etc.) and also with primitive data type (int8, int16, int32, int64, uint8, uint16, uint32, uint64, float, double).

Returns PResult::C_OK if success, another result otherwise.

Definition at line 189 of file PByteStream.h.

◆ ReadObject() [2/12]

template<>
PResult PByteStream::ReadObject ( bool &  value)
inline

Definition at line 271 of file PByteStream.h.

◆ ReadObject() [3/12]

template<>
PResult PByteStream::ReadObject ( papillon::int8 &  value)
inline

Definition at line 272 of file PByteStream.h.

◆ ReadObject() [4/12]

template<>
PResult PByteStream::ReadObject ( papillon::int16 &  value)
inline

Definition at line 273 of file PByteStream.h.

◆ ReadObject() [5/12]

template<>
PResult PByteStream::ReadObject ( papillon::int32 &  value)
inline

Definition at line 274 of file PByteStream.h.

◆ ReadObject() [6/12]

template<>
PResult PByteStream::ReadObject ( papillon::int64 &  value)
inline

Definition at line 275 of file PByteStream.h.

◆ ReadObject() [7/12]

template<>
PResult PByteStream::ReadObject ( papillon::uint8 &  value)
inline

Definition at line 276 of file PByteStream.h.

◆ ReadObject() [8/12]

template<>
PResult PByteStream::ReadObject ( papillon::uint16 &  value)
inline

Definition at line 277 of file PByteStream.h.

◆ ReadObject() [9/12]

template<>
PResult PByteStream::ReadObject ( papillon::uint32 &  value)
inline

Definition at line 278 of file PByteStream.h.

◆ ReadObject() [10/12]

template<>
PResult PByteStream::ReadObject ( papillon::uint64 &  value)
inline

Definition at line 279 of file PByteStream.h.

◆ ReadObject() [11/12]

template<>
PResult PByteStream::ReadObject ( float &  value)
inline

Definition at line 280 of file PByteStream.h.

◆ ReadObject() [12/12]

template<>
PResult PByteStream::ReadObject ( double &  value)
inline

Definition at line 281 of file PByteStream.h.

◆ Resize()

PResult PByteStream::Resize ( papillon::uint64  newSize)

Sets the size of this stream, is possible.

Returns PResult::C_OK if success, another result otherwise.

◆ Rewind()

PResult PByteStream::Rewind ( )

Resets the position within the current stream (sets position to the first byte), if seeking is supported.

◆ Seek()

PResult PByteStream::Seek ( papillon::uint64  n)

Sets the position within the current stream if seeking is supported.

◆ Size()

papillon::int64 PByteStream::Size ( ) const

Returns the size of this stream in bytes, if available.

◆ WriteByte()

PResult PByteStream::WriteByte ( papillon::int8  value)

Writes a byte to this stream.

Returns PResult::C_OK if success, another result otherwise.

◆ WriteBytes() [1/2]

PResult PByteStream::WriteBytes ( const void *  sourceBuffer,
papillon::uint64  n 
)

Writes n bytes from the specified buffer to this stream.

Returns PResult::C_OK if success, another result otherwise.

◆ WriteBytes() [2/2]

PResult PByteStream::WriteBytes ( const PByteArray sourceArray,
papillon::uint64  sourcePosition,
papillon::uint64  numBytes = 0 
)

Writes the bytes from the specified array to this stream.

If numBytes is 0, then writes all bytes from the specified array's position to the end of the array. Returns PResult::C_OK if success, another result otherwise.

◆ WriteObject() [1/12]

template<typename T >
PResult PByteStream::WriteObject ( const T &  t)
inline

Serialises the specified object to this stream.

This method works with any Papillon objects (PString, PDateTime, PProperties, etc.) and also with primitive data type (int8, int16, int32, int64, uint8, uint16, uint32, uint64, float, double).

Returns PResult::C_OK if success, another result otherwise.

Definition at line 244 of file PByteStream.h.

◆ WriteObject() [2/12]

template<>
PResult PByteStream::WriteObject ( const bool &  value)
inline

Definition at line 283 of file PByteStream.h.

◆ WriteObject() [3/12]

template<>
PResult PByteStream::WriteObject ( const papillon::int8 &  value)
inline

Definition at line 284 of file PByteStream.h.

◆ WriteObject() [4/12]

template<>
PResult PByteStream::WriteObject ( const papillon::int16 &  value)
inline

Definition at line 285 of file PByteStream.h.

◆ WriteObject() [5/12]

template<>
PResult PByteStream::WriteObject ( const papillon::int32 &  value)
inline

Definition at line 286 of file PByteStream.h.

◆ WriteObject() [6/12]

template<>
PResult PByteStream::WriteObject ( const papillon::int64 &  value)
inline

Definition at line 287 of file PByteStream.h.

◆ WriteObject() [7/12]

template<>
PResult PByteStream::WriteObject ( const papillon::uint8 &  value)
inline

Definition at line 288 of file PByteStream.h.

◆ WriteObject() [8/12]

template<>
PResult PByteStream::WriteObject ( const papillon::uint16 &  value)
inline

Definition at line 289 of file PByteStream.h.

◆ WriteObject() [9/12]

template<>
PResult PByteStream::WriteObject ( const papillon::uint32 &  value)
inline

Definition at line 290 of file PByteStream.h.

◆ WriteObject() [10/12]

template<>
PResult PByteStream::WriteObject ( const papillon::uint64 &  value)
inline

Definition at line 291 of file PByteStream.h.

◆ WriteObject() [11/12]

template<>
PResult PByteStream::WriteObject ( const float &  value)
inline

Definition at line 292 of file PByteStream.h.

◆ WriteObject() [12/12]

template<>
PResult PByteStream::WriteObject ( const double &  value)
inline

Definition at line 293 of file PByteStream.h.