aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/google/protobuf/io/zero_copy_stream_impl.h
diff options
context:
space:
mode:
authorGravatar kenton@google.com <kenton@google.com@630680e5-0e50-0410-840e-4b1c322b438d>2009-07-29 01:13:20 +0000
committerGravatar kenton@google.com <kenton@google.com@630680e5-0e50-0410-840e-4b1c322b438d>2009-07-29 01:13:20 +0000
commit80b1d62bfcea65c59e2160da71dad84b1bd19cef (patch)
tree5423b830c53174fec83a7ea01ff0877e11c1ddb6 /src/google/protobuf/io/zero_copy_stream_impl.h
parentd2fd0638c309113ccae3731a58e30419f522269a (diff)
Submit recent changes from internal branch, including "lite mode" for
C++ and Java. See CHANGES.txt for more details.
Diffstat (limited to 'src/google/protobuf/io/zero_copy_stream_impl.h')
-rw-r--r--src/google/protobuf/io/zero_copy_stream_impl.h280
1 files changed, 1 insertions, 279 deletions
diff --git a/src/google/protobuf/io/zero_copy_stream_impl.h b/src/google/protobuf/io/zero_copy_stream_impl.h
index 448aa216..d7cf73ff 100644
--- a/src/google/protobuf/io/zero_copy_stream_impl.h
+++ b/src/google/protobuf/io/zero_copy_stream_impl.h
@@ -45,6 +45,7 @@
#include <string>
#include <iosfwd>
#include <google/protobuf/io/zero_copy_stream.h>
+#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
#include <google/protobuf/stubs/common.h>
@@ -52,285 +53,6 @@ namespace google {
namespace protobuf {
namespace io {
-// ===================================================================
-
-// A ZeroCopyInputStream backed by an in-memory array of bytes.
-class LIBPROTOBUF_EXPORT ArrayInputStream : public ZeroCopyInputStream {
- public:
- // Create an InputStream that returns the bytes pointed to by "data".
- // "data" remains the property of the caller but must remain valid until
- // the stream is destroyed. If a block_size is given, calls to Next()
- // will return data blocks no larger than the given size. Otherwise, the
- // first call to Next() returns the entire array. block_size is mainly
- // useful for testing; in production you would probably never want to set
- // it.
- ArrayInputStream(const void* data, int size, int block_size = -1);
- ~ArrayInputStream();
-
- // implements ZeroCopyInputStream ----------------------------------
- bool Next(const void** data, int* size);
- void BackUp(int count);
- bool Skip(int count);
- int64 ByteCount() const;
-
-
- private:
- const uint8* const data_; // The byte array.
- const int size_; // Total size of the array.
- const int block_size_; // How many bytes to return at a time.
-
- int position_;
- int last_returned_size_; // How many bytes we returned last time Next()
- // was called (used for error checking only).
-
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ArrayInputStream);
-};
-
-// ===================================================================
-
-// A ZeroCopyOutputStream backed by an in-memory array of bytes.
-class LIBPROTOBUF_EXPORT ArrayOutputStream : public ZeroCopyOutputStream {
- public:
- // Create an OutputStream that writes to the bytes pointed to by "data".
- // "data" remains the property of the caller but must remain valid until
- // the stream is destroyed. If a block_size is given, calls to Next()
- // will return data blocks no larger than the given size. Otherwise, the
- // first call to Next() returns the entire array. block_size is mainly
- // useful for testing; in production you would probably never want to set
- // it.
- ArrayOutputStream(void* data, int size, int block_size = -1);
- ~ArrayOutputStream();
-
- // implements ZeroCopyOutputStream ---------------------------------
- bool Next(void** data, int* size);
- void BackUp(int count);
- int64 ByteCount() const;
-
- private:
- uint8* const data_; // The byte array.
- const int size_; // Total size of the array.
- const int block_size_; // How many bytes to return at a time.
-
- int position_;
- int last_returned_size_; // How many bytes we returned last time Next()
- // was called (used for error checking only).
-
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ArrayOutputStream);
-};
-
-// ===================================================================
-
-// A ZeroCopyOutputStream which appends bytes to a string.
-class LIBPROTOBUF_EXPORT StringOutputStream : public ZeroCopyOutputStream {
- public:
- // Create a StringOutputStream which appends bytes to the given string.
- // The string remains property of the caller, but it MUST NOT be accessed
- // in any way until the stream is destroyed.
- //
- // Hint: If you call target->reserve(n) before creating the stream,
- // the first call to Next() will return at least n bytes of buffer
- // space.
- explicit StringOutputStream(string* target);
- ~StringOutputStream();
-
- // implements ZeroCopyOutputStream ---------------------------------
- bool Next(void** data, int* size);
- void BackUp(int count);
- int64 ByteCount() const;
-
- private:
- static const int kMinimumSize = 16;
-
- string* target_;
-
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(StringOutputStream);
-};
-
-// Note: There is no StringInputStream. Instead, just create an
-// ArrayInputStream as follows:
-// ArrayInputStream input(str.data(), str.size());
-
-// ===================================================================
-
-
-// ===================================================================
-
-// A generic traditional input stream interface.
-//
-// Lots of traditional input streams (e.g. file descriptors, C stdio
-// streams, and C++ iostreams) expose an interface where every read
-// involves copying bytes into a buffer. If you want to take such an
-// interface and make a ZeroCopyInputStream based on it, simply implement
-// CopyingInputStream and then use CopyingInputStreamAdaptor.
-//
-// CopyingInputStream implementations should avoid buffering if possible.
-// CopyingInputStreamAdaptor does its own buffering and will read data
-// in large blocks.
-class LIBPROTOBUF_EXPORT CopyingInputStream {
- public:
- virtual ~CopyingInputStream();
-
- // Reads up to "size" bytes into the given buffer. Returns the number of
- // bytes read. Read() waits until at least one byte is available, or
- // returns zero if no bytes will ever become available (EOF), or -1 if a
- // permanent read error occurred.
- virtual int Read(void* buffer, int size) = 0;
-
- // Skips the next "count" bytes of input. Returns the number of bytes
- // actually skipped. This will always be exactly equal to "count" unless
- // EOF was reached or a permanent read error occurred.
- //
- // The default implementation just repeatedly calls Read() into a scratch
- // buffer.
- virtual int Skip(int count);
-};
-
-// A ZeroCopyInputStream which reads from a CopyingInputStream. This is
-// useful for implementing ZeroCopyInputStreams that read from traditional
-// streams. Note that this class is not really zero-copy.
-//
-// If you want to read from file descriptors or C++ istreams, this is
-// already implemented for you: use FileInputStream or IstreamInputStream
-// respectively.
-class LIBPROTOBUF_EXPORT CopyingInputStreamAdaptor : public ZeroCopyInputStream {
- public:
- // Creates a stream that reads from the given CopyingInputStream.
- // If a block_size is given, it specifies the number of bytes that
- // should be read and returned with each call to Next(). Otherwise,
- // a reasonable default is used. The caller retains ownership of
- // copying_stream unless SetOwnsCopyingStream(true) is called.
- explicit CopyingInputStreamAdaptor(CopyingInputStream* copying_stream,
- int block_size = -1);
- ~CopyingInputStreamAdaptor();
-
- // Call SetOwnsCopyingStream(true) to tell the CopyingInputStreamAdaptor to
- // delete the underlying CopyingInputStream when it is destroyed.
- void SetOwnsCopyingStream(bool value) { owns_copying_stream_ = value; }
-
- // implements ZeroCopyInputStream ----------------------------------
- bool Next(const void** data, int* size);
- void BackUp(int count);
- bool Skip(int count);
- int64 ByteCount() const;
-
- private:
- // Insures that buffer_ is not NULL.
- void AllocateBufferIfNeeded();
- // Frees the buffer and resets buffer_used_.
- void FreeBuffer();
-
- // The underlying copying stream.
- CopyingInputStream* copying_stream_;
- bool owns_copying_stream_;
-
- // True if we have seen a permenant error from the underlying stream.
- bool failed_;
-
- // The current position of copying_stream_, relative to the point where
- // we started reading.
- int64 position_;
-
- // Data is read into this buffer. It may be NULL if no buffer is currently
- // in use. Otherwise, it points to an array of size buffer_size_.
- scoped_array<uint8> buffer_;
- const int buffer_size_;
-
- // Number of valid bytes currently in the buffer (i.e. the size last
- // returned by Next()). 0 <= buffer_used_ <= buffer_size_.
- int buffer_used_;
-
- // Number of bytes in the buffer which were backed up over by a call to
- // BackUp(). These need to be returned again.
- // 0 <= backup_bytes_ <= buffer_used_
- int backup_bytes_;
-
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CopyingInputStreamAdaptor);
-};
-
-// ===================================================================
-
-// A generic traditional output stream interface.
-//
-// Lots of traditional output streams (e.g. file descriptors, C stdio
-// streams, and C++ iostreams) expose an interface where every write
-// involves copying bytes from a buffer. If you want to take such an
-// interface and make a ZeroCopyOutputStream based on it, simply implement
-// CopyingOutputStream and then use CopyingOutputStreamAdaptor.
-//
-// CopyingOutputStream implementations should avoid buffering if possible.
-// CopyingOutputStreamAdaptor does its own buffering and will write data
-// in large blocks.
-class LIBPROTOBUF_EXPORT CopyingOutputStream {
- public:
- virtual ~CopyingOutputStream();
-
- // Writes "size" bytes from the given buffer to the output. Returns true
- // if successful, false on a write error.
- virtual bool Write(const void* buffer, int size) = 0;
-};
-
-// A ZeroCopyOutputStream which writes to a CopyingOutputStream. This is
-// useful for implementing ZeroCopyOutputStreams that write to traditional
-// streams. Note that this class is not really zero-copy.
-//
-// If you want to write to file descriptors or C++ ostreams, this is
-// already implemented for you: use FileOutputStream or OstreamOutputStream
-// respectively.
-class LIBPROTOBUF_EXPORT CopyingOutputStreamAdaptor : public ZeroCopyOutputStream {
- public:
- // Creates a stream that writes to the given Unix file descriptor.
- // If a block_size is given, it specifies the size of the buffers
- // that should be returned by Next(). Otherwise, a reasonable default
- // is used.
- explicit CopyingOutputStreamAdaptor(CopyingOutputStream* copying_stream,
- int block_size = -1);
- ~CopyingOutputStreamAdaptor();
-
- // Writes all pending data to the underlying stream. Returns false if a
- // write error occurred on the underlying stream. (The underlying
- // stream itself is not necessarily flushed.)
- bool Flush();
-
- // Call SetOwnsCopyingStream(true) to tell the CopyingOutputStreamAdaptor to
- // delete the underlying CopyingOutputStream when it is destroyed.
- void SetOwnsCopyingStream(bool value) { owns_copying_stream_ = value; }
-
- // implements ZeroCopyOutputStream ---------------------------------
- bool Next(void** data, int* size);
- void BackUp(int count);
- int64 ByteCount() const;
-
- private:
- // Write the current buffer, if it is present.
- bool WriteBuffer();
- // Insures that buffer_ is not NULL.
- void AllocateBufferIfNeeded();
- // Frees the buffer.
- void FreeBuffer();
-
- // The underlying copying stream.
- CopyingOutputStream* copying_stream_;
- bool owns_copying_stream_;
-
- // True if we have seen a permenant error from the underlying stream.
- bool failed_;
-
- // The current position of copying_stream_, relative to the point where
- // we started writing.
- int64 position_;
-
- // Data is written from this buffer. It may be NULL if no buffer is
- // currently in use. Otherwise, it points to an array of size buffer_size_.
- scoped_array<uint8> buffer_;
- const int buffer_size_;
-
- // Number of valid bytes currently in the buffer (i.e. the size last
- // returned by Next()). When BackUp() is called, we just reduce this.
- // 0 <= buffer_used_ <= buffer_size_.
- int buffer_used_;
-
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CopyingOutputStreamAdaptor);
-};
// ===================================================================