Debian Codegraph @

src/kj/io.c++

raw symbols SCIP
1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributorscxx . . $ `<file>/src/kj/io.c++`/
2 // Licensed under the MIT License:
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21
22 #ifndef _GNU_SOURCEcxx . . $ `<built-in>:466:9`!
23 #define _GNU_SOURCE
24 #endif
25
26 #if _WIN32
27 #include "win32-api-version.h"
28 #endif
29
30 #include "io.h"cxx . . $ `<file>/src/kj/io.h`/
31 #include "debug.h"cxx . . $ `<file>/src/kj/debug.h`/
32 #include "miniposix.h"cxx . . $ `<file>/src/kj/miniposix.h`/
33 #include <algorithm>
34 #include <errno.h>
35 #include "vector.h"cxx . . $ `<file>/src/kj/vector.h`/
36 #include <limits.h>
37
38 #if _WIN32
39 #include <windows.h>
40 #include "windows-sanity.h"
41 #else
42 #include <sys/uio.h>
43 #endif
44
45 namespace kjcxx . . $ kj/ {
46
47 InputStreamcxx . . $ kj/InputStream#::~cxx . . $ kj/InputStream#`~InputStream`(49f6e7a06ebc5aa8).InputStreamcxx . . $ kj/InputStream#() noexcept(false) {}
48 OutputStreamcxx . . $ kj/OutputStream#::~cxx . . $ kj/OutputStream#`~OutputStream`(49f6e7a06ebc5aa8).OutputStreamcxx . . $ kj/OutputStream#() noexcept(false) {}
49 BufferedInputStreamcxx . . $ kj/BufferedInputStream#::~cxx . . $ kj/BufferedInputStream#`~BufferedInputStream`(49f6e7a06ebc5aa8).BufferedInputStreamcxx . . $ kj/BufferedInputStream#() noexcept(false) {}
50 BufferedOutputStreamcxx . . $ kj/BufferedOutputStream#::~cxx . . $ kj/BufferedOutputStream#`~BufferedOutputStream`(49f6e7a06ebc5aa8).BufferedOutputStreamcxx . . $ kj/BufferedOutputStream#() noexcept(false) {}
51
52 size_t InputStreamcxx . . $ kj/InputStream#::readcxx . . $ kj/InputStream#read(e2eaf0fd878a292f).(void* buffer, size_t minBytes, size_t maxBytes) {
53 size_t n = tryReadcxx . . $ kj/InputStream#tryRead(e2eaf0fd878a292f).(buffer, minBytes, maxBytes);
54 KJ_REQUIREcxx . . $ `src/kj/debug.h:227:9`!(n >= minBytes, "Premature EOF") {
55 // Pretend we read zeros from the input.
56 memset(reinterpret_cast<bytecxx . . $ kj/byte#*>(buffer) + n, 0, minBytes - n);
57 return minBytes;
58 }
59 return n;
60 }
61
62 void InputStreamcxx . . $ kj/InputStream#::skipcxx . . $ kj/InputStream#skip(2c44cd906ae4e829).(size_t bytes) {
63 char scratch[8192];
64 while (bytes > 0) {
65 size_t amount = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).(bytes, sizeof(scratch));
66 readcxx . . $ kj/InputStream#read(5500c4293b1bbef0).(scratch, amount);
67 bytes -= amount;
68 }
69 }
70
71
72 namespacecxx . . $ kj/`$anonymous_namespace_src/kj/io.c++`/ {
73
74 Arraycxx . . $ kj/Array#<bytecxx . . $ kj/byte#> readAllcxx . . $ kj/`$anonymous_namespace_src/kj/io.c++`/readAll(d3e249488bd214b6).(InputStreamcxx . . $ kj/InputStream#& input, uint64_tcxx . . $ uint64_t# limit, bool nulTerminate) {
75 Vectorcxx . . $ kj/Vector#<Arraycxx . . $ kj/Array#<bytecxx . . $ kj/byte#>> partscxx . . $ kj/Vector#Vector(49f6e7a06ebc5aa8).;
76 constexpr size_t BLOCK_SIZE = 4096;
77
78 for (;;) {
79 KJ_REQUIREcxx . . $ `src/kj/debug.h:227:9`!(limit > 0, "Reached limit before EOF.");
80 auto part = heapArraycxx . . $ kj/heapArray(30935e9a1d746cdf).<bytecxx . . $ kj/byte#>(kjcxx . . $ kj/::mincxx . . $ kj/min(4af833f8d939a8d2).(BLOCK_SIZE, limit));
81 size_t n = input.tryReadcxx . . $ kj/InputStream#tryRead(e2eaf0fd878a292f).(part.begincxx . . $ kj/Array#begin(155dc7f103cdb9db).(), part.sizecxx . . $ kj/Array#size(fe4d2d262d4164a8).(), part.sizecxx . . $ kj/Array#size(fe4d2d262d4164a8).());
82 limit -= n;
83 if (n < part.sizecxx . . $ kj/Array#size(fe4d2d262d4164a8).()) {
84 auto result = heapArraycxx . . $ kj/heapArray(30935e9a1d746cdf).<bytecxx . . $ kj/byte#>(parts.sizecxx . . $ kj/Vector#size(fe4d2d262d4164a8).() * BLOCK_SIZE + n + nulTerminate);
85 bytecxx . . $ kj/byte#* pos = result.begincxx . . $ kj/Array#begin(155dc7f103cdb9db).();
86 for (auto& p: parts) {
87 memcpy(pos, p.begincxx . . $ kj/Array#begin(155dc7f103cdb9db).(), BLOCK_SIZE);
88 pos += BLOCK_SIZE;
89 }
90 memcpy(pos, part.begincxx . . $ kj/Array#begin(155dc7f103cdb9db).(), n);
91 pos += n;
92 if (nulTerminate) *pos++ = '\0';
93 KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(pos == result.end());
94 return resultcxx . . $ kj/Array#Array(d736060a6fd4d37e).;
95 } else {
96 parts.addcxx . . $ kj/Vector#add(7f9966f43a57f391).(kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(part));
97 }
98 }
99 }
100
101 } // namespace
102
103 Stringcxx . . $ kj/String# InputStreamcxx . . $ kj/InputStream#::readAllTextcxx . . $ kj/InputStream#readAllText(1fe7ee1713dc9afb).(uint64_tcxx . . $ uint64_t# limit) {
104 return Stringcxx . . $ kj/String#String(74a5dfe5bc25aac).(readAllcxx . . $ kj/`$anonymous_namespace_src/kj/io.c++`/readAll(d3e249488bd214b6).(*this, limit, true).releaseAsCharscxx . . $ kj/Array#releaseAsChars(b8aa2ce78eb48d42).());
105 }
106 Arraycxx . . $ kj/Array#<bytecxx . . $ kj/byte#> InputStreamcxx . . $ kj/InputStream#::readAllBytescxx . . $ kj/InputStream#readAllBytes(7a707b5bf3ddf9a9).(uint64_tcxx . . $ uint64_t# limit) {
107 return readAllcxx . . $ kj/`$anonymous_namespace_src/kj/io.c++`/readAll(d3e249488bd214b6).(*this, limit, false);
108 }
109
110 void OutputStreamcxx . . $ kj/OutputStream#::writecxx . . $ kj/OutputStream#write(9a8c2cc5c087affd).(ArrayPtrcxx . . $ kj/ArrayPtr#<const ArrayPtrcxx . . $ kj/ArrayPtr#<const bytecxx . . $ kj/byte#>> pieces) {
111 for (auto piece: pieces) {
112 writecxx . . $ kj/OutputStream#write(8c7266571c1d65cc).(piece.begincxx . . $ kj/ArrayPtr#begin(155dc7f103cdb9db).(), piece.sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).());
113 }
114 }
115
116 ArrayPtrcxx . . $ kj/ArrayPtr#<const bytecxx . . $ kj/byte#> BufferedInputStreamcxx . . $ kj/BufferedInputStream#::getReadBuffercxx . . $ kj/BufferedInputStream#getReadBuffer(9d2ad03af55baf09).() {
117 auto result = tryGetReadBuffercxx . . $ kj/BufferedInputStream#tryGetReadBuffer(9d2ad03af55baf09).();
118 KJ_REQUIREcxx . . $ `src/kj/debug.h:227:9`!(result.size() > 0, "Premature EOF");
119 return result;
120 }
121
122 // =======================================================================================
123
124 BufferedInputStreamWrappercxx . . $ kj/BufferedInputStreamWrapper#::BufferedInputStreamWrappercxx . . $ kj/BufferedInputStreamWrapper#BufferedInputStreamWrapper(461e786f64c76094).(InputStreamcxx . . $ kj/InputStream#& inner, ArrayPtrcxx . . $ kj/ArrayPtr#<bytecxx . . $ kj/byte#> buffer)
125 : innercxx . . $ kj/BufferedInputStreamWrapper#inner.(inner), ownedBuffercxx . . $ kj/BufferedInputStreamWrapper#ownedBuffer.(buffer ==cxx . . $ kj/ArrayPtr#`operator==`(b102b25da64d45e5). nullptr ? heapArraycxx . . $ kj/heapArray(30935e9a1d746cdf).<bytecxx . . $ kj/byte#>(8192) : nullptrcxx . . $ kj/Array#Array(49b4c06ce5d54a60).),
126 buffercxx . . $ kj/BufferedInputStreamWrapper#buffer.(buffer ==cxx . . $ kj/ArrayPtr#`operator==`(b102b25da64d45e5). nullptr ? ownedBuffercxx . . $ kj/BufferedInputStreamWrapper#ownedBuffer. : buffer) {}
127
128 BufferedInputStreamWrappercxx . . $ kj/BufferedInputStreamWrapper#::~cxx . . $ kj/BufferedInputStreamWrapper#`~BufferedInputStreamWrapper`(49f6e7a06ebc5aa8).BufferedInputStreamWrappercxx . . $ kj/BufferedInputStreamWrapper#() noexcept(false) {}
129
130 ArrayPtrcxx . . $ kj/ArrayPtr#<const bytecxx . . $ kj/byte#> BufferedInputStreamWrappercxx . . $ kj/BufferedInputStreamWrapper#::tryGetReadBuffercxx . . $ kj/BufferedInputStreamWrapper#tryGetReadBuffer(9d2ad03af55baf09).() {
131 if (bufferAvailablecxx . . $ kj/BufferedInputStreamWrapper#bufferAvailable..sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).() == 0) {
132 size_t n = innercxx . . $ kj/BufferedInputStreamWrapper#inner..tryReadcxx . . $ kj/InputStream#tryRead(e2eaf0fd878a292f).(buffercxx . . $ kj/BufferedInputStreamWrapper#buffer..begincxx . . $ kj/ArrayPtr#begin(155dc7f103cdb9db).(), 1, buffercxx . . $ kj/BufferedInputStreamWrapper#buffer..sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).());
133 bufferAvailablecxx . . $ kj/BufferedInputStreamWrapper#bufferAvailable. =cxx . . $ kj/ArrayPtr#`operator=`(d8484f2dc887c4f0). buffercxx . . $ kj/BufferedInputStreamWrapper#buffer..slicecxx . . $ kj/ArrayPtr#slice(2c7e4e1cf603d8fe).(0, n);
134 }
135
136 return bufferAvailablecxx . . $ kj/BufferedInputStreamWrapper#bufferAvailable.;
137 }
138
139 size_t BufferedInputStreamWrappercxx . . $ kj/BufferedInputStreamWrapper#::tryReadcxx . . $ kj/BufferedInputStreamWrapper#tryRead(e2eaf0fd878a292f).(void* dst, size_t minBytes, size_t maxBytes) {
140 if (minBytes <= bufferAvailablecxx . . $ kj/BufferedInputStreamWrapper#bufferAvailable..sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).()) {
141 // Serve from current buffer.
142 size_t n = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).(bufferAvailablecxx . . $ kj/BufferedInputStreamWrapper#bufferAvailable..sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).(), maxBytes);
143 memcpy(dst, bufferAvailablecxx . . $ kj/BufferedInputStreamWrapper#bufferAvailable..begincxx . . $ kj/ArrayPtr#begin(155dc7f103cdb9db).(), n);
144 bufferAvailablecxx . . $ kj/BufferedInputStreamWrapper#bufferAvailable. =cxx . . $ kj/ArrayPtr#`operator=`(d8484f2dc887c4f0). bufferAvailablecxx . . $ kj/BufferedInputStreamWrapper#bufferAvailable..slicecxx . . $ kj/ArrayPtr#slice(2c7e4e1cf603d8fe).(n, bufferAvailablecxx . . $ kj/BufferedInputStreamWrapper#bufferAvailable..sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).());
145 return n;
146 } else {
147 // Copy current available into destination.
148 memcpy(dst, bufferAvailablecxx . . $ kj/BufferedInputStreamWrapper#bufferAvailable..begincxx . . $ kj/ArrayPtr#begin(155dc7f103cdb9db).(), bufferAvailablecxx . . $ kj/BufferedInputStreamWrapper#bufferAvailable..sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).());
149 size_t fromFirstBuffer = bufferAvailablecxx . . $ kj/BufferedInputStreamWrapper#bufferAvailable..sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).();
150
151 dst = reinterpret_cast<bytecxx . . $ kj/byte#*>(dst) + fromFirstBuffer;
152 minBytes -= fromFirstBuffer;
153 maxBytes -= fromFirstBuffer;
154
155 if (maxBytes <= buffercxx . . $ kj/BufferedInputStreamWrapper#buffer..sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).()) {
156 // Read the next buffer-full.
157 size_t n = innercxx . . $ kj/BufferedInputStreamWrapper#inner..tryReadcxx . . $ kj/InputStream#tryRead(e2eaf0fd878a292f).(buffercxx . . $ kj/BufferedInputStreamWrapper#buffer..begincxx . . $ kj/ArrayPtr#begin(155dc7f103cdb9db).(), minBytes, buffercxx . . $ kj/BufferedInputStreamWrapper#buffer..sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).());
158 size_t fromSecondBuffer = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).(n, maxBytes);
159 memcpy(dst, buffercxx . . $ kj/BufferedInputStreamWrapper#buffer..begincxx . . $ kj/ArrayPtr#begin(155dc7f103cdb9db).(), fromSecondBuffer);
160 bufferAvailablecxx . . $ kj/BufferedInputStreamWrapper#bufferAvailable. =cxx . . $ kj/ArrayPtr#`operator=`(d8484f2dc887c4f0). buffercxx . . $ kj/BufferedInputStreamWrapper#buffer..slicecxx . . $ kj/ArrayPtr#slice(2c7e4e1cf603d8fe).(fromSecondBuffer, n);
161 return fromFirstBuffer + fromSecondBuffer;
162 } else {
163 // Forward large read to the underlying stream.
164 bufferAvailablecxx . . $ kj/BufferedInputStreamWrapper#bufferAvailable. =cxx . . $ kj/ArrayPtr#`operator=`(8f0565344b56ab1). nullptr;
165 return fromFirstBuffer + innercxx . . $ kj/BufferedInputStreamWrapper#inner..tryReadcxx . . $ kj/InputStream#tryRead(e2eaf0fd878a292f).(dst, minBytes, maxBytes);
166 }
167 }
168 }
169
170 void BufferedInputStreamWrappercxx . . $ kj/BufferedInputStreamWrapper#::skipcxx . . $ kj/BufferedInputStreamWrapper#skip(2c44cd906ae4e829).(size_t bytes) {
171 if (bytes <= bufferAvailablecxx . . $ kj/BufferedInputStreamWrapper#bufferAvailable..sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).()) {
172 bufferAvailablecxx . . $ kj/BufferedInputStreamWrapper#bufferAvailable. =cxx . . $ kj/ArrayPtr#`operator=`(d8484f2dc887c4f0). bufferAvailablecxx . . $ kj/BufferedInputStreamWrapper#bufferAvailable..slicecxx . . $ kj/ArrayPtr#slice(2c7e4e1cf603d8fe).(bytes, bufferAvailablecxx . . $ kj/BufferedInputStreamWrapper#bufferAvailable..sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).());
173 } else {
174 bytes -= bufferAvailablecxx . . $ kj/BufferedInputStreamWrapper#bufferAvailable..sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).();
175 if (bytes <= buffercxx . . $ kj/BufferedInputStreamWrapper#buffer..sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).()) {
176 // Read the next buffer-full.
177 size_t n = innercxx . . $ kj/BufferedInputStreamWrapper#inner..readcxx . . $ kj/InputStream#read(e2eaf0fd878a292f).(buffercxx . . $ kj/BufferedInputStreamWrapper#buffer..begincxx . . $ kj/ArrayPtr#begin(155dc7f103cdb9db).(), bytes, buffercxx . . $ kj/BufferedInputStreamWrapper#buffer..sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).());
178 bufferAvailablecxx . . $ kj/BufferedInputStreamWrapper#bufferAvailable. =cxx . . $ kj/ArrayPtr#`operator=`(d8484f2dc887c4f0). buffercxx . . $ kj/BufferedInputStreamWrapper#buffer..slicecxx . . $ kj/ArrayPtr#slice(2c7e4e1cf603d8fe).(bytes, n);
179 } else {
180 // Forward large skip to the underlying stream.
181 bufferAvailablecxx . . $ kj/BufferedInputStreamWrapper#bufferAvailable. =cxx . . $ kj/ArrayPtr#`operator=`(8f0565344b56ab1). nullptr;
182 innercxx . . $ kj/BufferedInputStreamWrapper#inner..skipcxx . . $ kj/InputStream#skip(2c44cd906ae4e829).(bytes);
183 }
184 }
185 }
186
187 // -------------------------------------------------------------------
188
189 BufferedOutputStreamWrappercxx . . $ kj/BufferedOutputStreamWrapper#::BufferedOutputStreamWrappercxx . . $ kj/BufferedOutputStreamWrapper#BufferedOutputStreamWrapper(5bd3172a9dce0610).(OutputStreamcxx . . $ kj/OutputStream#& inner, ArrayPtrcxx . . $ kj/ArrayPtr#<bytecxx . . $ kj/byte#> buffer)
190 : innercxx . . $ kj/BufferedOutputStreamWrapper#inner.(inner),
191 ownedBuffercxx . . $ kj/BufferedOutputStreamWrapper#ownedBuffer.(buffer ==cxx . . $ kj/ArrayPtr#`operator==`(b102b25da64d45e5). nullptr ? heapArraycxx . . $ kj/heapArray(30935e9a1d746cdf).<bytecxx . . $ kj/byte#>(8192) : nullptrcxx . . $ kj/Array#Array(49b4c06ce5d54a60).),
192 buffercxx . . $ kj/BufferedOutputStreamWrapper#buffer.(buffer ==cxx . . $ kj/ArrayPtr#`operator==`(b102b25da64d45e5). nullptr ? ownedBuffercxx . . $ kj/BufferedOutputStreamWrapper#ownedBuffer. : buffer),
193 bufferPoscxx . . $ kj/BufferedOutputStreamWrapper#bufferPos.(this->buffercxx . . $ kj/BufferedOutputStreamWrapper#buffer..begincxx . . $ kj/ArrayPtr#begin(155dc7f103cdb9db).()) {}
194
195 BufferedOutputStreamWrappercxx . . $ kj/BufferedOutputStreamWrapper#::~cxx . . $ kj/BufferedOutputStreamWrapper#`~BufferedOutputStreamWrapper`(49f6e7a06ebc5aa8).BufferedOutputStreamWrappercxx . . $ kj/BufferedOutputStreamWrapper#() noexcept(false) {
196 unwindDetectorcxx . . $ kj/BufferedOutputStreamWrapper#unwindDetector..catchExceptionsIfUnwindingcxx . . $ kj/UnwindDetector#catchExceptionsIfUnwinding(8c463d1e13f06b00).([&]() {
197 flushcxx . . $ kj/BufferedOutputStreamWrapper#flush(49f6e7a06ebc5aa8).();
198 });
199 }
200
201 void BufferedOutputStreamWrappercxx . . $ kj/BufferedOutputStreamWrapper#::flushcxx . . $ kj/BufferedOutputStreamWrapper#flush(49f6e7a06ebc5aa8).() {
202 if (bufferPoscxx . . $ kj/BufferedOutputStreamWrapper#bufferPos. > buffercxx . . $ kj/BufferedOutputStreamWrapper#buffer..begincxx . . $ kj/ArrayPtr#begin(155dc7f103cdb9db).()) {
203 innercxx . . $ kj/BufferedOutputStreamWrapper#inner..writecxx . . $ kj/OutputStream#write(8c7266571c1d65cc).(buffercxx . . $ kj/BufferedOutputStreamWrapper#buffer..begincxx . . $ kj/ArrayPtr#begin(155dc7f103cdb9db).(), bufferPoscxx . . $ kj/BufferedOutputStreamWrapper#bufferPos. - buffercxx . . $ kj/BufferedOutputStreamWrapper#buffer..begincxx . . $ kj/ArrayPtr#begin(155dc7f103cdb9db).());
204 bufferPoscxx . . $ kj/BufferedOutputStreamWrapper#bufferPos. = buffercxx . . $ kj/BufferedOutputStreamWrapper#buffer..begincxx . . $ kj/ArrayPtr#begin(155dc7f103cdb9db).();
205 }
206 }
207
208 ArrayPtrcxx . . $ kj/ArrayPtr#<bytecxx . . $ kj/byte#> BufferedOutputStreamWrappercxx . . $ kj/BufferedOutputStreamWrapper#::getWriteBuffercxx . . $ kj/BufferedOutputStreamWrapper#getWriteBuffer(fc5425f282210c4e).() {
209 return arrayPtrcxx . . $ kj/arrayPtr(25656fe11b4c7c4b).(bufferPoscxx . . $ kj/BufferedOutputStreamWrapper#bufferPos., buffercxx . . $ kj/BufferedOutputStreamWrapper#buffer..endcxx . . $ kj/ArrayPtr#end(155dc7f103cdb9db).());
210 }
211
212 void BufferedOutputStreamWrappercxx . . $ kj/BufferedOutputStreamWrapper#::writecxx . . $ kj/BufferedOutputStreamWrapper#write(8c7266571c1d65cc).(const void* src, size_t size) {
213 if (src == bufferPoscxx . . $ kj/BufferedOutputStreamWrapper#bufferPos.) {
214 // Oh goody, the caller wrote directly into our buffer.
215 bufferPoscxx . . $ kj/BufferedOutputStreamWrapper#bufferPos. += size;
216 } else {
217 size_t available = buffercxx . . $ kj/BufferedOutputStreamWrapper#buffer..endcxx . . $ kj/ArrayPtr#end(155dc7f103cdb9db).() - bufferPoscxx . . $ kj/BufferedOutputStreamWrapper#bufferPos.;
218
219 if (size <= available) {
220 memcpy(bufferPoscxx . . $ kj/BufferedOutputStreamWrapper#bufferPos., src, size);
221 bufferPoscxx . . $ kj/BufferedOutputStreamWrapper#bufferPos. += size;
222 } else if (size <= buffercxx . . $ kj/BufferedOutputStreamWrapper#buffer..sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).()) {
223 // Too much for this buffer, but not a full buffer's worth, so we'll go ahead and copy.
224 memcpy(bufferPoscxx . . $ kj/BufferedOutputStreamWrapper#bufferPos., src, available);
225 innercxx . . $ kj/BufferedOutputStreamWrapper#inner..writecxx . . $ kj/OutputStream#write(8c7266571c1d65cc).(buffercxx . . $ kj/BufferedOutputStreamWrapper#buffer..begincxx . . $ kj/ArrayPtr#begin(155dc7f103cdb9db).(), buffercxx . . $ kj/BufferedOutputStreamWrapper#buffer..sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).());
226
227 size -= available;
228 src = reinterpret_cast<const bytecxx . . $ kj/byte#*>(src) + available;
229
230 memcpy(buffercxx . . $ kj/BufferedOutputStreamWrapper#buffer..begincxx . . $ kj/ArrayPtr#begin(155dc7f103cdb9db).(), src, size);
231 bufferPoscxx . . $ kj/BufferedOutputStreamWrapper#bufferPos. = buffercxx . . $ kj/BufferedOutputStreamWrapper#buffer..begincxx . . $ kj/ArrayPtr#begin(155dc7f103cdb9db).() + size;
232 } else {
233 // Writing so much data that we might as well write directly to avoid a copy.
234 innercxx . . $ kj/BufferedOutputStreamWrapper#inner..writecxx . . $ kj/OutputStream#write(8c7266571c1d65cc).(buffercxx . . $ kj/BufferedOutputStreamWrapper#buffer..begincxx . . $ kj/ArrayPtr#begin(155dc7f103cdb9db).(), bufferPoscxx . . $ kj/BufferedOutputStreamWrapper#bufferPos. - buffercxx . . $ kj/BufferedOutputStreamWrapper#buffer..begincxx . . $ kj/ArrayPtr#begin(155dc7f103cdb9db).());
235 bufferPoscxx . . $ kj/BufferedOutputStreamWrapper#bufferPos. = buffercxx . . $ kj/BufferedOutputStreamWrapper#buffer..begincxx . . $ kj/ArrayPtr#begin(155dc7f103cdb9db).();
236 innercxx . . $ kj/BufferedOutputStreamWrapper#inner..writecxx . . $ kj/OutputStream#write(8c7266571c1d65cc).(src, size);
237 }
238 }
239 }
240
241 // =======================================================================================
242
243 ArrayInputStreamcxx . . $ kj/ArrayInputStream#::ArrayInputStreamcxx . . $ kj/ArrayInputStream#ArrayInputStream(30ede09ecd811094).(ArrayPtrcxx . . $ kj/ArrayPtr#<const bytecxx . . $ kj/byte#> array): arraycxx . . $ kj/ArrayInputStream#array.(array) {}
244 ArrayInputStreamcxx . . $ kj/ArrayInputStream#::~cxx . . $ kj/ArrayInputStream#`~ArrayInputStream`(49f6e7a06ebc5aa8).ArrayInputStreamcxx . . $ kj/ArrayInputStream#() noexcept(false) {}
245
246 ArrayPtrcxx . . $ kj/ArrayPtr#<const bytecxx . . $ kj/byte#> ArrayInputStreamcxx . . $ kj/ArrayInputStream#::tryGetReadBuffercxx . . $ kj/ArrayInputStream#tryGetReadBuffer(9d2ad03af55baf09).() {
247 return arraycxx . . $ kj/ArrayInputStream#array.;
248 }
249
250 size_t ArrayInputStreamcxx . . $ kj/ArrayInputStream#::tryReadcxx . . $ kj/ArrayInputStream#tryRead(e2eaf0fd878a292f).(void* dst, size_t minBytes, size_t maxBytes) {
251 size_t n = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).(maxBytes, arraycxx . . $ kj/ArrayInputStream#array..sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).());
252 memcpy(dst, arraycxx . . $ kj/ArrayInputStream#array..begincxx . . $ kj/ArrayPtr#begin(155dc7f103cdb9db).(), n);
253 arraycxx . . $ kj/ArrayInputStream#array. =cxx . . $ kj/ArrayPtr#`operator=`(596308cff75d5c06). arraycxx . . $ kj/ArrayInputStream#array..slicecxx . . $ kj/ArrayPtr#slice(2c7e4e1cf603d8fe).(n, arraycxx . . $ kj/ArrayInputStream#array..sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).());
254 return n;
255 }
256
257 void ArrayInputStreamcxx . . $ kj/ArrayInputStream#::skipcxx . . $ kj/ArrayInputStream#skip(2c44cd906ae4e829).(size_t bytes) {
258 KJ_REQUIREcxx . . $ `src/kj/debug.h:227:9`!(array.size() >= bytes, "ArrayInputStream ended prematurely.") {
259 bytes = arraycxx . . $ kj/ArrayInputStream#array..sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).();
260 break;
261 }
262 arraycxx . . $ kj/ArrayInputStream#array. =cxx . . $ kj/ArrayPtr#`operator=`(596308cff75d5c06). arraycxx . . $ kj/ArrayInputStream#array..slicecxx . . $ kj/ArrayPtr#slice(2c7e4e1cf603d8fe).(bytes, arraycxx . . $ kj/ArrayInputStream#array..sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).());
263 }
264
265 // -------------------------------------------------------------------
266
267 ArrayOutputStreamcxx . . $ kj/ArrayOutputStream#::ArrayOutputStreamcxx . . $ kj/ArrayOutputStream#ArrayOutputStream(263fd99fb500e72e).(ArrayPtrcxx . . $ kj/ArrayPtr#<bytecxx . . $ kj/byte#> array): arraycxx . . $ kj/ArrayOutputStream#array.(array), fillPoscxx . . $ kj/ArrayOutputStream#fillPos.(array.begincxx . . $ kj/ArrayPtr#begin(155dc7f103cdb9db).()) {}
268 ArrayOutputStreamcxx . . $ kj/ArrayOutputStream#::~cxx . . $ kj/ArrayOutputStream#`~ArrayOutputStream`(49f6e7a06ebc5aa8).ArrayOutputStreamcxx . . $ kj/ArrayOutputStream#() noexcept(false) {}
269
270 ArrayPtrcxx . . $ kj/ArrayPtr#<bytecxx . . $ kj/byte#> ArrayOutputStreamcxx . . $ kj/ArrayOutputStream#::getWriteBuffercxx . . $ kj/ArrayOutputStream#getWriteBuffer(fc5425f282210c4e).() {
271 return arrayPtrcxx . . $ kj/arrayPtr(25656fe11b4c7c4b).(fillPoscxx . . $ kj/ArrayOutputStream#fillPos., arraycxx . . $ kj/ArrayOutputStream#array..endcxx . . $ kj/ArrayPtr#end(155dc7f103cdb9db).());
272 }
273
274 void ArrayOutputStreamcxx . . $ kj/ArrayOutputStream#::writecxx . . $ kj/ArrayOutputStream#write(8c7266571c1d65cc).(const void* src, size_t size) {
275 if (src == fillPoscxx . . $ kj/ArrayOutputStream#fillPos. && fillPoscxx . . $ kj/ArrayOutputStream#fillPos. != arraycxx . . $ kj/ArrayOutputStream#array..endcxx . . $ kj/ArrayPtr#end(155dc7f103cdb9db).()) {
276 // Oh goody, the caller wrote directly into our buffer.
277 KJ_REQUIREcxx . . $ `src/kj/debug.h:227:9`!(size <= array.end() - fillPos, size, fillPos, array.end() - fillPos);
278 fillPoscxx . . $ kj/ArrayOutputStream#fillPos. += size;
279 } else {
280 KJ_REQUIREcxx . . $ `src/kj/debug.h:227:9`!(size <= (size_t)(array.end() - fillPos),
281 "ArrayOutputStream's backing array was not large enough for the data written.");
282 memcpy(fillPoscxx . . $ kj/ArrayOutputStream#fillPos., src, size);
283 fillPoscxx . . $ kj/ArrayOutputStream#fillPos. += size;
284 }
285 }
286
287 // -------------------------------------------------------------------
288
289 VectorOutputStreamcxx . . $ kj/VectorOutputStream#::VectorOutputStreamcxx . . $ kj/VectorOutputStream#VectorOutputStream(2c44cd906ae4e829).(size_t initialCapacity)
290 : vectorcxx . . $ kj/VectorOutputStream#vector.(heapArraycxx . . $ kj/heapArray(30935e9a1d746cdf).<bytecxx . . $ kj/byte#>(initialCapacity)), fillPoscxx . . $ kj/VectorOutputStream#fillPos.(vectorcxx . . $ kj/VectorOutputStream#vector..begincxx . . $ kj/Array#begin(155dc7f103cdb9db).()) {}
291 VectorOutputStreamcxx . . $ kj/VectorOutputStream#::~cxx . . $ kj/VectorOutputStream#`~VectorOutputStream`(49f6e7a06ebc5aa8).VectorOutputStreamcxx . . $ kj/VectorOutputStream#() noexcept(false) {}
292
293 ArrayPtrcxx . . $ kj/ArrayPtr#<bytecxx . . $ kj/byte#> VectorOutputStreamcxx . . $ kj/VectorOutputStream#::getWriteBuffercxx . . $ kj/VectorOutputStream#getWriteBuffer(fc5425f282210c4e).() {
294 // Grow if needed.
295 if (fillPoscxx . . $ kj/VectorOutputStream#fillPos. == vectorcxx . . $ kj/VectorOutputStream#vector..endcxx . . $ kj/Array#end(155dc7f103cdb9db).()) {
296 growcxx . . $ kj/VectorOutputStream#grow(2c44cd906ae4e829).(vectorcxx . . $ kj/VectorOutputStream#vector..sizecxx . . $ kj/Array#size(fe4d2d262d4164a8).() + 1);
297 }
298
299 return arrayPtrcxx . . $ kj/arrayPtr(25656fe11b4c7c4b).(fillPoscxx . . $ kj/VectorOutputStream#fillPos., vectorcxx . . $ kj/VectorOutputStream#vector..endcxx . . $ kj/Array#end(155dc7f103cdb9db).());
300 }
301
302 void VectorOutputStreamcxx . . $ kj/VectorOutputStream#::writecxx . . $ kj/VectorOutputStream#write(8c7266571c1d65cc).(const void* src, size_t size) {
303 if (src == fillPoscxx . . $ kj/VectorOutputStream#fillPos. && fillPoscxx . . $ kj/VectorOutputStream#fillPos. != vectorcxx . . $ kj/VectorOutputStream#vector..endcxx . . $ kj/Array#end(155dc7f103cdb9db).()) {
304 // Oh goody, the caller wrote directly into our buffer.
305 KJ_REQUIREcxx . . $ `src/kj/debug.h:227:9`!(size <= vector.end() - fillPos, size, fillPos, vector.end() - fillPos);
306 fillPoscxx . . $ kj/VectorOutputStream#fillPos. += size;
307 } else {
308 if (vectorcxx . . $ kj/VectorOutputStream#vector..endcxx . . $ kj/Array#end(155dc7f103cdb9db).() - fillPoscxx . . $ kj/VectorOutputStream#fillPos. < size) {
309 growcxx . . $ kj/VectorOutputStream#grow(2c44cd906ae4e829).(fillPoscxx . . $ kj/VectorOutputStream#fillPos. - vectorcxx . . $ kj/VectorOutputStream#vector..begincxx . . $ kj/Array#begin(155dc7f103cdb9db).() + size);
310 }
311
312 memcpy(fillPoscxx . . $ kj/VectorOutputStream#fillPos., src, size);
313 fillPoscxx . . $ kj/VectorOutputStream#fillPos. += size;
314 }
315 }
316
317 void VectorOutputStreamcxx . . $ kj/VectorOutputStream#::growcxx . . $ kj/VectorOutputStream#grow(2c44cd906ae4e829).(size_t minSize) {
318 size_t newSize = vectorcxx . . $ kj/VectorOutputStream#vector..sizecxx . . $ kj/Array#size(fe4d2d262d4164a8).() * 2;
319 while (newSize < minSize) newSize *= 2;
320 auto newVector = heapArraycxx . . $ kj/heapArray(30935e9a1d746cdf).<bytecxx . . $ kj/byte#>(newSize);
321 memcpy(newVector.begincxx . . $ kj/Array#begin(155dc7f103cdb9db).(), vectorcxx . . $ kj/VectorOutputStream#vector..begincxx . . $ kj/Array#begin(155dc7f103cdb9db).(), fillPoscxx . . $ kj/VectorOutputStream#fillPos. - vectorcxx . . $ kj/VectorOutputStream#vector..begincxx . . $ kj/Array#begin(155dc7f103cdb9db).());
322 fillPoscxx . . $ kj/VectorOutputStream#fillPos. = fillPoscxx . . $ kj/VectorOutputStream#fillPos. - vectorcxx . . $ kj/VectorOutputStream#vector..begincxx . . $ kj/Array#begin(155dc7f103cdb9db).() + newVector.begincxx . . $ kj/Array#begin(155dc7f103cdb9db).();
323 vectorcxx . . $ kj/VectorOutputStream#vector. =cxx . . $ kj/Array#`operator=`(4e73d5a16a77c634). kjcxx . . $ kj/::mvcxx . . $ kj/mv(9768e8ef939d477).(newVector);
324 }
325
326 // =======================================================================================
327
328 AutoCloseFdcxx . . $ kj/AutoCloseFd#::~cxx . . $ kj/AutoCloseFd#`~AutoCloseFd`(49f6e7a06ebc5aa8).AutoCloseFdcxx . . $ kj/AutoCloseFd#() noexcept(false) {
329 if (fdcxx . . $ kj/AutoCloseFd#fd. >= 0) {
330 // Don't use SYSCALL() here because close() should not be repeated on EINTR.
331 if (miniposixcxx . . $ kj/miniposix/::closecxx . . $ kj/miniposix/close(7864480464b09eea).(fdcxx . . $ kj/AutoCloseFd#fd.) < 0) {
332 KJ_FAIL_SYSCALLcxx . . $ `src/kj/debug.h:246:9`!("close", errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!, fd) {
333 // This ensures we don't throw an exception if unwinding.
334 break;
335 }
336 }
337 }
338 }
339
340 FdInputStreamcxx . . $ kj/FdInputStream#::~cxx . . $ kj/FdInputStream#`~FdInputStream`(49f6e7a06ebc5aa8).FdInputStreamcxx . . $ kj/FdInputStream#() noexcept(false) {}
341
342 size_t FdInputStreamcxx . . $ kj/FdInputStream#::tryReadcxx . . $ kj/FdInputStream#tryRead(e2eaf0fd878a292f).(void* buffer, size_t minBytes, size_t maxBytes) {
343 bytecxx . . $ kj/byte#* pos = reinterpret_cast<bytecxx . . $ kj/byte#*>(buffer);
344 bytecxx . . $ kj/byte#* min = pos + minBytes;
345 bytecxx . . $ kj/byte#* max = pos + maxBytes;
346
347 while (pos < min) {
348 miniposixcxx . . $ kj/miniposix/::ssize_tcxx . . $ kj/miniposix/ssize_t# n;
349 KJ_SYSCALLcxx . . $ `src/kj/debug.h:236:9`!(n = miniposix::read(fd, pos, max - pos), fd);
350 if (n == 0) {
351 break;
352 }
353 pos += n;
354 }
355
356 return pos - reinterpret_cast<bytecxx . . $ kj/byte#*>(buffer);
357 }
358
359 FdOutputStreamcxx . . $ kj/FdOutputStream#::~cxx . . $ kj/FdOutputStream#`~FdOutputStream`(49f6e7a06ebc5aa8).FdOutputStreamcxx . . $ kj/FdOutputStream#() noexcept(false) {}
360
361 #if _WIN32 || __APPLE__
362 // On platforms that can't handle writes of more than INT_MAX (e.g. Windows, Mac), we will clamp
363 // writes to this size. We use 1GB rather than INT_MAX since INT_MAX is 2GB minus 1 byte. Being
364 // off by one from a big round number feels rude (alignment issues may harm performance), and 1GB
365 // should be plenty in any case.
366 static constexpr size_t WRITE_CLAMP_SIZE = 1u << 30;
367 #endif
368
369 void FdOutputStreamcxx . . $ kj/FdOutputStream#::writecxx . . $ kj/FdOutputStream#write(8c7266571c1d65cc).(const void* buffer, size_t size) {
370 const char* pos = reinterpret_cast<const char*>(buffer);
371
372 while (size > 0) {
373 miniposixcxx . . $ kj/miniposix/::ssize_tcxx . . $ kj/miniposix/ssize_t# n;
374 #if _WIN32 || __APPLE__
375 // Windows and Mac suffer from bugs in which they will fail if given more than INT_MAX bytes
376 // in a single write operation. We can just clamp the size since the loop will then handle
377 // writing the rest. I don't know why these platforms don't just do this themselves.
378 KJ_SYSCALL(n = miniposix::write(fd, pos, kj::min(size, WRITE_CLAMP_SIZE)), fd);
379 #else
380 KJ_SYSCALLcxx . . $ `src/kj/debug.h:236:9`!(n = miniposix::write(fd, pos, size), fd);
381 #endif
382 KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(n > 0, "write() returned zero.");
383 pos += n;
384 size -= n;
385 }
386 }
387
388 void FdOutputStreamcxx . . $ kj/FdOutputStream#::writecxx . . $ kj/FdOutputStream#write(9a8c2cc5c087affd).(ArrayPtrcxx . . $ kj/ArrayPtr#<const ArrayPtrcxx . . $ kj/ArrayPtr#<const bytecxx . . $ kj/byte#>> pieces) {
389 #if _WIN32
390 // Windows has no reasonable writev(). It has WriteFileGather, but this call has the unreasonable
391 // restriction that each segment must be page-aligned. So, fall back to the default implementation
392
393 OutputStream::write(pieces);
394
395 #else
396 const size_t iovmax = miniposixcxx . . $ kj/miniposix/::iovMaxcxx . . $ kj/miniposix/iovMax(f708dce9a8856c70).();
397 while (pieces.sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).() > iovmax) {
398 writecxx . . $ kj/FdOutputStream#write(9a8c2cc5c087affd).(pieces.slicecxx . . $ kj/ArrayPtr#slice(2c7e4e1cf603d8fe).(0, iovmax));
399 pieces =cxx . . $ kj/ArrayPtr#`operator=`(1db1f54bc7b38063). pieces.slicecxx . . $ kj/ArrayPtr#slice(2c7e4e1cf603d8fe).(iovmax, pieces.sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).());
400 }
401
402 KJ_STACK_ARRAYcxx . . $ `src/kj/common.h:406:9`!(struct iovec, iov, pieces.size(), 16, 128);
403
404 for (uintcxx . . $ kj/uint# i = 0; i < pieces.sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).(); i++) {
405 // writev() interface is not const-correct. :(
406 iov[cxx . . $ kj/ArrayPtr#`operator[]`(ec43b8547ac5c60).i].iov_basecxx . . $ iovec#iov_base. = const_cast<bytecxx . . $ kj/byte#*>(pieces[cxx . . $ kj/ArrayPtr#`operator[]`(ec43b8547ac5c60).i].begincxx . . $ kj/ArrayPtr#begin(1016ee5406d91ab9).());
407 iov[cxx . . $ kj/ArrayPtr#`operator[]`(ec43b8547ac5c60).i].iov_lencxx . . $ iovec#iov_len. = pieces[cxx . . $ kj/ArrayPtr#`operator[]`(ec43b8547ac5c60).i].sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).();
408 }
409
410 struct ioveccxx . . $ iovec#* current = iov.begincxx . . $ kj/ArrayPtr#begin(155dc7f103cdb9db).();
411
412 // Advance past any leading empty buffers so that a write full of only empty buffers does not
413 // cause a syscall at all.
414 while (current < iov.endcxx . . $ kj/ArrayPtr#end(155dc7f103cdb9db).() && current->iov_lencxx . . $ iovec#iov_len. == 0) {
415 ++current;
416 }
417
418 while (current < iov.endcxx . . $ kj/ArrayPtr#end(155dc7f103cdb9db).()) {
419 size_t iovCount = iov.endcxx . . $ kj/ArrayPtr#end(155dc7f103cdb9db).() - current;
420
421 #if __APPLE__
422 // MacOS will fail if you give it more than INT_MAX bytes to write at once. We can solve this
423 // by carefully truncating the list to a lesser number. Why the OS doesn't just return a short
424 // write itself, I don't know.
425 size_t totalSize = 0;
426 struct iovec* editedPiece = nullptr;
427 size_t editedPieceOriginalLen = 0;
428 for (auto i: kj::zeroTo(iovCount)) {
429 auto& piece = *(current + i);
430 totalSize += piece.iov_len;
431 if (totalSize >= WRITE_CLAMP_SIZE) {
432 // Truncate the list after this piece.
433 iovCount = i + 1;
434
435 if (totalSize > WRITE_CLAMP_SIZE) {
436 // We also have to truncate this piece. Patch it in-place and plan to fix it later.
437 editedPiece = &piece;
438 editedPieceOriginalLen = piece.iov_len;
439 size_t overage = totalSize - WRITE_CLAMP_SIZE;
440 piece.iov_len -= overage;
441 }
442
443 break;
444 }
445 }
446 #endif
447
448 // Issue the write.
449 ssize_t n = 0;
450 KJ_SYSCALLcxx . . $ `src/kj/debug.h:236:9`!(n = ::writev(fd, current, iovCount), fd);
451 KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(n > 0, "writev() returned zero.");
452
453 #if __APPLE__
454 // If we patched the list above, unpatch now.
455 if (editedPiece != nullptr) {
456 editedPiece->iov_len = editedPieceOriginalLen;
457 }
458 #endif
459
460 // Advance past all buffers that were fully-written.
461 while (current < iov.endcxx . . $ kj/ArrayPtr#end(155dc7f103cdb9db).() && static_cast<size_t>(n) >= current->iov_lencxx . . $ iovec#iov_len.) {
462 n -= current->iov_lencxx . . $ iovec#iov_len.;
463 ++current;
464 }
465
466 // If we only partially-wrote one of the buffers, adjust the pointer and size to include only
467 // the unwritten part.
468 if (n > 0) {
469 current->iov_basecxx . . $ iovec#iov_base. = reinterpret_cast<bytecxx . . $ kj/byte#*>(current->iov_basecxx . . $ iovec#iov_base.) + n;
470 current->iov_lencxx . . $ iovec#iov_len. -= n;
471 }
472 }
473 #endif
474 }
475
476 // =======================================================================================
477
478 #if _WIN32
479
480 AutoCloseHandle::~AutoCloseHandle() noexcept(false) {
481 if (handle != (void*)-1) {
482 KJ_WIN32(CloseHandle(handle));
483 }
484 }
485
486 HandleInputStream::~HandleInputStream() noexcept(false) {}
487
488 size_t HandleInputStream::tryRead(void* buffer, size_t minBytes, size_t maxBytes) {
489 byte* pos = reinterpret_cast<byte*>(buffer);
490 byte* min = pos + minBytes;
491 byte* max = pos + maxBytes;
492
493 while (pos < min) {
494 DWORD n;
495 KJ_WIN32(ReadFile(handle, pos, kj::min(max - pos, DWORD(kj::maxValue)), &n, nullptr));
496 if (n == 0) {
497 break;
498 }
499 pos += n;
500 }
501
502 return pos - reinterpret_cast<byte*>(buffer);
503 }
504
505 HandleOutputStream::~HandleOutputStream() noexcept(false) {}
506
507 void HandleOutputStream::write(const void* buffer, size_t size) {
508 const char* pos = reinterpret_cast<const char*>(buffer);
509
510 while (size > 0) {
511 DWORD n;
512 KJ_WIN32(WriteFile(handle, pos, kj::min(size, DWORD(kj::maxValue)), &n, nullptr));
513 KJ_ASSERT(n > 0, "write() returned zero.");
514 pos += n;
515 size -= n;
516 }
517 }
518
519 #endif // _WIN32
520
521 } // namespace kj