xtd - Reference Guide  0.2.0
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
List of all members
xtd::io::binary_reader Class Reference

#include <binary_reader.h>

Definition

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

class core_export_ binary_reader : public xtd::object
Inheritance
xtd::objectxtd::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/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::io;
class program {
inline static const ustring file_name = "app_settings.dat";
public:
static void main() {
write_default_values();
display_values();
}
static void write_default_values() {
using_(fstream fs = file::open(file_name, ios::out | ios::binary | ios::trunc)) {
binary_writer writer(fs);
writer.write(1.250F);
writer.write(R"(c:\Temp)");
writer.write(10);
writer.write(true);
}
}
static void display_values() {
float aspect_ratio;
ustring temp_directory;
int auto_save_time;
bool show_status_bar;
if (file::exists(file_name)) {
using_(fstream fs = file::open(file_name, ios::in | ios::binary)) {
binary_reader 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);
// 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
Examples
The same example with stream operators.
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::io;
class program {
inline static const ustring file_name = "app_settings.dat";
public:
static void main() {
write_default_values();
display_values();
}
static void write_default_values() {
using_(fstream fs = file::open(file_name, ios::out | ios::binary | ios::trunc)) {
binary_writer writer(fs);
writer << 1.250F;
writer << R"(c:\Temp)";
writer << 10;
writer << true;
}
}
static void display_values() {
float aspect_ratio;
ustring temp_directory;
int auto_save_time;
bool show_status_bar;
if (file::exists(file_name)) {
using_(fstream fs = file::open(file_name, ios::in | ios::binary)) {
binary_reader 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);
// 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.

Constructors

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

Properties

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

Methods

void close ()
 Closes the xtd::io::binary_reader object and the underlying stream, and releases any system resources associated with the reader. More...
 
int32_t peek_char () const
 Returns the next available character and does not advance the byte or character position. More...
 
virtual int32_t 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. More...
 
virtual size_t read (std::vector< byte_t > &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. More...
 
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. More...
 
virtual bool read_boolean ()
 Reads a boolean value from the current stream and advances the current position of the stream by one byte. More...
 
virtual byte_t read_byte ()
 Reads the next byte from the current stream and advances the current position of the stream by one byte. More...
 
virtual std::vector< byte_t > read_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. More...
 
virtual char read_char ()
 Reads the next character from the current stream and advances the current position of the stream by one byte. More...
 
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. More...
 
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. More...
 
virtual int16_t read_int16 ()
 Reads a 2-byte signed integer from the current stream and advances the current position of the stream by two bytes. More...
 
virtual int32_t read_int32 ()
 Reads a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes. More...
 
virtual int64_t read_int64 ()
 Reads a 8-byte signed integer from the current stream and advances the current position of the stream by eight bytes. More...
 
virtual sbyte_t read_sbyte ()
 Reads the a signed byte from the current stream and advances the current position of the stream by one byte. More...
 
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. More...
 
virtual ustring read_string ()
 Reads a string from the current stream. The string is prefixed with the length. More...
 
virtual uint16_t read_uint16 ()
 Reads a 2-byte unsigned integer from the current stream and advances the current position of the stream by two bytes. More...
 
virtual uint32_t read_uint32 ()
 Reads a 4-byte unsigned integer from the current stream and advances the current position of the stream by four bytes. More...
 
virtual uint64_t read_uint64 ()
 Reads a 8-byte unsigned integer from the current stream and advances the current position of the stream by eight bytes. More...
 

Additional Inherited Members

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

Constructor & Destructor Documentation

◆ binary_reader() [1/2]

xtd::io::binary_reader::binary_reader ( const xtd::ustring 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/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::io;
class program {
inline static const ustring file_name = "app_settings.dat";
public:
static void main() {
write_default_values();
display_values();
}
static void write_default_values() {
using_(fstream fs = file::open(file_name, ios::out | ios::binary | ios::trunc)) {
binary_writer writer(fs);
writer.write(1.250F);
writer.write(R"(c:\Temp)");
writer.write(10);
writer.write(true);
}
}
static void display_values() {
float aspect_ratio;
ustring temp_directory;
int auto_save_time;
bool show_status_bar;
if (file::exists(file_name)) {
using_(fstream fs = file::open(file_name, ios::in | ios::binary)) {
binary_reader 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);
// 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.

◆ 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.

◆ 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.

◆ peek_char()

int32_t 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.

◆ read() [1/3]

virtual int32_t 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< byte_t > &  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 byte_t 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<byte_t> 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_t 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_t 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_t 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_t 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.

◆ read_string()

virtual ustring 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_t 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_t 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_t 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.

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