1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
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
14 // distribution.
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.
18 //
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.
30
31 // Author: kenton@google.com (Kenton Varda)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
34 //
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
42 // cases.
43
44 #ifndef GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_LITE_H__
45 #define GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_LITE_H__
46
47 #include <string>
48 #include <iosfwd>
49 #include <google/protobuf/io/zero_copy_stream.h>
50 #include <google/protobuf/stubs/common.h>
51 #include <google/protobuf/stubs/stl_util.h>
52
53
54 namespace google {
55 namespace protobuf {
56 namespace io {
57
58 // ===================================================================
59
60 // A ZeroCopyInputStream backed by an in-memory array of bytes.
61 class LIBPROTOBUF_EXPORT ArrayInputStream : public ZeroCopyInputStream {
62 public:
63 // Create an InputStream that returns the bytes pointed to by "data".
64 // "data" remains the property of the caller but must remain valid until
65 // the stream is destroyed. If a block_size is given, calls to Next()
66 // will return data blocks no larger than the given size. Otherwise, the
67 // first call to Next() returns the entire array. block_size is mainly
68 // useful for testing; in production you would probably never want to set
69 // it.
70 ArrayInputStream(const void* data, int size, int block_size = -1);
71 ~ArrayInputStream();
72
73 // implements ZeroCopyInputStream ----------------------------------
74 bool Next(const void** data, int* size);
75 void BackUp(int count);
76 bool Skip(int count);
77 int64 ByteCount() const;
78
79
80 private:
81 const uint8* const data_; // The byte array.
82 const int size_; // Total size of the array.
83 const int block_size_; // How many bytes to return at a time.
84
85 int position_;
86 int last_returned_size_; // How many bytes we returned last time Next()
87 // was called (used for error checking only).
88
89 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ArrayInputStream);
90 };
91
92 // ===================================================================
93
94 // A ZeroCopyOutputStream backed by an in-memory array of bytes.
95 class LIBPROTOBUF_EXPORT ArrayOutputStream : public ZeroCopyOutputStream {
96 public:
97 // Create an OutputStream that writes to the bytes pointed to by "data".
98 // "data" remains the property of the caller but must remain valid until
99 // the stream is destroyed. If a block_size is given, calls to Next()
100 // will return data blocks no larger than the given size. Otherwise, the
101 // first call to Next() returns the entire array. block_size is mainly
102 // useful for testing; in production you would probably never want to set
103 // it.
104 ArrayOutputStream(void* data, int size, int block_size = -1);
105 ~ArrayOutputStream();
106
107 // implements ZeroCopyOutputStream ---------------------------------
108 bool Next(void** data, int* size);
109 void BackUp(int count);
110 int64 ByteCount() const;
111
112 private:
113 uint8* const data_; // The byte array.
114 const int size_; // Total size of the array.
115 const int block_size_; // How many bytes to return at a time.
116
117 int position_;
118 int last_returned_size_; // How many bytes we returned last time Next()
119 // was called (used for error checking only).
120
121 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ArrayOutputStream);
122 };
123
124 // ===================================================================
125
126 // A ZeroCopyOutputStream which appends bytes to a string.
127 class LIBPROTOBUF_EXPORT StringOutputStream : public ZeroCopyOutputStream {
128 public:
129 // Create a StringOutputStream which appends bytes to the given string.
130 // The string remains property of the caller, but it MUST NOT be accessed
131 // in any way until the stream is destroyed.
132 //
133 // Hint: If you call target->reserve(n) before creating the stream,
134 // the first call to Next() will return at least n bytes of buffer
135 // space.
136 explicit StringOutputStream(string* target);
137 ~StringOutputStream();
138
139 // implements ZeroCopyOutputStream ---------------------------------
140 bool Next(void** data, int* size);
141 void BackUp(int count);
142 int64 ByteCount() const;
143
144 private:
145 static const int kMinimumSize = 16;
146
147 string* target_;
148
149 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(StringOutputStream);
150 };
151
152 // Note: There is no StringInputStream. Instead, just create an
153 // ArrayInputStream as follows:
154 // ArrayInputStream input(str.data(), str.size());
155
156 // ===================================================================
157
158 // A generic traditional input stream interface.
159 //
160 // Lots of traditional input streams (e.g. file descriptors, C stdio
161 // streams, and C++ iostreams) expose an interface where every read
162 // involves copying bytes into a buffer. If you want to take such an
163 // interface and make a ZeroCopyInputStream based on it, simply implement
164 // CopyingInputStream and then use CopyingInputStreamAdaptor.
165 //
166 // CopyingInputStream implementations should avoid buffering if possible.
167 // CopyingInputStreamAdaptor does its own buffering and will read data
168 // in large blocks.
169 class LIBPROTOBUF_EXPORT CopyingInputStream {
170 public:
171 virtual ~CopyingInputStream();
172
173 // Reads up to "size" bytes into the given buffer. Returns the number of
174 // bytes read. Read() waits until at least one byte is available, or
175 // returns zero if no bytes will ever become available (EOF), or -1 if a
176 // permanent read error occurred.
177 virtual int Read(void* buffer, int size) = 0;
178
179 // Skips the next "count" bytes of input. Returns the number of bytes
180 // actually skipped. This will always be exactly equal to "count" unless
181 // EOF was reached or a permanent read error occurred.
182 //
183 // The default implementation just repeatedly calls Read() into a scratch
184 // buffer.
185 virtual int Skip(int count);
186 };
187
188 // A ZeroCopyInputStream which reads from a CopyingInputStream. This is
189 // useful for implementing ZeroCopyInputStreams that read from traditional
190 // streams. Note that this class is not really zero-copy.
191 //
192 // If you want to read from file descriptors or C++ istreams, this is
193 // already implemented for you: use FileInputStream or IstreamInputStream
194 // respectively.
195 class LIBPROTOBUF_EXPORT CopyingInputStreamAdaptor : public ZeroCopyInputStream {
196 public:
197 // Creates a stream that reads from the given CopyingInputStream.
198 // If a block_size is given, it specifies the number of bytes that
199 // should be read and returned with each call to Next(). Otherwise,
200 // a reasonable default is used. The caller retains ownership of
201 // copying_stream unless SetOwnsCopyingStream(true) is called.
202 explicit CopyingInputStreamAdaptor(CopyingInputStream* copying_stream,
203 int block_size = -1);
204 ~CopyingInputStreamAdaptor();
205
206 // Call SetOwnsCopyingStream(true) to tell the CopyingInputStreamAdaptor to
207 // delete the underlying CopyingInputStream when it is destroyed.
SetOwnsCopyingStream(bool value)208 void SetOwnsCopyingStream(bool value) { owns_copying_stream_ = value; }
209
210 // implements ZeroCopyInputStream ----------------------------------
211 bool Next(const void** data, int* size);
212 void BackUp(int count);
213 bool Skip(int count);
214 int64 ByteCount() const;
215
216 private:
217 // Insures that buffer_ is not NULL.
218 void AllocateBufferIfNeeded();
219 // Frees the buffer and resets buffer_used_.
220 void FreeBuffer();
221
222 // The underlying copying stream.
223 CopyingInputStream* copying_stream_;
224 bool owns_copying_stream_;
225
226 // True if we have seen a permenant error from the underlying stream.
227 bool failed_;
228
229 // The current position of copying_stream_, relative to the point where
230 // we started reading.
231 int64 position_;
232
233 // Data is read into this buffer. It may be NULL if no buffer is currently
234 // in use. Otherwise, it points to an array of size buffer_size_.
235 scoped_array<uint8> buffer_;
236 const int buffer_size_;
237
238 // Number of valid bytes currently in the buffer (i.e. the size last
239 // returned by Next()). 0 <= buffer_used_ <= buffer_size_.
240 int buffer_used_;
241
242 // Number of bytes in the buffer which were backed up over by a call to
243 // BackUp(). These need to be returned again.
244 // 0 <= backup_bytes_ <= buffer_used_
245 int backup_bytes_;
246
247 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CopyingInputStreamAdaptor);
248 };
249
250 // ===================================================================
251
252 // A generic traditional output stream interface.
253 //
254 // Lots of traditional output streams (e.g. file descriptors, C stdio
255 // streams, and C++ iostreams) expose an interface where every write
256 // involves copying bytes from a buffer. If you want to take such an
257 // interface and make a ZeroCopyOutputStream based on it, simply implement
258 // CopyingOutputStream and then use CopyingOutputStreamAdaptor.
259 //
260 // CopyingOutputStream implementations should avoid buffering if possible.
261 // CopyingOutputStreamAdaptor does its own buffering and will write data
262 // in large blocks.
263 class LIBPROTOBUF_EXPORT CopyingOutputStream {
264 public:
265 virtual ~CopyingOutputStream();
266
267 // Writes "size" bytes from the given buffer to the output. Returns true
268 // if successful, false on a write error.
269 virtual bool Write(const void* buffer, int size) = 0;
270 };
271
272 // A ZeroCopyOutputStream which writes to a CopyingOutputStream. This is
273 // useful for implementing ZeroCopyOutputStreams that write to traditional
274 // streams. Note that this class is not really zero-copy.
275 //
276 // If you want to write to file descriptors or C++ ostreams, this is
277 // already implemented for you: use FileOutputStream or OstreamOutputStream
278 // respectively.
279 class LIBPROTOBUF_EXPORT CopyingOutputStreamAdaptor : public ZeroCopyOutputStream {
280 public:
281 // Creates a stream that writes to the given Unix file descriptor.
282 // If a block_size is given, it specifies the size of the buffers
283 // that should be returned by Next(). Otherwise, a reasonable default
284 // is used.
285 explicit CopyingOutputStreamAdaptor(CopyingOutputStream* copying_stream,
286 int block_size = -1);
287 ~CopyingOutputStreamAdaptor();
288
289 // Writes all pending data to the underlying stream. Returns false if a
290 // write error occurred on the underlying stream. (The underlying
291 // stream itself is not necessarily flushed.)
292 bool Flush();
293
294 // Call SetOwnsCopyingStream(true) to tell the CopyingOutputStreamAdaptor to
295 // delete the underlying CopyingOutputStream when it is destroyed.
SetOwnsCopyingStream(bool value)296 void SetOwnsCopyingStream(bool value) { owns_copying_stream_ = value; }
297
298 // implements ZeroCopyOutputStream ---------------------------------
299 bool Next(void** data, int* size);
300 void BackUp(int count);
301 int64 ByteCount() const;
302
303 private:
304 // Write the current buffer, if it is present.
305 bool WriteBuffer();
306 // Insures that buffer_ is not NULL.
307 void AllocateBufferIfNeeded();
308 // Frees the buffer.
309 void FreeBuffer();
310
311 // The underlying copying stream.
312 CopyingOutputStream* copying_stream_;
313 bool owns_copying_stream_;
314
315 // True if we have seen a permenant error from the underlying stream.
316 bool failed_;
317
318 // The current position of copying_stream_, relative to the point where
319 // we started writing.
320 int64 position_;
321
322 // Data is written from this buffer. It may be NULL if no buffer is
323 // currently in use. Otherwise, it points to an array of size buffer_size_.
324 scoped_array<uint8> buffer_;
325 const int buffer_size_;
326
327 // Number of valid bytes currently in the buffer (i.e. the size last
328 // returned by Next()). When BackUp() is called, we just reduce this.
329 // 0 <= buffer_used_ <= buffer_size_.
330 int buffer_used_;
331
332 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CopyingOutputStreamAdaptor);
333 };
334
335 // ===================================================================
336
337 // Return a pointer to mutable characters underlying the given string. The
338 // return value is valid until the next time the string is resized. We
339 // trust the caller to treat the return value as an array of length s->size().
mutable_string_data(string * s)340 inline char* mutable_string_data(string* s) {
341 #ifdef LANG_CXX11
342 // This should be simpler & faster than string_as_array() because the latter
343 // is guaranteed to return NULL when *s is empty, so it has to check for that.
344 return &(*s)[0];
345 #else
346 return string_as_array(s);
347 #endif
348 }
349
350 } // namespace io
351 } // namespace protobuf
352
353 } // namespace google
354 #endif // GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_LITE_H__
355