xtd 0.2.0
Loading...
Searching...
No Matches
xtd::io::binary_reader Class Reference
Inheritance diagram for xtd::io::binary_reader:
xtd::object

Definition

Reads primitive data types as binary values in a specific encoding.

Reads primitive data types as binary values in a specific encoding.
Definition binary_reader.h:40
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.h:42
#define core_export_
Define shared library export.
Definition core_export.h:13
Inheritance
xtd::objectxtd::io::binary_reader
Header
#include <xtd/io/binary_reader>
Namespace
xtd::io
Library
xtd.core
Examples
The following code example demonstrates how to store and retrieve application settings in a file.
#include <xtd/io/binary_reader>
#include <xtd/io/binary_writer>
#include <xtd/io/file>
#include <xtd/block_scope>
#include <xtd/console>
#include <xtd/startup>
using namespace xtd;
using namespace xtd::io;
class program {
inline static const string file_name = "app_settings.dat";
public:
static auto main() {
write_default_values();
display_values();
}
static void write_default_values() {
block_scope_(auto fs = file::open(file_name, std::ios::out | std::ios::binary | std::ios::trunc)) {
auto writer = binary_writer {fs};
writer.write(1.250F);
writer.write(R"(c:\Temp)");
writer.write(10);
writer.write(true);
}
}
static void display_values() {
auto aspect_ratio = .0f;
auto temp_directory = string::empty_string;
auto auto_save_time = 0;
auto show_status_bar = false;
if (file::exists(file_name)) {
block_scope_(auto fs = file::open(file_name, std::ios::in | std::ios::binary)) {
auto reader = binary_reader {fs};
aspect_ratio = reader.read_single();
temp_directory = reader.read_string();
auto_save_time = reader.read_int32();
show_status_bar = reader.read_boolean();
}
console::write_line("Aspect ratio set to: {}", aspect_ratio);
console::write_line("Temp directory is: {}", temp_directory);
console::write_line("Auto save time set to: {}", auto_save_time);
console::write_line("Show status bar: {}", show_status_bar);
}
}
};
startup_(program::main);
// This code produces the following output :
//
// Aspect ratio set to: 1.25
// Temp directory is: c:\Temp
// Auto save time set to: 10
// Show status bar: true
virtual float read_single()
Reads an 4-byte floating point value from the current stream and advances the current position of the...
Writes primitive types in binary to a stream and supports writing strings.
Definition binary_writer.h:42
virtual void write(bool value)
Writes a one-byte boolean value to the current stream, with 0 representing false and 1 representing t...
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.h:175
#define block_scope_(...)
The specified expression is cleared automatically when the scope is ended.
Definition block_scope.h:25
The xtd::io namespace contains types that allow reading and writing to files and data streams,...
Definition binary_reader.h:16
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Examples
The same example with stream operators.
#include <xtd/io/binary_reader>
#include <xtd/io/binary_writer>
#include <xtd/io/file>
#include <xtd/block_scope>
#include <xtd/console>
#include <xtd/startup>
using namespace xtd;
using namespace xtd::io;
class program {
inline static const string file_name = "app_settings.dat";
public:
static auto main() {
write_default_values();
display_values();
}
static void write_default_values() {
block_scope_(auto fs = file::open(file_name, std::ios::out | std::ios::binary | std::ios::trunc)) {
auto writer = binary_writer {fs};
writer << 1.250F;
writer << R"(c:\Temp)";
writer << 10;
writer << true;
}
}
static void display_values() {
auto aspect_ratio = .0f;
auto temp_directory = string::empty_string;
auto auto_save_time = 0;
auto show_status_bar = false;
if (file::exists(file_name)) {
block_scope_(auto fs = file::open(file_name, std::ios::in | std::ios::binary)) {
auto reader = binary_reader {fs};
reader >> aspect_ratio;
reader >> temp_directory;
reader >> auto_save_time;
reader >> show_status_bar;
}
console::write_line("Aspect ratio set to: {}", aspect_ratio);
console::write_line("Temp directory is: {}", temp_directory);
console::write_line("Auto save time set to: {}", auto_save_time);
console::write_line("Show status bar: {}", show_status_bar);
}
}
};
startup_(program::main);
// This code produces the following output :
//
// Aspect ratio set to: 1.25
// Temp directory is: c:\Temp
// Auto save time set to: 10
// Show status bar: true
Remarks
The xtd:io::binary_reader class provides methods that simplify reading primitive data types from a stream. For example, you can use the xtd:io::binary_reader::read_boolean method to read the next byte as a bool value and advance the current position in the stream by one byte. The class includes read methods that support different data types.
When you create a new instance of the xtd:io::binary_reader and whether to leave the stream open after disposing the xtd:io::binary_reader object.
Examples
binary_reader.cpp, binary_reader2.cpp, and binary_reader3.cpp.

Public Constructors

 binary_reader (const xtd::string &path)
 Initializes a new instance of the xtd::io::binary_reader class for the specified file name.
 
 binary_reader (std::istream &stream)
 Initializes a new instance of the xtd::io::binary_reader class for the specified stream.
 

Public Properties

std::optional< std::reference_wrapper< std::istream > > base_stream () const
 Returns the underlying stream.
 
bool end_of_stream () const
 Gets a value that indicates whether the current stream position is at the end of the stream.
 

Public Methods

void close ()
 Closes the xtd::io::binary_reader object and the underlying stream, and releases any system resources associated with the reader.
 
int32 peek_char () const
 Returns the next available character and does not advance the byte or character position.
 
std::streampos pop ()
 Pop the current top position.
 
void push (std::streampos pos=0)
 Push the current position.
 
virtual int32 read ()
 Reads characters from the underlying stream and advances the current position of the stream in accordance with the Encoding used and the specific character being read from the stream.
 
virtual size_t read (std::vector< xtd::byte > &buffer, size_t index, size_t count)
 Reads the specified number of bytes from the stream, starting from a specified point in the byte array.
 
virtual size_t read (std::vector< char > &buffer, size_t index, size_t count)
 Reads the specified number of characters from the stream, starting from a specified point in the character array.
 
virtual bool read_boolean ()
 Reads a boolean value from the current stream and advances the current position of the stream by one byte.
 
virtual xtd::byte read_byte ()
 Reads the next byte from the current stream and advances the current position of the stream by one byte.
 
virtual std::vector< xtd::byteread_bytes (size_t count)
 Reads the specified number of bytes from the current stream into a byte array and advances the current position by that number of bytes.
 
virtual char read_char ()
 Reads the next character from the current stream and advances the current position of the stream by one byte.
 
virtual std::vector< char > read_chars (size_t count)
 Reads the specified number of characters from the current stream into a byte array and advances the current position by that number of bytes.
 
virtual double read_double ()
 Reads an 8-byte floating point value from the current stream and advances the current position of the stream by eight bytes.
 
virtual int16 read_int16 ()
 Reads a 2-byte signed integer from the current stream and advances the current position of the stream by two bytes.
 
virtual int32 read_int32 ()
 Reads a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes.
 
virtual int64 read_int64 ()
 Reads a 8-byte signed integer from the current stream and advances the current position of the stream by eight bytes.
 
virtual sbyte read_sbyte ()
 Reads the a signed byte from the current stream and advances the current position of the stream by one byte.
 
virtual float read_single ()
 Reads an 4-byte floating point value from the current stream and advances the current position of the stream by four bytes.
 
virtual string read_string ()
 Reads a string from the current stream. The string is prefixed with the length.
 
virtual uint16 read_uint16 ()
 Reads a 2-byte unsigned integer from the current stream and advances the current position of the stream by two bytes.
 
virtual uint32 read_uint32 ()
 Reads a 4-byte unsigned integer from the current stream and advances the current position of the stream by four bytes.
 
virtual uint64 read_uint64 ()
 Reads a 8-byte unsigned integer from the current stream and advances the current position of the stream by eight bytes.
 
void rewind ()
 Rewind stream.
 
void seekg (std::streamoff off, std::ios_base::seekdir dir=std::ios_base::cur)
 Change the position of the readers stream.
 
std::streampos tellg ()
 Tell the current seek position of the readers stream;.
 

Additional Inherited Members

- Public Member Functions inherited from xtd::object
 object ()=default
 Create a new instance of the ultimate base class object.
 
virtual bool equals (const object &obj) const noexcept
 Determines whether the specified object is equal to the current object.
 
virtual size_t get_hash_code () const noexcept
 Serves as a hash function for a particular type.
 
virtual type_object get_type () const noexcept
 Gets the type of the current instance.
 
template<typename object_t >
xtd::uptr< object_t > memberwise_clone () const
 Creates a shallow copy of the current object.
 
virtual xtd::string to_string () const noexcept
 Returns a xtd::string that represents the current object.
 
- Static Public Member Functions inherited from xtd::object
template<typename object_a_t , typename object_b_t >
static bool equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are considered equal.
 
template<typename object_a_t , typename object_b_t >
static bool reference_equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are the same instance.
 

Constructor & Destructor Documentation

◆ binary_reader() [1/2]

xtd::io::binary_reader::binary_reader ( const xtd::string path)
explicit

Initializes a new instance of the xtd::io::binary_reader class for the specified file name.

Parameters
pathThe complete file path to be read.
Exceptions
xtd::argument_exceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by xtd::io::path::invalid_path_chars.
Remarks
For a list of common I/O tasks, see Common I/O Tasks.

◆ binary_reader() [2/2]

xtd::io::binary_reader::binary_reader ( std::istream &  stream)
explicit

Initializes a new instance of the xtd::io::binary_reader class for the specified stream.

Parameters
streamThe stream to be read.
Examples
The following code example demonstrates how to store and retrieve application settings in a file.
#include <xtd/io/binary_reader>
#include <xtd/io/binary_writer>
#include <xtd/io/file>
#include <xtd/block_scope>
#include <xtd/console>
#include <xtd/startup>
using namespace xtd;
using namespace xtd::io;
class program {
inline static const string file_name = "app_settings.dat";
public:
static auto main() {
write_default_values();
display_values();
}
static void write_default_values() {
block_scope_(auto fs = file::open(file_name, std::ios::out | std::ios::binary | std::ios::trunc)) {
auto writer = binary_writer {fs};
writer.write(1.250F);
writer.write(R"(c:\Temp)");
writer.write(10);
writer.write(true);
}
}
static void display_values() {
auto aspect_ratio = .0f;
auto temp_directory = string::empty_string;
auto auto_save_time = 0;
auto show_status_bar = false;
if (file::exists(file_name)) {
block_scope_(auto fs = file::open(file_name, std::ios::in | std::ios::binary)) {
auto reader = binary_reader {fs};
aspect_ratio = reader.read_single();
temp_directory = reader.read_string();
auto_save_time = reader.read_int32();
show_status_bar = reader.read_boolean();
}
console::write_line("Aspect ratio set to: {}", aspect_ratio);
console::write_line("Temp directory is: {}", temp_directory);
console::write_line("Auto save time set to: {}", auto_save_time);
console::write_line("Show status bar: {}", show_status_bar);
}
}
};
startup_(program::main);
// This code produces the following output :
//
// Aspect ratio set to: 1.25
// Temp directory is: c:\Temp
// Auto save time set to: 10
// Show status bar: true
Remarks
For a list of common I/O tasks, see Common I/O Tasks.

Member Function Documentation

◆ base_stream()

std::optional< std::reference_wrapper< std::istream > > xtd::io::binary_reader::base_stream ( ) const

Returns the underlying stream.

Returns
The underlying stream.
Warning
Using the underlying stream while reading or while using the xtd::io::binary_reader can cause data loss and corruption. For example, the same bytes might be read more than once, bytes might be skipped, or character reading might become unpredictable.
Remarks
For a list of common I/O tasks, see Common I/O Tasks.

◆ end_of_stream()

bool xtd::io::binary_reader::end_of_stream ( ) const

Gets a value that indicates whether the current stream position is at the end of the stream.

Returns
true if the current stream position is at the end of the stream; otherwise false.

◆ close()

void xtd::io::binary_reader::close ( )

Closes the xtd::io::binary_reader object and the underlying stream, and releases any system resources associated with the reader.

◆ peek_char()

int32 xtd::io::binary_reader::peek_char ( ) const

Returns the next available character and does not advance the byte or character position.

Returns
The next available character, or EOF if no more characters are available or the stream does not support seeking.
Exceptions
xtd::io::io_exceptionAn I/O error occurred.
Remarks
For a list of common I/O tasks, see Common I/O Tasks.

◆ pop()

std::streampos xtd::io::binary_reader::pop ( )

Pop the current top position.

Examples
binary_reader3.cpp.

◆ push()

void xtd::io::binary_reader::push ( std::streampos  pos = 0)

Push the current position.

Parameters
posThe stream position to be push/saved relative to ios_base::beg direction
Examples
binary_reader3.cpp.

◆ read() [1/3]

virtual int32 xtd::io::binary_reader::read ( )
virtual

Reads characters from the underlying stream and advances the current position of the stream in accordance with the Encoding used and the specific character being read from the stream.

Returns
The next character from the input stream, or EOF if no characters are currently available.
Exceptions
xtd::io::io_exceptionAn I/O error occurred.
Remarks
xtd::io::binary_reader does not restore the file position after an unsuccessful read.
For a list of common I/O tasks, see Common I/O Tasks.

◆ read() [2/3]

virtual size_t xtd::io::binary_reader::read ( std::vector< xtd::byte > &  buffer,
size_t  index,
size_t  count 
)
virtual

Reads the specified number of bytes from the stream, starting from a specified point in the byte array.

Parameters
bufferThe buffer to read data into.
indexThe starting point in the buffer at which to begin reading into the buffer.
countThe number of bytes to read.
Returns
The number of bytes read into buffer. This might be less than the number of bytes requested if that many bytes are not available, or it might be zero if the end of the stream is reached.
Exceptions
xtd::argument_exceptionThe buffer length minus index is less than count.
xtd::io_io_exceptionAn I/O error occurred.
Remarks
xtd::io::binary_reader does not restore the file position after an unsuccessful read operation.
For a list of common I/O tasks, see Common I/O Tasks.

◆ read() [3/3]

virtual size_t xtd::io::binary_reader::read ( std::vector< char > &  buffer,
size_t  index,
size_t  count 
)
virtual

Reads the specified number of characters from the stream, starting from a specified point in the character array.

Parameters
bufferThe buffer to read data into.
indexThe starting point in the buffer at which to begin reading into the buffer.
countThe number of characters to read.
Exceptions
xtd::argument_exceptionThe buffer length minus index is less than count.
xtd::io_io_exceptionAn I/O error occurred.
Remarks
xtd::io::binary_reader does not restore the file position after an unsuccessful read operation.
For a list of common I/O tasks, see Common I/O Tasks.

◆ read_boolean()

virtual bool xtd::io::binary_reader::read_boolean ( )
virtual

Reads a boolean value from the current stream and advances the current position of the stream by one byte.

Returns
true if the byte is nonzero; otherwise, false.
Exceptions
EndOfStreamExceptionThe end of the stream is reached.
xtd::io::io_exceptionAn I/O error occurred.
Remarks
xtd::io::binary_reader does not restore the file position after an unsuccessful read operation.
For a list of common I/O tasks, see Common I/O Tasks.

◆ read_byte()

virtual xtd::byte xtd::io::binary_reader::read_byte ( )
virtual

Reads the next byte from the current stream and advances the current position of the stream by one byte.

Returns
The next byte read from the current stream.
Exceptions
EndOfStreamExceptionThe end of the stream is reached.
xtd::io::io_exceptionAn I/O error occurred.
Remarks
xtd::io::binary_reader does not restore the file position after an unsuccessful read operation.
For a list of common I/O tasks, see Common I/O Tasks.

◆ read_bytes()

virtual std::vector< xtd::byte > xtd::io::binary_reader::read_bytes ( size_t  count)
virtual

Reads the specified number of bytes from the current stream into a byte array and advances the current position by that number of bytes.

Parameters
countThe number of bytes to read. This value must be 0 or a non-negative number or an exception will occur.
Returns
A byte array containing data read from the underlying stream. This might be less than the number of bytes requested if the end of the stream is reached.
Exceptions
EndOfStreamExceptionThe end of the stream is reached.
xtd::io::io_exceptionAn I/O error occurred.
Remarks
xtd::io::binary_reader does not restore the file position after an unsuccessful read operation.
For a list of common I/O tasks, see Common I/O Tasks.

◆ read_char()

virtual char xtd::io::binary_reader::read_char ( )
virtual

Reads the next character from the current stream and advances the current position of the stream by one byte.

Returns
A character read from the current stream.
Exceptions
xtd::io::end_of_stream_exceptionThe end of the stream is reached.
xtd::io::io_exceptionAn I/O error occurred.
Remarks
xtd::io::binary_reader does not restore the file position after an unsuccessful read operation.
For a list of common I/O tasks, see Common I/O Tasks.

◆ read_chars()

virtual std::vector< char > xtd::io::binary_reader::read_chars ( size_t  count)
virtual

Reads the specified number of characters from the current stream into a byte array and advances the current position by that number of bytes.

Parameters
countThe number of characters to read. This value must be 0 or a non-negative number or an exception will occur.
Returns
A character array containing data read from the underlying stream. This might be less than the number of bytes requested if the end of the stream is reached.
Exceptions
xtd::io::end_of_stream_exceptionThe end of the stream is reached.
xtd::io::io_exceptionAn I/O error occurred.
Remarks
xtd::io::binary_reader does not restore the file position after an unsuccessful read operation.
For a list of common I/O tasks, see Common I/O Tasks.

◆ read_double()

virtual double xtd::io::binary_reader::read_double ( )
virtual

Reads an 8-byte floating point value from the current stream and advances the current position of the stream by eight bytes.

Returns
An 8-byte floating point value read from the current stream.
Exceptions
xtd::io::end_of_stream_exceptionThe end of the stream is reached.
xtd::io::io_exceptionAn I/O error occurred.
Remarks
xtd::io::binary_reader does not restore the file position after an unsuccessful read operation.
For a list of common I/O tasks, see Common I/O Tasks.

◆ read_int16()

virtual int16 xtd::io::binary_reader::read_int16 ( )
virtual

Reads a 2-byte signed integer from the current stream and advances the current position of the stream by two bytes.

Returns
A 2-byte signed integer read from the current stream.
Exceptions
xtd::io::end_of_stream_exceptionThe end of the stream is reached.
xtd::io::io_exceptionAn I/O error occurred.
Remarks
xtd::io::binary_reader does not restore the file position after an unsuccessful read operation.
For a list of common I/O tasks, see Common I/O Tasks.

◆ read_int32()

virtual int32 xtd::io::binary_reader::read_int32 ( )
virtual

Reads a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes.

Returns
A 4-byte signed integer read from the current stream.
Exceptions
xtd::io::end_of_stream_exceptionThe end of the stream is reached.
xtd::io::io_exceptionAn I/O error occurred.
Remarks
xtd::io::binary_reader does not restore the file position after an unsuccessful read operation.
For a list of common I/O tasks, see Common I/O Tasks.

◆ read_int64()

virtual int64 xtd::io::binary_reader::read_int64 ( )
virtual

Reads a 8-byte signed integer from the current stream and advances the current position of the stream by eight bytes.

Returns
A 8-byte signed integer read from the current stream.
Exceptions
xtd::io::end_of_stream_exceptionThe end of the stream is reached.
xtd::io::io_exceptionAn I/O error occurred.
Remarks
xtd::io::binary_reader does not restore the file position after an unsuccessful read operation.
For a list of common I/O tasks, see Common I/O Tasks.

◆ read_sbyte()

virtual sbyte xtd::io::binary_reader::read_sbyte ( )
virtual

Reads the a signed byte from the current stream and advances the current position of the stream by one byte.

Returns
A signed byte read from the current stream.
Exceptions
EndOfStreamExceptionThe end of the stream is reached.
xtd::io::io_exceptionAn I/O error occurred.
Remarks
xtd::io::binary_reader does not restore the file position after an unsuccessful read operation.
For a list of common I/O tasks, see Common I/O Tasks.

◆ read_single()

virtual float xtd::io::binary_reader::read_single ( )
virtual

Reads an 4-byte floating point value from the current stream and advances the current position of the stream by four bytes.

Returns
An 4-byte floating point value read from the current stream.
Exceptions
xtd::io::end_of_stream_exceptionThe end of the stream is reached.
xtd::io::io_exceptionAn I/O error occurred.
Remarks
xtd::io::binary_reader does not restore the file position after an unsuccessful read operation.
For a list of common I/O tasks, see Common I/O Tasks.
Examples
binary_reader.cpp.

◆ read_string()

virtual string xtd::io::binary_reader::read_string ( )
virtual

Reads a string from the current stream. The string is prefixed with the length.

Returns
The string being read.
Exceptions
xtd::io::end_of_stream_exceptionThe end of the stream is reached.
xtd::io::io_exceptionAn I/O error occurred.
Remarks
xtd::io::binary_reader does not restore the file position after an unsuccessful read operation.
For a list of common I/O tasks, see Common I/O Tasks.

◆ read_uint16()

virtual uint16 xtd::io::binary_reader::read_uint16 ( )
virtual

Reads a 2-byte unsigned integer from the current stream and advances the current position of the stream by two bytes.

Returns
A 2-byte unsigned integer read from the current stream.
Exceptions
xtd::io::end_of_stream_exceptionThe end of the stream is reached.
xtd::io::io_exceptionAn I/O error occurred.
Remarks
xtd::io::binary_reader does not restore the file position after an unsuccessful read operation.
For a list of common I/O tasks, see Common I/O Tasks.

◆ read_uint32()

virtual uint32 xtd::io::binary_reader::read_uint32 ( )
virtual

Reads a 4-byte unsigned integer from the current stream and advances the current position of the stream by four bytes.

Returns
A 4-byte unsigned integer read from the current stream.
Exceptions
xtd::io::end_of_stream_exceptionThe end of the stream is reached.
xtd::io::io_exceptionAn I/O error occurred.
Remarks
xtd::io::binary_reader does not restore the file position after an unsuccessful read operation.
For a list of common I/O tasks, see Common I/O Tasks.

◆ read_uint64()

virtual uint64 xtd::io::binary_reader::read_uint64 ( )
virtual

Reads a 8-byte unsigned integer from the current stream and advances the current position of the stream by eight bytes.

Returns
A 8-byte unsigned integer read from the current stream.
Exceptions
xtd::io::end_of_stream_exceptionThe end of the stream is reached.
xtd::io::io_exceptionAn I/O error occurred.
Remarks
xtd::io::binary_reader does not restore the file position after an unsuccessful read operation.
For a list of common I/O tasks, see Common I/O Tasks.

◆ rewind()

void xtd::io::binary_reader::rewind ( )

Rewind stream.

Examples
binary_reader3.cpp.

◆ seekg()

void xtd::io::binary_reader::seekg ( std::streamoff  off,
std::ios_base::seekdir  dir = std::ios_base::cur 
)

Change the position of the readers stream.

Parameters
offThe offset being added to the direction
dirThe seek direction. If ommited defaults to ios_base::cur

◆ tellg()

std::streampos xtd::io::binary_reader::tellg ( )

Tell the current seek position of the readers stream;.

Returns
The current seek position
Examples
binary_reader3.cpp.

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