Chris@64: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors Chris@64: // Licensed under the MIT License: Chris@64: // Chris@64: // Permission is hereby granted, free of charge, to any person obtaining a copy Chris@64: // of this software and associated documentation files (the "Software"), to deal Chris@64: // in the Software without restriction, including without limitation the rights Chris@64: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell Chris@64: // copies of the Software, and to permit persons to whom the Software is Chris@64: // furnished to do so, subject to the following conditions: Chris@64: // Chris@64: // The above copyright notice and this permission notice shall be included in Chris@64: // all copies or substantial portions of the Software. Chris@64: // Chris@64: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR Chris@64: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, Chris@64: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE Chris@64: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER Chris@64: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, Chris@64: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN Chris@64: // THE SOFTWARE. Chris@64: Chris@64: #ifndef KJ_IO_H_ Chris@64: #define KJ_IO_H_ Chris@64: Chris@64: #if defined(__GNUC__) && !KJ_HEADER_WARNINGS Chris@64: #pragma GCC system_header Chris@64: #endif Chris@64: Chris@64: #include Chris@64: #include "common.h" Chris@64: #include "array.h" Chris@64: #include "exception.h" Chris@64: Chris@64: namespace kj { Chris@64: Chris@64: // ======================================================================================= Chris@64: // Abstract interfaces Chris@64: Chris@64: class InputStream { Chris@64: public: Chris@64: virtual ~InputStream() noexcept(false); Chris@64: Chris@64: size_t read(void* buffer, size_t minBytes, size_t maxBytes); Chris@64: // Reads at least minBytes and at most maxBytes, copying them into the given buffer. Returns Chris@64: // the size read. Throws an exception on errors. Implemented in terms of tryRead(). Chris@64: // Chris@64: // maxBytes is the number of bytes the caller really wants, but minBytes is the minimum amount Chris@64: // needed by the caller before it can start doing useful processing. If the stream returns less Chris@64: // than maxBytes, the caller will usually call read() again later to get the rest. Returning Chris@64: // less than maxBytes is useful when it makes sense for the caller to parallelize processing Chris@64: // with I/O. Chris@64: // Chris@64: // Never blocks if minBytes is zero. If minBytes is zero and maxBytes is non-zero, this may Chris@64: // attempt a non-blocking read or may just return zero. To force a read, use a non-zero minBytes. Chris@64: // To detect EOF without throwing an exception, use tryRead(). Chris@64: // Chris@64: // If the InputStream can't produce minBytes, it MUST throw an exception, as the caller is not Chris@64: // expected to understand how to deal with partial reads. Chris@64: Chris@64: virtual size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) = 0; Chris@64: // Like read(), but may return fewer than minBytes on EOF. Chris@64: Chris@64: inline void read(void* buffer, size_t bytes) { read(buffer, bytes, bytes); } Chris@64: // Convenience method for reading an exact number of bytes. Chris@64: Chris@64: virtual void skip(size_t bytes); Chris@64: // Skips past the given number of bytes, discarding them. The default implementation read()s Chris@64: // into a scratch buffer. Chris@64: }; Chris@64: Chris@64: class OutputStream { Chris@64: public: Chris@64: virtual ~OutputStream() noexcept(false); Chris@64: Chris@64: virtual void write(const void* buffer, size_t size) = 0; Chris@64: // Always writes the full size. Throws exception on error. Chris@64: Chris@64: virtual void write(ArrayPtr> pieces); Chris@64: // Equivalent to write()ing each byte array in sequence, which is what the default implementation Chris@64: // does. Override if you can do something better, e.g. use writev() to do the write in a single Chris@64: // syscall. Chris@64: }; Chris@64: Chris@64: class BufferedInputStream: public InputStream { Chris@64: // An input stream which buffers some bytes in memory to reduce system call overhead. Chris@64: // - OR - Chris@64: // An input stream that actually reads from some in-memory data structure and wants to give its Chris@64: // caller a direct pointer to that memory to potentially avoid a copy. Chris@64: Chris@64: public: Chris@64: virtual ~BufferedInputStream() noexcept(false); Chris@64: Chris@64: ArrayPtr getReadBuffer(); Chris@64: // Get a direct pointer into the read buffer, which contains the next bytes in the input. If the Chris@64: // caller consumes any bytes, it should then call skip() to indicate this. This always returns a Chris@64: // non-empty buffer or throws an exception. Implemented in terms of tryGetReadBuffer(). Chris@64: Chris@64: virtual ArrayPtr tryGetReadBuffer() = 0; Chris@64: // Like getReadBuffer() but may return an empty buffer on EOF. Chris@64: }; Chris@64: Chris@64: class BufferedOutputStream: public OutputStream { Chris@64: // An output stream which buffers some bytes in memory to reduce system call overhead. Chris@64: // - OR - Chris@64: // An output stream that actually writes into some in-memory data structure and wants to give its Chris@64: // caller a direct pointer to that memory to potentially avoid a copy. Chris@64: Chris@64: public: Chris@64: virtual ~BufferedOutputStream() noexcept(false); Chris@64: Chris@64: virtual ArrayPtr getWriteBuffer() = 0; Chris@64: // Get a direct pointer into the write buffer. The caller may choose to fill in some prefix of Chris@64: // this buffer and then pass it to write(), in which case write() may avoid a copy. It is Chris@64: // incorrect to pass to write any slice of this buffer which is not a prefix. Chris@64: }; Chris@64: Chris@64: // ======================================================================================= Chris@64: // Buffered streams implemented as wrappers around regular streams Chris@64: Chris@64: class BufferedInputStreamWrapper: public BufferedInputStream { Chris@64: // Implements BufferedInputStream in terms of an InputStream. Chris@64: // Chris@64: // Note that the underlying stream's position is unpredictable once the wrapper is destroyed, Chris@64: // unless the entire stream was consumed. To read a predictable number of bytes in a buffered Chris@64: // way without going over, you'd need this wrapper to wrap some other wrapper which itself Chris@64: // implements an artificial EOF at the desired point. Such a stream should be trivial to write Chris@64: // but is not provided by the library at this time. Chris@64: Chris@64: public: Chris@64: explicit BufferedInputStreamWrapper(InputStream& inner, ArrayPtr buffer = nullptr); Chris@64: // Creates a buffered stream wrapping the given non-buffered stream. No guarantee is made about Chris@64: // the position of the inner stream after a buffered wrapper has been created unless the entire Chris@64: // input is read. Chris@64: // Chris@64: // If the second parameter is non-null, the stream uses the given buffer instead of allocating Chris@64: // its own. This may improve performance if the buffer can be reused. Chris@64: Chris@64: KJ_DISALLOW_COPY(BufferedInputStreamWrapper); Chris@64: ~BufferedInputStreamWrapper() noexcept(false); Chris@64: Chris@64: // implements BufferedInputStream ---------------------------------- Chris@64: ArrayPtr tryGetReadBuffer() override; Chris@64: size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override; Chris@64: void skip(size_t bytes) override; Chris@64: Chris@64: private: Chris@64: InputStream& inner; Chris@64: Array ownedBuffer; Chris@64: ArrayPtr buffer; Chris@64: ArrayPtr bufferAvailable; Chris@64: }; Chris@64: Chris@64: class BufferedOutputStreamWrapper: public BufferedOutputStream { Chris@64: // Implements BufferedOutputStream in terms of an OutputStream. Note that writes to the Chris@64: // underlying stream may be delayed until flush() is called or the wrapper is destroyed. Chris@64: Chris@64: public: Chris@64: explicit BufferedOutputStreamWrapper(OutputStream& inner, ArrayPtr buffer = nullptr); Chris@64: // Creates a buffered stream wrapping the given non-buffered stream. Chris@64: // Chris@64: // If the second parameter is non-null, the stream uses the given buffer instead of allocating Chris@64: // its own. This may improve performance if the buffer can be reused. Chris@64: Chris@64: KJ_DISALLOW_COPY(BufferedOutputStreamWrapper); Chris@64: ~BufferedOutputStreamWrapper() noexcept(false); Chris@64: Chris@64: void flush(); Chris@64: // Force the wrapper to write any remaining bytes in its buffer to the inner stream. Note that Chris@64: // this only flushes this object's buffer; this object has no idea how to flush any other buffers Chris@64: // that may be present in the underlying stream. Chris@64: Chris@64: // implements BufferedOutputStream --------------------------------- Chris@64: ArrayPtr getWriteBuffer() override; Chris@64: void write(const void* buffer, size_t size) override; Chris@64: Chris@64: private: Chris@64: OutputStream& inner; Chris@64: Array ownedBuffer; Chris@64: ArrayPtr buffer; Chris@64: byte* bufferPos; Chris@64: UnwindDetector unwindDetector; Chris@64: }; Chris@64: Chris@64: // ======================================================================================= Chris@64: // Array I/O Chris@64: Chris@64: class ArrayInputStream: public BufferedInputStream { Chris@64: public: Chris@64: explicit ArrayInputStream(ArrayPtr array); Chris@64: KJ_DISALLOW_COPY(ArrayInputStream); Chris@64: ~ArrayInputStream() noexcept(false); Chris@64: Chris@64: // implements BufferedInputStream ---------------------------------- Chris@64: ArrayPtr tryGetReadBuffer() override; Chris@64: size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override; Chris@64: void skip(size_t bytes) override; Chris@64: Chris@64: private: Chris@64: ArrayPtr array; Chris@64: }; Chris@64: Chris@64: class ArrayOutputStream: public BufferedOutputStream { Chris@64: public: Chris@64: explicit ArrayOutputStream(ArrayPtr array); Chris@64: KJ_DISALLOW_COPY(ArrayOutputStream); Chris@64: ~ArrayOutputStream() noexcept(false); Chris@64: Chris@64: ArrayPtr getArray() { Chris@64: // Get the portion of the array which has been filled in. Chris@64: return arrayPtr(array.begin(), fillPos); Chris@64: } Chris@64: Chris@64: // implements BufferedInputStream ---------------------------------- Chris@64: ArrayPtr getWriteBuffer() override; Chris@64: void write(const void* buffer, size_t size) override; Chris@64: Chris@64: private: Chris@64: ArrayPtr array; Chris@64: byte* fillPos; Chris@64: }; Chris@64: Chris@64: class VectorOutputStream: public BufferedOutputStream { Chris@64: public: Chris@64: explicit VectorOutputStream(size_t initialCapacity = 4096); Chris@64: KJ_DISALLOW_COPY(VectorOutputStream); Chris@64: ~VectorOutputStream() noexcept(false); Chris@64: Chris@64: ArrayPtr getArray() { Chris@64: // Get the portion of the array which has been filled in. Chris@64: return arrayPtr(vector.begin(), fillPos); Chris@64: } Chris@64: Chris@64: // implements BufferedInputStream ---------------------------------- Chris@64: ArrayPtr getWriteBuffer() override; Chris@64: void write(const void* buffer, size_t size) override; Chris@64: Chris@64: private: Chris@64: Array vector; Chris@64: byte* fillPos; Chris@64: Chris@64: void grow(size_t minSize); Chris@64: }; Chris@64: Chris@64: // ======================================================================================= Chris@64: // File descriptor I/O Chris@64: Chris@64: class AutoCloseFd { Chris@64: // A wrapper around a file descriptor which automatically closes the descriptor when destroyed. Chris@64: // The wrapper supports move construction for transferring ownership of the descriptor. If Chris@64: // close() returns an error, the destructor throws an exception, UNLESS the destructor is being Chris@64: // called during unwind from another exception, in which case the close error is ignored. Chris@64: // Chris@64: // If your code is not exception-safe, you should not use AutoCloseFd. In this case you will Chris@64: // have to call close() yourself and handle errors appropriately. Chris@64: Chris@64: public: Chris@64: inline AutoCloseFd(): fd(-1) {} Chris@64: inline AutoCloseFd(decltype(nullptr)): fd(-1) {} Chris@64: inline explicit AutoCloseFd(int fd): fd(fd) {} Chris@64: inline AutoCloseFd(AutoCloseFd&& other) noexcept: fd(other.fd) { other.fd = -1; } Chris@64: KJ_DISALLOW_COPY(AutoCloseFd); Chris@64: ~AutoCloseFd() noexcept(false); Chris@64: Chris@64: inline AutoCloseFd& operator=(AutoCloseFd&& other) { Chris@64: AutoCloseFd old(kj::mv(*this)); Chris@64: fd = other.fd; Chris@64: other.fd = -1; Chris@64: return *this; Chris@64: } Chris@64: Chris@64: inline AutoCloseFd& operator=(decltype(nullptr)) { Chris@64: AutoCloseFd old(kj::mv(*this)); Chris@64: return *this; Chris@64: } Chris@64: Chris@64: inline operator int() const { return fd; } Chris@64: inline int get() const { return fd; } Chris@64: Chris@64: operator bool() const = delete; Chris@64: // Deleting this operator prevents accidental use in boolean contexts, which Chris@64: // the int conversion operator above would otherwise allow. Chris@64: Chris@64: inline bool operator==(decltype(nullptr)) { return fd < 0; } Chris@64: inline bool operator!=(decltype(nullptr)) { return fd >= 0; } Chris@64: Chris@64: private: Chris@64: int fd; Chris@64: UnwindDetector unwindDetector; Chris@64: }; Chris@64: Chris@64: inline auto KJ_STRINGIFY(const AutoCloseFd& fd) Chris@64: -> decltype(kj::toCharSequence(implicitCast(fd))) { Chris@64: return kj::toCharSequence(implicitCast(fd)); Chris@64: } Chris@64: Chris@64: class FdInputStream: public InputStream { Chris@64: // An InputStream wrapping a file descriptor. Chris@64: Chris@64: public: Chris@64: explicit FdInputStream(int fd): fd(fd) {} Chris@64: explicit FdInputStream(AutoCloseFd fd): fd(fd), autoclose(mv(fd)) {} Chris@64: KJ_DISALLOW_COPY(FdInputStream); Chris@64: ~FdInputStream() noexcept(false); Chris@64: Chris@64: size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override; Chris@64: Chris@64: inline int getFd() const { return fd; } Chris@64: Chris@64: private: Chris@64: int fd; Chris@64: AutoCloseFd autoclose; Chris@64: }; Chris@64: Chris@64: class FdOutputStream: public OutputStream { Chris@64: // An OutputStream wrapping a file descriptor. Chris@64: Chris@64: public: Chris@64: explicit FdOutputStream(int fd): fd(fd) {} Chris@64: explicit FdOutputStream(AutoCloseFd fd): fd(fd), autoclose(mv(fd)) {} Chris@64: KJ_DISALLOW_COPY(FdOutputStream); Chris@64: ~FdOutputStream() noexcept(false); Chris@64: Chris@64: void write(const void* buffer, size_t size) override; Chris@64: void write(ArrayPtr> pieces) override; Chris@64: Chris@64: inline int getFd() const { return fd; } Chris@64: Chris@64: private: Chris@64: int fd; Chris@64: AutoCloseFd autoclose; Chris@64: }; Chris@64: Chris@64: // ======================================================================================= Chris@64: // Win32 Handle I/O Chris@64: Chris@64: #ifdef _WIN32 Chris@64: Chris@64: class AutoCloseHandle { Chris@64: // A wrapper around a Win32 HANDLE which automatically closes the handle when destroyed. Chris@64: // The wrapper supports move construction for transferring ownership of the handle. If Chris@64: // CloseHandle() returns an error, the destructor throws an exception, UNLESS the destructor is Chris@64: // being called during unwind from another exception, in which case the close error is ignored. Chris@64: // Chris@64: // If your code is not exception-safe, you should not use AutoCloseHandle. In this case you will Chris@64: // have to call close() yourself and handle errors appropriately. Chris@64: Chris@64: public: Chris@64: inline AutoCloseHandle(): handle((void*)-1) {} Chris@64: inline AutoCloseHandle(decltype(nullptr)): handle((void*)-1) {} Chris@64: inline explicit AutoCloseHandle(void* handle): handle(handle) {} Chris@64: inline AutoCloseHandle(AutoCloseHandle&& other) noexcept: handle(other.handle) { Chris@64: other.handle = (void*)-1; Chris@64: } Chris@64: KJ_DISALLOW_COPY(AutoCloseHandle); Chris@64: ~AutoCloseHandle() noexcept(false); Chris@64: Chris@64: inline AutoCloseHandle& operator=(AutoCloseHandle&& other) { Chris@64: AutoCloseHandle old(kj::mv(*this)); Chris@64: handle = other.handle; Chris@64: other.handle = (void*)-1; Chris@64: return *this; Chris@64: } Chris@64: Chris@64: inline AutoCloseHandle& operator=(decltype(nullptr)) { Chris@64: AutoCloseHandle old(kj::mv(*this)); Chris@64: return *this; Chris@64: } Chris@64: Chris@64: inline operator void*() const { return handle; } Chris@64: inline void* get() const { return handle; } Chris@64: Chris@64: operator bool() const = delete; Chris@64: // Deleting this operator prevents accidental use in boolean contexts, which Chris@64: // the void* conversion operator above would otherwise allow. Chris@64: Chris@64: inline bool operator==(decltype(nullptr)) { return handle != (void*)-1; } Chris@64: inline bool operator!=(decltype(nullptr)) { return handle == (void*)-1; } Chris@64: Chris@64: private: Chris@64: void* handle; // -1 (aka INVALID_HANDLE_VALUE) if not valid. Chris@64: }; Chris@64: Chris@64: class HandleInputStream: public InputStream { Chris@64: // An InputStream wrapping a Win32 HANDLE. Chris@64: Chris@64: public: Chris@64: explicit HandleInputStream(void* handle): handle(handle) {} Chris@64: explicit HandleInputStream(AutoCloseHandle handle): handle(handle), autoclose(mv(handle)) {} Chris@64: KJ_DISALLOW_COPY(HandleInputStream); Chris@64: ~HandleInputStream() noexcept(false); Chris@64: Chris@64: size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override; Chris@64: Chris@64: private: Chris@64: void* handle; Chris@64: AutoCloseHandle autoclose; Chris@64: }; Chris@64: Chris@64: class HandleOutputStream: public OutputStream { Chris@64: // An OutputStream wrapping a Win32 HANDLE. Chris@64: Chris@64: public: Chris@64: explicit HandleOutputStream(void* handle): handle(handle) {} Chris@64: explicit HandleOutputStream(AutoCloseHandle handle): handle(handle), autoclose(mv(handle)) {} Chris@64: KJ_DISALLOW_COPY(HandleOutputStream); Chris@64: ~HandleOutputStream() noexcept(false); Chris@64: Chris@64: void write(const void* buffer, size_t size) override; Chris@64: Chris@64: private: Chris@64: void* handle; Chris@64: AutoCloseHandle autoclose; Chris@64: }; Chris@64: Chris@64: #endif // _WIN32 Chris@64: Chris@64: } // namespace kj Chris@64: Chris@64: #endif // KJ_IO_H_