AWS SDK for C++

AWS SDK for C++ Version 1.11.617

Loading...
Searching...
No Matches
StreamBufProtectedWriter.h
1
7#pragma once
8
9#include <aws/core/Core_EXPORTS.h>
10#include <aws/core/utils/Array.h>
11#include <streambuf>
12#include <functional>
13
14namespace Aws
15{
16 namespace Utils
17 {
18 namespace Stream
19 {
23 class AWS_CORE_API StreamBufProtectedWriter : public std::streambuf
24 {
25 public:
27
28 using WriterFunc = std::function<bool(char* dst, uint64_t dstSz, uint64_t& read)>;
29
30 static uint64_t WriteToBuffer(Aws::IOStream& ioStream, const WriterFunc& writerFunc)
31 {
32 uint64_t totalRead = 0;
33
34 while (true)
35 {
36 StreamBufProtectedWriter* pBufferCasted = static_cast<StreamBufProtectedWriter*>(ioStream.rdbuf());
37 bool bufferPresent = pBufferCasted && pBufferCasted->pptr() && (pBufferCasted->pptr() < pBufferCasted->epptr());
38 uint64_t read = 0;
39 bool success = false;
40 if (bufferPresent)
41 {
42 // have access to underlying put ptr.
43 success = WriteDirectlyToPtr(pBufferCasted, writerFunc, read);
44 }
45 else
46 {
47 // can't access underlying buffer, stream buffer maybe be customized to not use put ptr.
48 // or underlying put buffer is simply not initialized yet.
49 success = WriteWithHelperBuffer(ioStream, writerFunc, read);
50 }
51 totalRead += read;
52 if (!success)
53 {
54 break;
55 }
56
57 if (pBufferCasted && pBufferCasted->pptr() && (pBufferCasted->pptr() >= pBufferCasted->epptr()))
58 {
59 if(!ForceOverflow(ioStream, writerFunc))
60 {
61 break;
62 } else {
63 totalRead++;
64 }
65 }
66 }
67 return totalRead;
68 }
69 protected:
70 static bool ForceOverflow(Aws::IOStream& ioStream, const WriterFunc& writerFunc)
71 {
72 char dstChar;
73 uint64_t read = 0;
74 if (writerFunc(&dstChar, 1, read) && read > 0)
75 {
76 ioStream.write(&dstChar, 1);
77 if (ioStream.fail()) {
78 AWS_LOGSTREAM_ERROR("StreamBufProtectedWriter", "Failed to write 1 byte (eof: "
79 << ioStream.eof() << ", bad: " << ioStream.bad() << ")");
80 return false;
81 }
82 return true;
83 }
84 return false;
85 }
86
87 static uint64_t WriteWithHelperBuffer(Aws::IOStream& ioStream, const WriterFunc& writerFunc, uint64_t& read)
88 {
89 char tmpBuf[1024];
90 uint64_t tmpBufSz = sizeof(tmpBuf);
91
92 if(writerFunc(tmpBuf, tmpBufSz, read) && read > 0)
93 {
94 ioStream.write(tmpBuf, read);
95 if (ioStream.fail()) {
96 AWS_LOGSTREAM_ERROR("StreamBufProtectedWriter", "Failed to write " << tmpBufSz
97 << " (eof: " << ioStream.eof() << ", bad: " << ioStream.bad() << ")");
98 return false;
99 }
100 return true;
101 }
102 return false;
103 }
104
105 static uint64_t WriteDirectlyToPtr(StreamBufProtectedWriter* pBuffer, const WriterFunc& writerFunc, uint64_t& read)
106 {
107 auto dstBegin = pBuffer->pptr();
108 uint64_t dstSz = pBuffer->epptr() - dstBegin;
109 if(writerFunc(dstBegin, dstSz, read) && read > 0)
110 {
111 assert(read <= dstSz);
112 pBuffer->pbump((int) read);
113 return true;
114 }
115 return false;
116 }
117 };
118 }
119 }
120}
static uint64_t WriteWithHelperBuffer(Aws::IOStream &ioStream, const WriterFunc &writerFunc, uint64_t &read)
static uint64_t WriteDirectlyToPtr(StreamBufProtectedWriter *pBuffer, const WriterFunc &writerFunc, uint64_t &read)
std::function< bool(char *dst, uint64_t dstSz, uint64_t &read)> WriterFunc
static bool ForceOverflow(Aws::IOStream &ioStream, const WriterFunc &writerFunc)
static uint64_t WriteToBuffer(Aws::IOStream &ioStream, const WriterFunc &writerFunc)
std::basic_iostream< char, std::char_traits< char > > IOStream