Chris@63
|
1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
|
Chris@63
|
2 // Licensed under the MIT License:
|
Chris@63
|
3 //
|
Chris@63
|
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
Chris@63
|
5 // of this software and associated documentation files (the "Software"), to deal
|
Chris@63
|
6 // in the Software without restriction, including without limitation the rights
|
Chris@63
|
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
Chris@63
|
8 // copies of the Software, and to permit persons to whom the Software is
|
Chris@63
|
9 // furnished to do so, subject to the following conditions:
|
Chris@63
|
10 //
|
Chris@63
|
11 // The above copyright notice and this permission notice shall be included in
|
Chris@63
|
12 // all copies or substantial portions of the Software.
|
Chris@63
|
13 //
|
Chris@63
|
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
Chris@63
|
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
Chris@63
|
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
Chris@63
|
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
Chris@63
|
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
Chris@63
|
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
Chris@63
|
20 // THE SOFTWARE.
|
Chris@63
|
21
|
Chris@63
|
22 #ifndef KJ_IO_H_
|
Chris@63
|
23 #define KJ_IO_H_
|
Chris@63
|
24
|
Chris@63
|
25 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS
|
Chris@63
|
26 #pragma GCC system_header
|
Chris@63
|
27 #endif
|
Chris@63
|
28
|
Chris@63
|
29 #include <stddef.h>
|
Chris@63
|
30 #include "common.h"
|
Chris@63
|
31 #include "array.h"
|
Chris@63
|
32 #include "exception.h"
|
Chris@63
|
33
|
Chris@63
|
34 namespace kj {
|
Chris@63
|
35
|
Chris@63
|
36 // =======================================================================================
|
Chris@63
|
37 // Abstract interfaces
|
Chris@63
|
38
|
Chris@63
|
39 class InputStream {
|
Chris@63
|
40 public:
|
Chris@63
|
41 virtual ~InputStream() noexcept(false);
|
Chris@63
|
42
|
Chris@63
|
43 size_t read(void* buffer, size_t minBytes, size_t maxBytes);
|
Chris@63
|
44 // Reads at least minBytes and at most maxBytes, copying them into the given buffer. Returns
|
Chris@63
|
45 // the size read. Throws an exception on errors. Implemented in terms of tryRead().
|
Chris@63
|
46 //
|
Chris@63
|
47 // maxBytes is the number of bytes the caller really wants, but minBytes is the minimum amount
|
Chris@63
|
48 // needed by the caller before it can start doing useful processing. If the stream returns less
|
Chris@63
|
49 // than maxBytes, the caller will usually call read() again later to get the rest. Returning
|
Chris@63
|
50 // less than maxBytes is useful when it makes sense for the caller to parallelize processing
|
Chris@63
|
51 // with I/O.
|
Chris@63
|
52 //
|
Chris@63
|
53 // Never blocks if minBytes is zero. If minBytes is zero and maxBytes is non-zero, this may
|
Chris@63
|
54 // attempt a non-blocking read or may just return zero. To force a read, use a non-zero minBytes.
|
Chris@63
|
55 // To detect EOF without throwing an exception, use tryRead().
|
Chris@63
|
56 //
|
Chris@63
|
57 // If the InputStream can't produce minBytes, it MUST throw an exception, as the caller is not
|
Chris@63
|
58 // expected to understand how to deal with partial reads.
|
Chris@63
|
59
|
Chris@63
|
60 virtual size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) = 0;
|
Chris@63
|
61 // Like read(), but may return fewer than minBytes on EOF.
|
Chris@63
|
62
|
Chris@63
|
63 inline void read(void* buffer, size_t bytes) { read(buffer, bytes, bytes); }
|
Chris@63
|
64 // Convenience method for reading an exact number of bytes.
|
Chris@63
|
65
|
Chris@63
|
66 virtual void skip(size_t bytes);
|
Chris@63
|
67 // Skips past the given number of bytes, discarding them. The default implementation read()s
|
Chris@63
|
68 // into a scratch buffer.
|
Chris@63
|
69 };
|
Chris@63
|
70
|
Chris@63
|
71 class OutputStream {
|
Chris@63
|
72 public:
|
Chris@63
|
73 virtual ~OutputStream() noexcept(false);
|
Chris@63
|
74
|
Chris@63
|
75 virtual void write(const void* buffer, size_t size) = 0;
|
Chris@63
|
76 // Always writes the full size. Throws exception on error.
|
Chris@63
|
77
|
Chris@63
|
78 virtual void write(ArrayPtr<const ArrayPtr<const byte>> pieces);
|
Chris@63
|
79 // Equivalent to write()ing each byte array in sequence, which is what the default implementation
|
Chris@63
|
80 // does. Override if you can do something better, e.g. use writev() to do the write in a single
|
Chris@63
|
81 // syscall.
|
Chris@63
|
82 };
|
Chris@63
|
83
|
Chris@63
|
84 class BufferedInputStream: public InputStream {
|
Chris@63
|
85 // An input stream which buffers some bytes in memory to reduce system call overhead.
|
Chris@63
|
86 // - OR -
|
Chris@63
|
87 // An input stream that actually reads from some in-memory data structure and wants to give its
|
Chris@63
|
88 // caller a direct pointer to that memory to potentially avoid a copy.
|
Chris@63
|
89
|
Chris@63
|
90 public:
|
Chris@63
|
91 virtual ~BufferedInputStream() noexcept(false);
|
Chris@63
|
92
|
Chris@63
|
93 ArrayPtr<const byte> getReadBuffer();
|
Chris@63
|
94 // Get a direct pointer into the read buffer, which contains the next bytes in the input. If the
|
Chris@63
|
95 // caller consumes any bytes, it should then call skip() to indicate this. This always returns a
|
Chris@63
|
96 // non-empty buffer or throws an exception. Implemented in terms of tryGetReadBuffer().
|
Chris@63
|
97
|
Chris@63
|
98 virtual ArrayPtr<const byte> tryGetReadBuffer() = 0;
|
Chris@63
|
99 // Like getReadBuffer() but may return an empty buffer on EOF.
|
Chris@63
|
100 };
|
Chris@63
|
101
|
Chris@63
|
102 class BufferedOutputStream: public OutputStream {
|
Chris@63
|
103 // An output stream which buffers some bytes in memory to reduce system call overhead.
|
Chris@63
|
104 // - OR -
|
Chris@63
|
105 // An output stream that actually writes into some in-memory data structure and wants to give its
|
Chris@63
|
106 // caller a direct pointer to that memory to potentially avoid a copy.
|
Chris@63
|
107
|
Chris@63
|
108 public:
|
Chris@63
|
109 virtual ~BufferedOutputStream() noexcept(false);
|
Chris@63
|
110
|
Chris@63
|
111 virtual ArrayPtr<byte> getWriteBuffer() = 0;
|
Chris@63
|
112 // Get a direct pointer into the write buffer. The caller may choose to fill in some prefix of
|
Chris@63
|
113 // this buffer and then pass it to write(), in which case write() may avoid a copy. It is
|
Chris@63
|
114 // incorrect to pass to write any slice of this buffer which is not a prefix.
|
Chris@63
|
115 };
|
Chris@63
|
116
|
Chris@63
|
117 // =======================================================================================
|
Chris@63
|
118 // Buffered streams implemented as wrappers around regular streams
|
Chris@63
|
119
|
Chris@63
|
120 class BufferedInputStreamWrapper: public BufferedInputStream {
|
Chris@63
|
121 // Implements BufferedInputStream in terms of an InputStream.
|
Chris@63
|
122 //
|
Chris@63
|
123 // Note that the underlying stream's position is unpredictable once the wrapper is destroyed,
|
Chris@63
|
124 // unless the entire stream was consumed. To read a predictable number of bytes in a buffered
|
Chris@63
|
125 // way without going over, you'd need this wrapper to wrap some other wrapper which itself
|
Chris@63
|
126 // implements an artificial EOF at the desired point. Such a stream should be trivial to write
|
Chris@63
|
127 // but is not provided by the library at this time.
|
Chris@63
|
128
|
Chris@63
|
129 public:
|
Chris@63
|
130 explicit BufferedInputStreamWrapper(InputStream& inner, ArrayPtr<byte> buffer = nullptr);
|
Chris@63
|
131 // Creates a buffered stream wrapping the given non-buffered stream. No guarantee is made about
|
Chris@63
|
132 // the position of the inner stream after a buffered wrapper has been created unless the entire
|
Chris@63
|
133 // input is read.
|
Chris@63
|
134 //
|
Chris@63
|
135 // If the second parameter is non-null, the stream uses the given buffer instead of allocating
|
Chris@63
|
136 // its own. This may improve performance if the buffer can be reused.
|
Chris@63
|
137
|
Chris@63
|
138 KJ_DISALLOW_COPY(BufferedInputStreamWrapper);
|
Chris@63
|
139 ~BufferedInputStreamWrapper() noexcept(false);
|
Chris@63
|
140
|
Chris@63
|
141 // implements BufferedInputStream ----------------------------------
|
Chris@63
|
142 ArrayPtr<const byte> tryGetReadBuffer() override;
|
Chris@63
|
143 size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override;
|
Chris@63
|
144 void skip(size_t bytes) override;
|
Chris@63
|
145
|
Chris@63
|
146 private:
|
Chris@63
|
147 InputStream& inner;
|
Chris@63
|
148 Array<byte> ownedBuffer;
|
Chris@63
|
149 ArrayPtr<byte> buffer;
|
Chris@63
|
150 ArrayPtr<byte> bufferAvailable;
|
Chris@63
|
151 };
|
Chris@63
|
152
|
Chris@63
|
153 class BufferedOutputStreamWrapper: public BufferedOutputStream {
|
Chris@63
|
154 // Implements BufferedOutputStream in terms of an OutputStream. Note that writes to the
|
Chris@63
|
155 // underlying stream may be delayed until flush() is called or the wrapper is destroyed.
|
Chris@63
|
156
|
Chris@63
|
157 public:
|
Chris@63
|
158 explicit BufferedOutputStreamWrapper(OutputStream& inner, ArrayPtr<byte> buffer = nullptr);
|
Chris@63
|
159 // Creates a buffered stream wrapping the given non-buffered stream.
|
Chris@63
|
160 //
|
Chris@63
|
161 // If the second parameter is non-null, the stream uses the given buffer instead of allocating
|
Chris@63
|
162 // its own. This may improve performance if the buffer can be reused.
|
Chris@63
|
163
|
Chris@63
|
164 KJ_DISALLOW_COPY(BufferedOutputStreamWrapper);
|
Chris@63
|
165 ~BufferedOutputStreamWrapper() noexcept(false);
|
Chris@63
|
166
|
Chris@63
|
167 void flush();
|
Chris@63
|
168 // Force the wrapper to write any remaining bytes in its buffer to the inner stream. Note that
|
Chris@63
|
169 // this only flushes this object's buffer; this object has no idea how to flush any other buffers
|
Chris@63
|
170 // that may be present in the underlying stream.
|
Chris@63
|
171
|
Chris@63
|
172 // implements BufferedOutputStream ---------------------------------
|
Chris@63
|
173 ArrayPtr<byte> getWriteBuffer() override;
|
Chris@63
|
174 void write(const void* buffer, size_t size) override;
|
Chris@63
|
175
|
Chris@63
|
176 private:
|
Chris@63
|
177 OutputStream& inner;
|
Chris@63
|
178 Array<byte> ownedBuffer;
|
Chris@63
|
179 ArrayPtr<byte> buffer;
|
Chris@63
|
180 byte* bufferPos;
|
Chris@63
|
181 UnwindDetector unwindDetector;
|
Chris@63
|
182 };
|
Chris@63
|
183
|
Chris@63
|
184 // =======================================================================================
|
Chris@63
|
185 // Array I/O
|
Chris@63
|
186
|
Chris@63
|
187 class ArrayInputStream: public BufferedInputStream {
|
Chris@63
|
188 public:
|
Chris@63
|
189 explicit ArrayInputStream(ArrayPtr<const byte> array);
|
Chris@63
|
190 KJ_DISALLOW_COPY(ArrayInputStream);
|
Chris@63
|
191 ~ArrayInputStream() noexcept(false);
|
Chris@63
|
192
|
Chris@63
|
193 // implements BufferedInputStream ----------------------------------
|
Chris@63
|
194 ArrayPtr<const byte> tryGetReadBuffer() override;
|
Chris@63
|
195 size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override;
|
Chris@63
|
196 void skip(size_t bytes) override;
|
Chris@63
|
197
|
Chris@63
|
198 private:
|
Chris@63
|
199 ArrayPtr<const byte> array;
|
Chris@63
|
200 };
|
Chris@63
|
201
|
Chris@63
|
202 class ArrayOutputStream: public BufferedOutputStream {
|
Chris@63
|
203 public:
|
Chris@63
|
204 explicit ArrayOutputStream(ArrayPtr<byte> array);
|
Chris@63
|
205 KJ_DISALLOW_COPY(ArrayOutputStream);
|
Chris@63
|
206 ~ArrayOutputStream() noexcept(false);
|
Chris@63
|
207
|
Chris@63
|
208 ArrayPtr<byte> getArray() {
|
Chris@63
|
209 // Get the portion of the array which has been filled in.
|
Chris@63
|
210 return arrayPtr(array.begin(), fillPos);
|
Chris@63
|
211 }
|
Chris@63
|
212
|
Chris@63
|
213 // implements BufferedInputStream ----------------------------------
|
Chris@63
|
214 ArrayPtr<byte> getWriteBuffer() override;
|
Chris@63
|
215 void write(const void* buffer, size_t size) override;
|
Chris@63
|
216
|
Chris@63
|
217 private:
|
Chris@63
|
218 ArrayPtr<byte> array;
|
Chris@63
|
219 byte* fillPos;
|
Chris@63
|
220 };
|
Chris@63
|
221
|
Chris@63
|
222 class VectorOutputStream: public BufferedOutputStream {
|
Chris@63
|
223 public:
|
Chris@63
|
224 explicit VectorOutputStream(size_t initialCapacity = 4096);
|
Chris@63
|
225 KJ_DISALLOW_COPY(VectorOutputStream);
|
Chris@63
|
226 ~VectorOutputStream() noexcept(false);
|
Chris@63
|
227
|
Chris@63
|
228 ArrayPtr<byte> getArray() {
|
Chris@63
|
229 // Get the portion of the array which has been filled in.
|
Chris@63
|
230 return arrayPtr(vector.begin(), fillPos);
|
Chris@63
|
231 }
|
Chris@63
|
232
|
Chris@63
|
233 // implements BufferedInputStream ----------------------------------
|
Chris@63
|
234 ArrayPtr<byte> getWriteBuffer() override;
|
Chris@63
|
235 void write(const void* buffer, size_t size) override;
|
Chris@63
|
236
|
Chris@63
|
237 private:
|
Chris@63
|
238 Array<byte> vector;
|
Chris@63
|
239 byte* fillPos;
|
Chris@63
|
240
|
Chris@63
|
241 void grow(size_t minSize);
|
Chris@63
|
242 };
|
Chris@63
|
243
|
Chris@63
|
244 // =======================================================================================
|
Chris@63
|
245 // File descriptor I/O
|
Chris@63
|
246
|
Chris@63
|
247 class AutoCloseFd {
|
Chris@63
|
248 // A wrapper around a file descriptor which automatically closes the descriptor when destroyed.
|
Chris@63
|
249 // The wrapper supports move construction for transferring ownership of the descriptor. If
|
Chris@63
|
250 // close() returns an error, the destructor throws an exception, UNLESS the destructor is being
|
Chris@63
|
251 // called during unwind from another exception, in which case the close error is ignored.
|
Chris@63
|
252 //
|
Chris@63
|
253 // If your code is not exception-safe, you should not use AutoCloseFd. In this case you will
|
Chris@63
|
254 // have to call close() yourself and handle errors appropriately.
|
Chris@63
|
255
|
Chris@63
|
256 public:
|
Chris@63
|
257 inline AutoCloseFd(): fd(-1) {}
|
Chris@63
|
258 inline AutoCloseFd(decltype(nullptr)): fd(-1) {}
|
Chris@63
|
259 inline explicit AutoCloseFd(int fd): fd(fd) {}
|
Chris@63
|
260 inline AutoCloseFd(AutoCloseFd&& other) noexcept: fd(other.fd) { other.fd = -1; }
|
Chris@63
|
261 KJ_DISALLOW_COPY(AutoCloseFd);
|
Chris@63
|
262 ~AutoCloseFd() noexcept(false);
|
Chris@63
|
263
|
Chris@63
|
264 inline AutoCloseFd& operator=(AutoCloseFd&& other) {
|
Chris@63
|
265 AutoCloseFd old(kj::mv(*this));
|
Chris@63
|
266 fd = other.fd;
|
Chris@63
|
267 other.fd = -1;
|
Chris@63
|
268 return *this;
|
Chris@63
|
269 }
|
Chris@63
|
270
|
Chris@63
|
271 inline AutoCloseFd& operator=(decltype(nullptr)) {
|
Chris@63
|
272 AutoCloseFd old(kj::mv(*this));
|
Chris@63
|
273 return *this;
|
Chris@63
|
274 }
|
Chris@63
|
275
|
Chris@63
|
276 inline operator int() const { return fd; }
|
Chris@63
|
277 inline int get() const { return fd; }
|
Chris@63
|
278
|
Chris@63
|
279 operator bool() const = delete;
|
Chris@63
|
280 // Deleting this operator prevents accidental use in boolean contexts, which
|
Chris@63
|
281 // the int conversion operator above would otherwise allow.
|
Chris@63
|
282
|
Chris@63
|
283 inline bool operator==(decltype(nullptr)) { return fd < 0; }
|
Chris@63
|
284 inline bool operator!=(decltype(nullptr)) { return fd >= 0; }
|
Chris@63
|
285
|
Chris@63
|
286 private:
|
Chris@63
|
287 int fd;
|
Chris@63
|
288 UnwindDetector unwindDetector;
|
Chris@63
|
289 };
|
Chris@63
|
290
|
Chris@63
|
291 inline auto KJ_STRINGIFY(const AutoCloseFd& fd)
|
Chris@63
|
292 -> decltype(kj::toCharSequence(implicitCast<int>(fd))) {
|
Chris@63
|
293 return kj::toCharSequence(implicitCast<int>(fd));
|
Chris@63
|
294 }
|
Chris@63
|
295
|
Chris@63
|
296 class FdInputStream: public InputStream {
|
Chris@63
|
297 // An InputStream wrapping a file descriptor.
|
Chris@63
|
298
|
Chris@63
|
299 public:
|
Chris@63
|
300 explicit FdInputStream(int fd): fd(fd) {}
|
Chris@63
|
301 explicit FdInputStream(AutoCloseFd fd): fd(fd), autoclose(mv(fd)) {}
|
Chris@63
|
302 KJ_DISALLOW_COPY(FdInputStream);
|
Chris@63
|
303 ~FdInputStream() noexcept(false);
|
Chris@63
|
304
|
Chris@63
|
305 size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override;
|
Chris@63
|
306
|
Chris@63
|
307 inline int getFd() const { return fd; }
|
Chris@63
|
308
|
Chris@63
|
309 private:
|
Chris@63
|
310 int fd;
|
Chris@63
|
311 AutoCloseFd autoclose;
|
Chris@63
|
312 };
|
Chris@63
|
313
|
Chris@63
|
314 class FdOutputStream: public OutputStream {
|
Chris@63
|
315 // An OutputStream wrapping a file descriptor.
|
Chris@63
|
316
|
Chris@63
|
317 public:
|
Chris@63
|
318 explicit FdOutputStream(int fd): fd(fd) {}
|
Chris@63
|
319 explicit FdOutputStream(AutoCloseFd fd): fd(fd), autoclose(mv(fd)) {}
|
Chris@63
|
320 KJ_DISALLOW_COPY(FdOutputStream);
|
Chris@63
|
321 ~FdOutputStream() noexcept(false);
|
Chris@63
|
322
|
Chris@63
|
323 void write(const void* buffer, size_t size) override;
|
Chris@63
|
324 void write(ArrayPtr<const ArrayPtr<const byte>> pieces) override;
|
Chris@63
|
325
|
Chris@63
|
326 inline int getFd() const { return fd; }
|
Chris@63
|
327
|
Chris@63
|
328 private:
|
Chris@63
|
329 int fd;
|
Chris@63
|
330 AutoCloseFd autoclose;
|
Chris@63
|
331 };
|
Chris@63
|
332
|
Chris@63
|
333 // =======================================================================================
|
Chris@63
|
334 // Win32 Handle I/O
|
Chris@63
|
335
|
Chris@63
|
336 #ifdef _WIN32
|
Chris@63
|
337
|
Chris@63
|
338 class AutoCloseHandle {
|
Chris@63
|
339 // A wrapper around a Win32 HANDLE which automatically closes the handle when destroyed.
|
Chris@63
|
340 // The wrapper supports move construction for transferring ownership of the handle. If
|
Chris@63
|
341 // CloseHandle() returns an error, the destructor throws an exception, UNLESS the destructor is
|
Chris@63
|
342 // being called during unwind from another exception, in which case the close error is ignored.
|
Chris@63
|
343 //
|
Chris@63
|
344 // If your code is not exception-safe, you should not use AutoCloseHandle. In this case you will
|
Chris@63
|
345 // have to call close() yourself and handle errors appropriately.
|
Chris@63
|
346
|
Chris@63
|
347 public:
|
Chris@63
|
348 inline AutoCloseHandle(): handle((void*)-1) {}
|
Chris@63
|
349 inline AutoCloseHandle(decltype(nullptr)): handle((void*)-1) {}
|
Chris@63
|
350 inline explicit AutoCloseHandle(void* handle): handle(handle) {}
|
Chris@63
|
351 inline AutoCloseHandle(AutoCloseHandle&& other) noexcept: handle(other.handle) {
|
Chris@63
|
352 other.handle = (void*)-1;
|
Chris@63
|
353 }
|
Chris@63
|
354 KJ_DISALLOW_COPY(AutoCloseHandle);
|
Chris@63
|
355 ~AutoCloseHandle() noexcept(false);
|
Chris@63
|
356
|
Chris@63
|
357 inline AutoCloseHandle& operator=(AutoCloseHandle&& other) {
|
Chris@63
|
358 AutoCloseHandle old(kj::mv(*this));
|
Chris@63
|
359 handle = other.handle;
|
Chris@63
|
360 other.handle = (void*)-1;
|
Chris@63
|
361 return *this;
|
Chris@63
|
362 }
|
Chris@63
|
363
|
Chris@63
|
364 inline AutoCloseHandle& operator=(decltype(nullptr)) {
|
Chris@63
|
365 AutoCloseHandle old(kj::mv(*this));
|
Chris@63
|
366 return *this;
|
Chris@63
|
367 }
|
Chris@63
|
368
|
Chris@63
|
369 inline operator void*() const { return handle; }
|
Chris@63
|
370 inline void* get() const { return handle; }
|
Chris@63
|
371
|
Chris@63
|
372 operator bool() const = delete;
|
Chris@63
|
373 // Deleting this operator prevents accidental use in boolean contexts, which
|
Chris@63
|
374 // the void* conversion operator above would otherwise allow.
|
Chris@63
|
375
|
Chris@63
|
376 inline bool operator==(decltype(nullptr)) { return handle != (void*)-1; }
|
Chris@63
|
377 inline bool operator!=(decltype(nullptr)) { return handle == (void*)-1; }
|
Chris@63
|
378
|
Chris@63
|
379 private:
|
Chris@63
|
380 void* handle; // -1 (aka INVALID_HANDLE_VALUE) if not valid.
|
Chris@63
|
381 };
|
Chris@63
|
382
|
Chris@63
|
383 class HandleInputStream: public InputStream {
|
Chris@63
|
384 // An InputStream wrapping a Win32 HANDLE.
|
Chris@63
|
385
|
Chris@63
|
386 public:
|
Chris@63
|
387 explicit HandleInputStream(void* handle): handle(handle) {}
|
Chris@63
|
388 explicit HandleInputStream(AutoCloseHandle handle): handle(handle), autoclose(mv(handle)) {}
|
Chris@63
|
389 KJ_DISALLOW_COPY(HandleInputStream);
|
Chris@63
|
390 ~HandleInputStream() noexcept(false);
|
Chris@63
|
391
|
Chris@63
|
392 size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override;
|
Chris@63
|
393
|
Chris@63
|
394 private:
|
Chris@63
|
395 void* handle;
|
Chris@63
|
396 AutoCloseHandle autoclose;
|
Chris@63
|
397 };
|
Chris@63
|
398
|
Chris@63
|
399 class HandleOutputStream: public OutputStream {
|
Chris@63
|
400 // An OutputStream wrapping a Win32 HANDLE.
|
Chris@63
|
401
|
Chris@63
|
402 public:
|
Chris@63
|
403 explicit HandleOutputStream(void* handle): handle(handle) {}
|
Chris@63
|
404 explicit HandleOutputStream(AutoCloseHandle handle): handle(handle), autoclose(mv(handle)) {}
|
Chris@63
|
405 KJ_DISALLOW_COPY(HandleOutputStream);
|
Chris@63
|
406 ~HandleOutputStream() noexcept(false);
|
Chris@63
|
407
|
Chris@63
|
408 void write(const void* buffer, size_t size) override;
|
Chris@63
|
409
|
Chris@63
|
410 private:
|
Chris@63
|
411 void* handle;
|
Chris@63
|
412 AutoCloseHandle autoclose;
|
Chris@63
|
413 };
|
Chris@63
|
414
|
Chris@63
|
415 #endif // _WIN32
|
Chris@63
|
416
|
Chris@63
|
417 } // namespace kj
|
Chris@63
|
418
|
Chris@63
|
419 #endif // KJ_IO_H_
|