You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/xtd.core/include/xtd/io/memory_stream.hpp
+32-79Lines changed: 32 additions & 79 deletions
Original file line number
Diff line number
Diff line change
@@ -2,43 +2,31 @@
2
2
/// @brief Contains xtd::io::memory_stream class.
3
3
/// @copyright Copyright (c) 2025 Gammasoft. All rights reserved.
4
4
#pragma once
5
-
#include"seek_origin.hpp"
6
-
#include"../core_export.hpp"
7
-
#include"../array.hpp"
8
-
#include"../object.hpp"
9
-
#include"../string.hpp"
10
-
#include<cstdio>
11
-
#include<sstream>
5
+
#include"stream.hpp"
6
+
#include"../collections/generic/list.hpp"
7
+
#include"../new_ptr.hpp"
12
8
13
9
/// @brief The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
14
10
namespacextd {
15
11
/// @brief The xtd::io namespace contains types that allow reading and writing to files and data streams, and types that provide basic file and directory support.
16
12
namespaceio {
17
-
/// @cond
18
13
/// @brief Creates a stream whose backing store is memory.
19
14
/// ```cpp
20
-
/// class core_export_ memory_stream : public xtd::object, public std::stringstream
15
+
/// class core_export_ memory_stream : public xtd::io::stream
21
16
/// ```
22
17
/// @par Inheritance
23
18
/// xtd::object → xtd::io::text_writer
24
19
/// @par Header
25
20
/// ```cpp
26
-
/// #include <xtd/io/text_writer>
21
+
/// #include <xtd/io/memory_stream>
27
22
/// ```
28
23
/// @par Namespace
29
24
/// xtd::io
30
25
/// @par Library
31
26
/// xtd.core
32
27
/// @ingroup xtd_core io
33
-
classcore_export_ memory_stream : public xtd::object, public std::stringstream {
28
+
classcore_export_ memory_stream : public xtd::io::stream {
34
29
public:
35
-
/// @name Public Aliases
36
-
37
-
/// @{
38
-
/// @brief Represents the underlying memory buffer type.
39
-
using memory_buffer = std::stringbuf;
40
-
/// @}
41
-
42
30
/// @name Public constructors
43
31
44
32
/// @{
@@ -60,60 +48,43 @@ namespace xtd {
60
48
/// @{
61
49
/// @brief Gets a value indicating whether the current stream supports reading.
62
50
/// @return `true` if the stream supports reading; otherwise, `false`.
63
-
boolcan_read() constnoexcept;
51
+
boolcan_read() constnoexceptoverride;
64
52
65
53
/// @brief Gets a value indicating whether the current stream supports seeking.
66
54
/// @return `true` if the stream supports seeking; otherwise, `false`.
67
-
boolcan_seek() constnoexcept;
55
+
boolcan_seek() constnoexceptoverride;
68
56
69
57
/// @brief Gets a value indicating whether the current stream supports writing.
70
58
/// @return `true` if the stream supports writing; otherwise, `false`.
71
-
boolcan_write() constnoexcept;
59
+
boolcan_write() constnoexceptoverride;
72
60
73
61
/// @brief Gets the number of bytes allocated for this stream.
74
62
/// @return The length of the usable portion of the buffer for the stream.
75
63
/// @exception xtd::argument_out_of_range_exception A capacity is less than the current length of the stream.
76
64
/// @remarks `capacity` is the buffer length for system-provided byte arrays. `capacity` cannot be set to a value less than the current length of the stream.
77
-
xtd::size capacity() constnoexcept;
65
+
xtd::size capacity() const;
78
66
/// @brief Sets the number of bytes allocated for this stream.
79
67
/// @param value The length of the usable portion of the buffer for the stream.
80
-
/// @return This current instance.
81
68
/// @exception xtd::argument_out_of_range_exception A capacity is less than the current length of the stream.
82
69
/// @remarks `capacity` is the buffer length for system-provided byte arrays. `capacity` cannot be set to a value less than the current length of the stream.
83
-
memory_stream&capacity(xtd::size value);
70
+
voidcapacity(xtd::size value);
84
71
85
72
/// @brief Gets the length of the stream in bytes.
86
73
/// @return The length of the stream in bytes.
87
-
xtd::size length() constnoexcept;
74
+
xtd::size length() constoverride;
88
75
89
76
/// @brief Gets the current position within the stream.
90
77
/// @return The current position within the stream.
91
-
xtd::size position() constnoexcept;
78
+
xtd::size position() constoverride;
92
79
/// @brief Sets the current position within the stream.
93
80
/// @param value The current position within the stream.
94
-
/// @return This current instance.
95
-
memory_stream& position(xtd::size value);
81
+
voidposition(xtd::size value) override;
96
82
/// @}
97
83
98
84
/// @name Public Methods
99
85
100
86
/// @{
101
-
/// @brief Reads the bytes from the current memory stream and writes them to another stream, using a specified buffer size.
102
-
/// @param destination The stream to which the contents of the current memory stream will be copied.
103
-
/// @param buffer_size The size of the buffer. This value must be greater than zero. The default size is 81920.
/// @brief Returns the array of unsigned bytes from which this stream was created.
107
-
/// @return The byte array from which this stream was created, or the underlying array if a byte array was not provided to the xtd::memory_stream::memory_stream constructor during construction of the current instance.
108
-
/// @remarks Note that the buffer contains allocated bytes which might be unused. For example, if the string "test" is written into the xtd::memory_stream object, the length of the buffer returned from xtd::memory_stream::get_buffer is 256, not 4, with 252 bytes unused. To obtain only the data in the buffer, use the xtd::memory_stream::to_array method; however, xtd::memory_stream::to_array creates a copy of the data in memory.
109
-
/// @remarks To create a xtd::memory_stream instance with a publicly visible buffer, use xtd::memory_stream::memory_stream, xtd::memory_stream::memory_stream (const array<byte>&, xtd::zize, xtd::size, bool, bool), or xtd::memory_stream::memory_stream (xtd::size). If the current stream is resizable, two calls to this method do not return the same array if the underlying byte array is resized between calls. For additional information, see xtd::memory_stream::capacity.
110
-
const memory_buffer& get_buffer() const;
111
-
112
-
using std::stringstream::read;
113
-
/// @brief Reads a sequence of bytes from the current memory stream and advances the position within the memory stream by the number of bytes read.
114
-
/// @param buffer A region of memory. When this method returns, the contents of this span are replaced by the bytes read from the current memory stream source.
115
-
/// @return The total number of bytes read into the buffer. This can be less than the number of bytes allocated in the buffer if that many bytes are not currently available, or zero (0) if the end of the memory stream has been reached.
116
-
xtd::size read(xtd::array<xtd::byte>& buffer);
87
+
using xtd::io::stream::read;
117
88
/// @brief Reads a block of bytes from the current stream and writes the data to a buffer.
118
89
/// @param buffer When this method returns, contains the specified byte array with the values between `offset` and (`offset` + `count` - 1) replaced by the characters read from the current stream.
119
90
/// @param offset The zero-based byte offset in buffer at which to begin storing data from the current stream.
@@ -123,20 +94,7 @@ namespace xtd {
123
94
/// @remarks If the read operation is successful, the current position within the stream advances by the number of bytes read. If an exception occurs, the current position within the stream remains unchanged.
124
95
/// @remarks The `read` method will return zero only if the end of the stream is reached. In all other cases, `read` always reads at least one byte from the stream before returning. By definition, if no data is available from the stream upon a call to `read`, the `read` method returns zero (the end of the stream is reached automatically). An implementation is free to return fewer bytes than requested even if the end of the stream has not been reached.
125
96
/// @remarks Use xtd::io::binary_reader for reading primitive data types.
/// @return The byte cast to a xtd::int32, or -1 if the end of the stream has been reached.
130
-
/// @remarks If the read operation is successful, the current position within the stream is advanced by one byte. If an exception occurs, the current position within the stream is unchanged.
131
-
/// @par Examples
132
-
/// This code example is part of a larger example provided for the xtd::io::memory_stream class.
/// @brief Sets the position within the current stream to the specified value.
142
100
/// @param offset The new position within the stream. This is relative to the loc parameter, and can be positive or negative.
@@ -147,20 +105,17 @@ namespace xtd {
147
105
/// @brief Sets the length of the current stream to the specified value.
148
106
/// @param value The value at which to set the length.
149
107
/// @remarks If the specified value is less than the current length of the stream, the stream is truncated. If after the truncation the current position within the stream is past the end of the stream, the xtd::io::memery_stream::read_byte method returns -1, the xtd::io::memery_stream::read method reads zero bytes into the provided byte array, and xtd::io::memery_stream::write and xtd::io::memery_stream::write_byte methods append specified bytes at the end of the stream, increasing its length. If the specified value is larger than the current capacity and the stream is resizable, the capacity is increased, and the current position within the stream is unchanged. If the length is increased, the contents of the stream between the old and the new length are initialized to zeros.
150
-
voidset_length(xtd::size value);
108
+
voidset_length(xtd::size value)override;
151
109
152
110
/// @brief Writes the stream contents to a byte array, regardless of the Position property.
153
111
/// @return A new byte array.
154
112
/// @remarks This method omits unused bytes in xtd::io::memory_stream from the array. To get the entire buffer, use the xtd::io::memory_stream::get_buffer method.
155
113
/// @remarks This method returns a copy of the contents of the xtd::io::memory_stream as a byte array. If the current instance was constructed on a provided byte array, a copy of the section of the array to which this instance has access is returned. See the xtd::io::memory_stream constructor for details.
156
114
xtd::array<xtd::byte> to_array() const;
157
115
158
-
using std::stringstream::write;
159
-
/// @brief Writes the sequence of bytes contained in `source` into the current memory stream and advances the current position within this memory stream by the number of bytes written.
160
-
/// @param bytes A region of memory. This method copies the contents of this region to the current memory stream.
161
-
voidwrite(const xtd::array<xtd::byte>& bytes);
116
+
using xtd::io::stream::write;
162
117
/// @brief Writes a block of bytes to the current stream using data read from a buffer.
163
-
/// @param bytes The buffer to write data from.
118
+
/// @param buffer The buffer to write data from.
164
119
/// @param offset The zero-based byte offset in buffer at which to begin copying bytes to the current stream.
165
120
/// @param count The maximum number of bytes to write.
166
121
/// @par Examples
@@ -171,27 +126,25 @@ namespace xtd {
171
126
/// ```
172
127
/// @remarks The `offset` parameter gives the offset of the first byte in `buffer` to write from, and the `count` parameter gives the number of bytes to write. If the write operation is successful, the current position within the stream is advanced by the number of bytes written. If an exception occurs, the current position within the stream is unchanged.
173
128
/// @remarks Except for a `memory_stream` constructed with a byte[] parameter, write operations at the end of a `memory_stream` expand the `memory_stream`.
/// @brief Writes a byte to the current stream at the current position.
176
-
/// @param value The byte to write.
177
-
/// @par Examples
178
-
/// This code example is part of a larger example provided for the MemoryStream class.
179
-
/// ```cpp
180
-
/// // Write the second string to the stream, byte by byte.
181
-
/// count = 0;
182
-
/// while(count < second_string.Length) {
183
-
/// memStream.write_byte(second_string[count++]);
184
-
/// }
185
-
/// ```
186
-
/// @remarks Except for a `memory_stream` constructed with a byte[] parameter, write operations at the end of a `memory_stream` expand the `memory_stream`.
/// @brief Writes the entire contents of this memory stream to another stream.
190
132
/// @param stream The stream to write this memory stream to.
191
133
/// @exception xtd::bject_closed_exception The current or target stream is closed.
192
134
/// @remarks When the current stream is open, this method is equivalent to calling [std::ostream::write](https://en.cppreference.com/w/cpp/io/basic_ostream/write) on the underlying buffer of this stream.
0 commit comments