xtd 0.2.0
Loading...
Searching...
No Matches
binary_writer.hpp
Go to the documentation of this file.
1
4#pragma once
5#define __XTD_STD_INTERNAL__
7#undef __XTD_STD_INTERNAL__
8#include "../array.hpp"
9#include "../fixed_array.hpp"
10#include "../object.hpp"
11#include "../optional.hpp"
12#include "../read_only_span"
13#include "../string.hpp"
14#include <fstream>
15#include <array>
16#include <vector>
17
19namespace xtd {
21 namespace io {
48 public:
50
56 explicit binary_writer(const xtd::string& path);
59 explicit binary_writer(std::ostream& stream);
62 binary_writer(const binary_writer&) = delete;
63 binary_writer& operator =(const binary_writer&) = delete;
66
68
73 std::optional<xtd::ref<std::ostream>> base_stream() const;
75
77
80 virtual void close();
81
84 virtual void flush();
85
92 virtual size_t seek(size_t offset, std::ios::seekdir origin);
93
98 virtual void write(bool value);
99
104 virtual void write(xtd::byte value);
105
110 virtual void write(char value);
111
116 template<size_t size>
117 void write(const std::array<xtd::byte, size>& buffer) {
118 for (auto b : buffer)
119 write(b);
120 }
121
126 template<size_t size>
127 void write(const std::array<char, size>& buffer) {
128 for (auto c : buffer)
129 write(c);
130 }
131
136 virtual void write(const xtd::read_only_span<xtd::byte>& buffer);
137
145 virtual void write(const xtd::array<xtd::byte>& buffer, size_t index, size_t count);
146
151 virtual void write(const xtd::read_only_span<char>& buffer);
152
160 virtual void write(const xtd::array<char>& buffer, size_t index, size_t count);
161
166 virtual void write(double value);
167
172 virtual void write(int16 value);
173
178 virtual void write(int32 value);
179
184 virtual void write(int64 value);
185
190 virtual void write(sbyte value);
191
196 virtual void write(float value);
197
202 virtual void write(const string& value);
204 virtual void write(const std::string& value);
205 virtual void write(const std::u8string& value);
206 virtual void write(const std::u16string& value);
207 virtual void write(const std::u32string& value);
208 virtual void write(const std::wstring& value);
209 virtual void write(const char* value);
210 virtual void write(const char8* value);
211 virtual void write(const char16* value);
212 virtual void write(const char32* value);
213 virtual void write(const wchar* value);
215
220 virtual void write(uint16 value);
221
226 virtual void write(uint32 value);
227
232 virtual void write(uint64 value);
233
235 virtual void write(slong value);
236 virtual void write(xtd::ulong value);
238
240
242 binary_writer & operator <<(bool value) {write(value); return *this;}
243 binary_writer & operator <<(byte value) {write(value); return *this;}
244 binary_writer & operator <<(char value) {write(value); return *this;}
245 template<size_t size>
246 binary_writer & operator <<(const xtd::fixed_array<xtd::byte, size>& value) {write(value); return *this;}
247 template<size_t size>
248 binary_writer & operator <<(const xtd::fixed_array<char, size>& value) {write(value); return *this;}
249 binary_writer & operator <<(const xtd::array<xtd::byte>& value) {write(value, 0, value.size()); return *this;}
250 binary_writer & operator <<(const xtd::array<char>& value) {write(value, 0, value.size()); return *this;}
251 binary_writer & operator <<(double value) {write(value); return *this;}
252 binary_writer & operator <<(int16 value) {write(value); return *this;}
253 binary_writer & operator <<(int32 value) {write(value); return *this;}
254 binary_writer & operator <<(int64 value) {write(value); return *this;}
255 binary_writer & operator <<(sbyte value) {write(value); return *this;}
256 binary_writer & operator <<(float value) {write(value); return *this;}
257 binary_writer & operator <<(const string& value) {write(value); return *this;}
258 binary_writer & operator <<(const char* value) {write(value); return *this;}
259 binary_writer & operator <<(const char8* value) {write(value); return *this;}
260 binary_writer & operator <<(const char16* value) {write(value); return *this;}
261 binary_writer & operator <<(const char32* value) {write(value); return *this;}
262 binary_writer & operator <<(const wchar* value) {write(value); return *this;}
263 binary_writer & operator <<(uint16 value) {write(value); return *this;}
264 binary_writer & operator <<(uint32 value) {write(value); return *this;}
265 binary_writer & operator <<(uint64 value) {write(value); return *this;}
267
268 private:
269 void write_7bit_encoded_int(int32 value);
270 std::ostream* stream_ = nullptr;
271 bool delete_when_destroy_ = false;
272 };
273 }
274}
Contains __xtd_std_version definitions.
Contains xtd::array class.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:63
Represents a fixed array class.
Definition fixed_array.hpp:23
Writes primitive types in binary to a stream and supports writing strings.
Definition binary_writer.hpp:47
virtual void flush()
Clears all buffers for the current writer and causes any buffered data to be written to the underlyin...
virtual void write(int64 value)
Writes a eight-byte signed integer to the current stream and advances the stream position by eight by...
void write(const std::array< xtd::byte, size > &buffer)
Writes a byte array to the underlying stream.
Definition binary_writer.hpp:117
virtual void write(const xtd::read_only_span< xtd::byte > &buffer)
Writes a byte array to the underlying stream.
virtual void write(const xtd::array< xtd::byte > &buffer, size_t index, size_t count)
Writes a region of a byte array to the current stream.
virtual void write(int16 value)
Writes a two-byte signed integer to the current stream and advances the stream position by two bytes.
virtual void write(double value)
Writes an eight-byte floating-point value to the current stream and advances the stream position by e...
virtual void write(const xtd::array< char > &buffer, size_t index, size_t count)
Writes a region of a character array to the current stream.
binary_writer(const xtd::string &path)
Initializes a new instance of the binary_writer class for the specified file name.
void write(const std::array< char, size > &buffer)
Writes a character array to the underlying stream.
Definition binary_writer.hpp:127
virtual size_t seek(size_t offset, std::ios::seekdir origin)
Sets the position within the current stream.
virtual void write(uint64 value)
Writes a eight-byte unsigned integer to the current stream and advances the stream position by eight ...
virtual void write(uint32 value)
Writes a four-byte unsigned integer to the current stream and advances the stream position by four by...
virtual void write(char value)
Writes a character to the current stream and advances the current position by one byte.
virtual void close()
Closes the xtd::io::binary_writer object and the underlying stream, and releases any system resources...
virtual void write(bool value)
Writes a one-byte boolean value to the current stream, with 0 representing false and 1 representing t...
virtual void write(xtd::byte value)
Writes an unsigned byte to the current stream and advances the stream position by one byte.
virtual void write(uint16 value)
Writes a two-byte unsigned integer to the current stream and advances the stream position by two byte...
binary_writer(std::ostream &stream)
Initializes a new instance of the binary_writer class for the specified stream.
virtual void write(int32 value)
Writes a four-byte signed integer to the current stream and advances the stream position by four byte...
std::optional< xtd::ref< std::ostream > > base_stream() const
Returns the underlying stream.
virtual void write(sbyte value)
Writes an signed byte to the current stream and advances the stream position by one byte.
virtual void write(float value)
Writes an four-byte floating-point value to the current stream and advances the stream position by fo...
virtual void write(const string &value)
Writes a length-prefixed string to this stream, and advances the current position of the stream and t...
virtual void write(const xtd::read_only_span< char > &buffer)
Writes a character array to the underlying stream.
Performs operations on std::basic_string instances that contain file or directory path information....
Definition path.hpp:37
Provides a generic view of a sequence of bytes. This is an abstract class.
Definition stream.hpp:40
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:44
Contains xtd::fixed_array class.
#define core_export_
Define shared library export.
Definition core_export.hpp:13
xtd::basic_string< char > string
Represents text as a sequence of UTF-8 code units.
Definition __string_definitions.hpp:43
std::int64_t int64
Represents a 64-bit signed integer.
Definition int64.hpp:23
std::uint32_t uint32
Represents a 32-bit unsigned integer.
Definition uint32.hpp:23
char8_t char8
Represents a 8-bit unicode character.
Definition char8.hpp:26
__slong__ slong
Represents a 32-bit or 64-bit signed integer.
Definition slong.hpp:27
std::int8_t sbyte
Represents a 8-bit signed integer.
Definition sbyte.hpp:23
wchar_t wchar
Represents a wide character.
Definition wchar.hpp:24
__ulong__ ulong
Represents a 32-bit or 64-bit unsigned integer.
Definition ulong.hpp:27
std::uint16_t uint16
Represents a 16-bit unsigned integer.
Definition uint16.hpp:23
char16_t char16
Represents a 16-bit unicode character.
Definition char16.hpp:26
std::int32_t int32
Represents a 32-bit signed integer.
Definition int32.hpp:23
std::int16_t int16
Represents a 16-bit signed integer.
Definition int16.hpp:23
std::uint64_t uint64
Represents a 64-bit unsigned integer.
Definition uint64.hpp:23
std::uint8_t byte
Represents a 8-bit unsigned integer.
Definition byte.hpp:23
char32_t char32
Represents a 32-bit unicode character.
Definition char32.hpp:23
@ write
Write access to the file. Data can be written to the file. Combine with Read for read/write access.
Definition file_access.hpp:19
@ c
The C key.
Definition console_key.hpp:92
@ b
The B key.
Definition console_key.hpp:90
The xtd::io namespace contains types that allow reading and writing to files and data streams,...
Definition binary_reader.hpp:17
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
constexpr read_only_span(const element_type(&array)[len]) noexcept
Creates an xtd::read_only_span with specified native array.
Definition read_only_span.hpp:112
Contains xtd::object class.
Contains xtd::optional type.
Contains xtd::string alias.