BufferStream.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /***************************************************************************
  2. *
  3. * Project _____ __ ____ _ _
  4. * ( _ ) /__\ (_ _)_| |_ _| |_
  5. * )(_)( /(__)\ )( (_ _)(_ _)
  6. * (_____)(__)(__)(__) |_| |_|
  7. *
  8. *
  9. * Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. *
  23. ***************************************************************************/
  24. #ifndef oatpp_data_stream_BufferStream_hpp
  25. #define oatpp_data_stream_BufferStream_hpp
  26. #include "Stream.hpp"
  27. namespace oatpp { namespace data{ namespace stream {
  28. /**
  29. * BufferOutputStream
  30. */
  31. class BufferOutputStream : public ConsistentOutputStream {
  32. public:
  33. static data::stream::DefaultInitializedContext DEFAULT_CONTEXT;
  34. private:
  35. p_char8 m_data;
  36. v_buff_size m_capacity;
  37. v_buff_size m_position;
  38. v_buff_size m_maxCapacity;
  39. IOMode m_ioMode;
  40. private:
  41. std::shared_ptr<void> m_capturedData;
  42. public:
  43. /**
  44. * Constructor.
  45. * @param growBytes
  46. * @param captureData - capture auxiliary data to not get deleted until it's done with the stream.
  47. */
  48. BufferOutputStream(v_buff_size initialCapacity = 2048, const std::shared_ptr<void>& captureData = nullptr);
  49. /**
  50. * Virtual destructor.
  51. */
  52. ~BufferOutputStream();
  53. /**
  54. * Write `count` of bytes to stream.
  55. * @param data - data to write.
  56. * @param count - number of bytes to write.
  57. * @param action - async specific action. If action is NOT &id:oatpp::async::Action::TYPE_NONE;, then
  58. * caller MUST return this action on coroutine iteration.
  59. * @return - actual number of bytes written. &id:oatpp::v_io_size;.
  60. */
  61. v_io_size write(const void *data, v_buff_size count, async::Action& action) override;
  62. /**
  63. * Set stream I/O mode.
  64. * @throws
  65. */
  66. void setOutputStreamIOMode(IOMode ioMode) override;
  67. /**
  68. * Get stream I/O mode.
  69. * @return
  70. */
  71. IOMode getOutputStreamIOMode() override;
  72. /**
  73. * Get stream context.
  74. * @return
  75. */
  76. Context& getOutputStreamContext() override;
  77. /**
  78. * Reserve bytes for future writes.
  79. */
  80. void reserveBytesUpfront(v_buff_size count);
  81. /**
  82. * Get pointer to data.
  83. * @return - pointer to data.
  84. */
  85. p_char8 getData();
  86. /**
  87. * Get current capacity.
  88. * Capacity may change.
  89. * @return
  90. */
  91. v_buff_size getCapacity();
  92. /**
  93. * Get current data write position.
  94. * @return - current data write position.
  95. */
  96. v_buff_size getCurrentPosition();
  97. /**
  98. * Set current data write position.
  99. * @param position - data write position.
  100. */
  101. void setCurrentPosition(v_buff_size position);
  102. /**
  103. * Reset stream buffer and its capacity. Also reset write position.
  104. * @param initialCapacity
  105. */
  106. void reset(v_buff_size initialCapacity = 2048);
  107. /**
  108. * Copy data to &id:oatpp::String;.
  109. * @return
  110. */
  111. oatpp::String toString();
  112. /**
  113. * Create &id:oatpp::String; from part of buffer.
  114. * @param pos - starting position in buffer.
  115. * @param count - size of bytes to write to substring.
  116. * @return - &id:oatpp::String;
  117. */
  118. oatpp::String getSubstring(v_buff_size pos, v_buff_size count);
  119. /**
  120. * Write all bytes from buffer to stream.
  121. * @param stream - stream to flush all data to.
  122. * @return - actual amount of bytes flushed.
  123. */
  124. oatpp::v_io_size flushToStream(OutputStream* stream);
  125. /**
  126. * Write all bytes from buffer to stream in async manner.
  127. * @param _this - pointer to `this` buffer.
  128. * @param stream - stream to flush all data to.
  129. * @return - &id:oatpp::async::CoroutineStarter;.
  130. */
  131. static oatpp::async::CoroutineStarter flushToStreamAsync(const std::shared_ptr<BufferOutputStream>& _this, const std::shared_ptr<OutputStream>& stream);
  132. };
  133. /**
  134. * BufferInputStream
  135. */
  136. class BufferInputStream : public BufferedInputStream {
  137. public:
  138. static data::stream::DefaultInitializedContext DEFAULT_CONTEXT;
  139. private:
  140. std::shared_ptr<std::string> m_memoryHandle;
  141. p_char8 m_data;
  142. v_buff_size m_size;
  143. v_buff_size m_position;
  144. IOMode m_ioMode;
  145. private:
  146. std::shared_ptr<void> m_capturedData;
  147. public:
  148. /**
  149. * Constructor.
  150. * @param memoryHandle - buffer memory handle. May be nullptr.
  151. * @param data - pointer to buffer data.
  152. * @param size - size of the buffer.
  153. * @param captureData - capture auxiliary data to not get deleted until it's done with the stream.
  154. */
  155. BufferInputStream(const std::shared_ptr<std::string>& memoryHandle,
  156. const void* data,
  157. v_buff_size size,
  158. const std::shared_ptr<void>& captureData = nullptr);
  159. /**
  160. * Constructor.
  161. * @param data - buffer.
  162. * @param captureData - capture auxiliary data to not get deleted until it's done with the stream.
  163. */
  164. BufferInputStream(const oatpp::String& data, const std::shared_ptr<void>& captureData = nullptr);
  165. /**
  166. * Reset stream data and set position to `0`.
  167. * @param memoryHandle - buffer memory handle. May be nullptr.
  168. * @param data - pointer to buffer data.
  169. * @param size - size of the buffer.
  170. * @param captureData - capture auxiliary data to not get deleted until it's done with the stream.
  171. */
  172. void reset(const std::shared_ptr<std::string>& memoryHandle,
  173. p_char8 data,
  174. v_buff_size size,
  175. const std::shared_ptr<void>& captureData = nullptr);
  176. /**
  177. * Same as `reset(nullptr, nullptr, 0);.`
  178. */
  179. void reset();
  180. /**
  181. * Read data from stream. <br>
  182. * It is a legal case if return result < count. Caller should handle this!
  183. * *Calls to this method are always NON-BLOCKING*
  184. * @param data - buffer to read data to.
  185. * @param count - size of the buffer.
  186. * @param action - async specific action. If action is NOT &id:oatpp::async::Action::TYPE_NONE;, then
  187. * caller MUST return this action on coroutine iteration.
  188. * @return - actual number of bytes read. 0 - designates end of the buffer.
  189. */
  190. v_io_size read(void *data, v_buff_size count, async::Action& action) override;
  191. /**
  192. * Set stream I/O mode.
  193. * @throws
  194. */
  195. void setInputStreamIOMode(IOMode ioMode) override;
  196. /**
  197. * Get stream I/O mode.
  198. * @return
  199. */
  200. IOMode getInputStreamIOMode() override;
  201. /**
  202. * Get stream context.
  203. * @return
  204. */
  205. Context& getInputStreamContext() override;
  206. /**
  207. * Get data memory handle.
  208. * @return - data memory handle.
  209. */
  210. std::shared_ptr<std::string> getDataMemoryHandle();
  211. /**
  212. * Get pointer to data.
  213. * @return - pointer to data.
  214. */
  215. p_char8 getData();
  216. /**
  217. * Get data size.
  218. * @return - data size.
  219. */
  220. v_buff_size getDataSize();
  221. /**
  222. * Get current data read position.
  223. * @return - current data read position.
  224. */
  225. v_buff_size getCurrentPosition();
  226. /**
  227. * Set current data read position.
  228. * @param position - data read position.
  229. */
  230. void setCurrentPosition(v_buff_size position);
  231. /**
  232. * Peek up to count of bytes int he buffer
  233. * @param data
  234. * @param count
  235. * @return [1..count], IOErrors.
  236. */
  237. v_io_size peek(void *data, v_buff_size count, async::Action& action) override;
  238. /**
  239. * Amount of bytes currently available to read from buffer.
  240. * @return &id:oatpp::v_io_size;.
  241. */
  242. v_io_size availableToRead() const override;
  243. /**
  244. * Commit read offset
  245. * @param count
  246. * @return [1..count], IOErrors.
  247. */
  248. v_io_size commitReadOffset(v_buff_size count) override;
  249. };
  250. }}}
  251. #endif // oatpp_data_stream_BufferStream_hpp