1
// Protocol Buffers - Google's data interchange format
2
// Copyright 2008 Google Inc. All rights reserved.
3
// http://code.google.com/p/protobuf/
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are
9
// * Redistributions of source code must retain the above copyright
10
// notice, this list of conditions and the following disclaimer.
11
// * Redistributions in binary form must reproduce the above
12
// copyright notice, this list of conditions and the following disclaimer
13
// in the documentation and/or other materials provided with the
15
// * Neither the name of Google Inc. nor the names of its
16
// contributors may be used to endorse or promote products derived from
17
// this software without specific prior written permission.
19
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
// Author: kenton@google.com (Kenton Varda)
32
// Based on original Protocol Buffers design by
33
// Sanjay Ghemawat, Jeff Dean, and others.
35
// This file contains common implementations of the interfaces defined in
36
// zero_copy_stream.h which are included in the "lite" protobuf library.
37
// These implementations cover I/O on raw arrays and strings, as well as
38
// adaptors which make it easy to implement streams based on traditional
39
// streams. Of course, many users will probably want to write their own
40
// implementations of these interfaces specific to the particular I/O
41
// abstractions they prefer to use, but these should cover the most common
44
#ifndef GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_LITE_H__
45
#define GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_LITE_H__
49
#include <google/protobuf/io/zero_copy_stream.h>
50
#include <google/protobuf/stubs/common.h>
57
// ===================================================================
59
// A ZeroCopyInputStream backed by an in-memory array of bytes.
60
class LIBPROTOBUF_EXPORT ArrayInputStream : public ZeroCopyInputStream {
62
// Create an InputStream that returns the bytes pointed to by "data".
63
// "data" remains the property of the caller but must remain valid until
64
// the stream is destroyed. If a block_size is given, calls to Next()
65
// will return data blocks no larger than the given size. Otherwise, the
66
// first call to Next() returns the entire array. block_size is mainly
67
// useful for testing; in production you would probably never want to set
69
ArrayInputStream(const void* data, int size, int block_size = -1);
72
// implements ZeroCopyInputStream ----------------------------------
73
bool Next(const void** data, int* size);
74
void BackUp(int count);
76
int64 ByteCount() const;
80
const uint8* const data_; // The byte array.
81
const int size_; // Total size of the array.
82
const int block_size_; // How many bytes to return at a time.
85
int last_returned_size_; // How many bytes we returned last time Next()
86
// was called (used for error checking only).
88
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ArrayInputStream);
91
// ===================================================================
93
// A ZeroCopyOutputStream backed by an in-memory array of bytes.
94
class LIBPROTOBUF_EXPORT ArrayOutputStream : public ZeroCopyOutputStream {
96
// Create an OutputStream that writes to the bytes pointed to by "data".
97
// "data" remains the property of the caller but must remain valid until
98
// the stream is destroyed. If a block_size is given, calls to Next()
99
// will return data blocks no larger than the given size. Otherwise, the
100
// first call to Next() returns the entire array. block_size is mainly
101
// useful for testing; in production you would probably never want to set
103
ArrayOutputStream(void* data, int size, int block_size = -1);
104
~ArrayOutputStream();
106
// implements ZeroCopyOutputStream ---------------------------------
107
bool Next(void** data, int* size);
108
void BackUp(int count);
109
int64 ByteCount() const;
112
uint8* const data_; // The byte array.
113
const int size_; // Total size of the array.
114
const int block_size_; // How many bytes to return at a time.
117
int last_returned_size_; // How many bytes we returned last time Next()
118
// was called (used for error checking only).
120
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ArrayOutputStream);
123
// ===================================================================
125
// A ZeroCopyOutputStream which appends bytes to a string.
126
class LIBPROTOBUF_EXPORT StringOutputStream : public ZeroCopyOutputStream {
128
// Create a StringOutputStream which appends bytes to the given string.
129
// The string remains property of the caller, but it MUST NOT be accessed
130
// in any way until the stream is destroyed.
132
// Hint: If you call target->reserve(n) before creating the stream,
133
// the first call to Next() will return at least n bytes of buffer
135
explicit StringOutputStream(string* target);
136
~StringOutputStream();
138
// implements ZeroCopyOutputStream ---------------------------------
139
bool Next(void** data, int* size);
140
void BackUp(int count);
141
int64 ByteCount() const;
144
static const int kMinimumSize = 16;
148
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(StringOutputStream);
151
// Note: There is no StringInputStream. Instead, just create an
152
// ArrayInputStream as follows:
153
// ArrayInputStream input(str.data(), str.size());
155
// ===================================================================
157
// A generic traditional input stream interface.
159
// Lots of traditional input streams (e.g. file descriptors, C stdio
160
// streams, and C++ iostreams) expose an interface where every read
161
// involves copying bytes into a buffer. If you want to take such an
162
// interface and make a ZeroCopyInputStream based on it, simply implement
163
// CopyingInputStream and then use CopyingInputStreamAdaptor.
165
// CopyingInputStream implementations should avoid buffering if possible.
166
// CopyingInputStreamAdaptor does its own buffering and will read data
168
class LIBPROTOBUF_EXPORT CopyingInputStream {
170
virtual ~CopyingInputStream();
172
// Reads up to "size" bytes into the given buffer. Returns the number of
173
// bytes read. Read() waits until at least one byte is available, or
174
// returns zero if no bytes will ever become available (EOF), or -1 if a
175
// permanent read error occurred.
176
virtual int Read(void* buffer, int size) = 0;
178
// Skips the next "count" bytes of input. Returns the number of bytes
179
// actually skipped. This will always be exactly equal to "count" unless
180
// EOF was reached or a permanent read error occurred.
182
// The default implementation just repeatedly calls Read() into a scratch
184
virtual int Skip(int count);
187
// A ZeroCopyInputStream which reads from a CopyingInputStream. This is
188
// useful for implementing ZeroCopyInputStreams that read from traditional
189
// streams. Note that this class is not really zero-copy.
191
// If you want to read from file descriptors or C++ istreams, this is
192
// already implemented for you: use FileInputStream or IstreamInputStream
194
class LIBPROTOBUF_EXPORT CopyingInputStreamAdaptor : public ZeroCopyInputStream {
196
// Creates a stream that reads from the given CopyingInputStream.
197
// If a block_size is given, it specifies the number of bytes that
198
// should be read and returned with each call to Next(). Otherwise,
199
// a reasonable default is used. The caller retains ownership of
200
// copying_stream unless SetOwnsCopyingStream(true) is called.
201
explicit CopyingInputStreamAdaptor(CopyingInputStream* copying_stream,
202
int block_size = -1);
203
~CopyingInputStreamAdaptor();
205
// Call SetOwnsCopyingStream(true) to tell the CopyingInputStreamAdaptor to
206
// delete the underlying CopyingInputStream when it is destroyed.
207
void SetOwnsCopyingStream(bool value) { owns_copying_stream_ = value; }
209
// implements ZeroCopyInputStream ----------------------------------
210
bool Next(const void** data, int* size);
211
void BackUp(int count);
212
bool Skip(int count);
213
int64 ByteCount() const;
216
// Insures that buffer_ is not NULL.
217
void AllocateBufferIfNeeded();
218
// Frees the buffer and resets buffer_used_.
221
// The underlying copying stream.
222
CopyingInputStream* copying_stream_;
223
bool owns_copying_stream_;
225
// True if we have seen a permenant error from the underlying stream.
228
// The current position of copying_stream_, relative to the point where
229
// we started reading.
232
// Data is read into this buffer. It may be NULL if no buffer is currently
233
// in use. Otherwise, it points to an array of size buffer_size_.
234
scoped_array<uint8> buffer_;
235
const int buffer_size_;
237
// Number of valid bytes currently in the buffer (i.e. the size last
238
// returned by Next()). 0 <= buffer_used_ <= buffer_size_.
241
// Number of bytes in the buffer which were backed up over by a call to
242
// BackUp(). These need to be returned again.
243
// 0 <= backup_bytes_ <= buffer_used_
246
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CopyingInputStreamAdaptor);
249
// ===================================================================
251
// A generic traditional output stream interface.
253
// Lots of traditional output streams (e.g. file descriptors, C stdio
254
// streams, and C++ iostreams) expose an interface where every write
255
// involves copying bytes from a buffer. If you want to take such an
256
// interface and make a ZeroCopyOutputStream based on it, simply implement
257
// CopyingOutputStream and then use CopyingOutputStreamAdaptor.
259
// CopyingOutputStream implementations should avoid buffering if possible.
260
// CopyingOutputStreamAdaptor does its own buffering and will write data
262
class LIBPROTOBUF_EXPORT CopyingOutputStream {
264
virtual ~CopyingOutputStream();
266
// Writes "size" bytes from the given buffer to the output. Returns true
267
// if successful, false on a write error.
268
virtual bool Write(const void* buffer, int size) = 0;
271
// A ZeroCopyOutputStream which writes to a CopyingOutputStream. This is
272
// useful for implementing ZeroCopyOutputStreams that write to traditional
273
// streams. Note that this class is not really zero-copy.
275
// If you want to write to file descriptors or C++ ostreams, this is
276
// already implemented for you: use FileOutputStream or OstreamOutputStream
278
class LIBPROTOBUF_EXPORT CopyingOutputStreamAdaptor : public ZeroCopyOutputStream {
280
// Creates a stream that writes to the given Unix file descriptor.
281
// If a block_size is given, it specifies the size of the buffers
282
// that should be returned by Next(). Otherwise, a reasonable default
284
explicit CopyingOutputStreamAdaptor(CopyingOutputStream* copying_stream,
285
int block_size = -1);
286
~CopyingOutputStreamAdaptor();
288
// Writes all pending data to the underlying stream. Returns false if a
289
// write error occurred on the underlying stream. (The underlying
290
// stream itself is not necessarily flushed.)
293
// Call SetOwnsCopyingStream(true) to tell the CopyingOutputStreamAdaptor to
294
// delete the underlying CopyingOutputStream when it is destroyed.
295
void SetOwnsCopyingStream(bool value) { owns_copying_stream_ = value; }
297
// implements ZeroCopyOutputStream ---------------------------------
298
bool Next(void** data, int* size);
299
void BackUp(int count);
300
int64 ByteCount() const;
303
// Write the current buffer, if it is present.
305
// Insures that buffer_ is not NULL.
306
void AllocateBufferIfNeeded();
310
// The underlying copying stream.
311
CopyingOutputStream* copying_stream_;
312
bool owns_copying_stream_;
314
// True if we have seen a permenant error from the underlying stream.
317
// The current position of copying_stream_, relative to the point where
318
// we started writing.
321
// Data is written from this buffer. It may be NULL if no buffer is
322
// currently in use. Otherwise, it points to an array of size buffer_size_.
323
scoped_array<uint8> buffer_;
324
const int buffer_size_;
326
// Number of valid bytes currently in the buffer (i.e. the size last
327
// returned by Next()). When BackUp() is called, we just reduce this.
328
// 0 <= buffer_used_ <= buffer_size_.
331
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CopyingOutputStreamAdaptor);
334
// ===================================================================
337
} // namespace protobuf
339
} // namespace google
340
#endif // GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_LITE_H__