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