zero_copy_stream_impl.h
Go to the documentation of this file.
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 only included in the full (non-lite)
37 // protobuf library. These implementations include Unix file descriptors
38 // and C++ iostreams. See also: zero_copy_stream_impl_lite.h
39 
40 #ifndef GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_H__
41 #define GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_H__
42 
43 #include <iosfwd>
44 #include <string>
48 
49 
50 #include <google/protobuf/port_def.inc>
51 
52 namespace google {
53 namespace protobuf {
54 namespace io {
55 
56 // ===================================================================
57 
58 // A ZeroCopyInputStream which reads from a file descriptor.
59 //
60 // FileInputStream is preferred over using an ifstream with IstreamInputStream.
61 // The latter will introduce an extra layer of buffering, harming performance.
62 // Also, it's conceivable that FileInputStream could someday be enhanced
63 // to use zero-copy file descriptors on OSs which support them.
64 class PROTOBUF_EXPORT FileInputStream : public ZeroCopyInputStream {
65  public:
66  // Creates a stream that reads from the given Unix file descriptor.
67  // If a block_size is given, it specifies the number of bytes that
68  // should be read and returned with each call to Next(). Otherwise,
69  // a reasonable default is used.
70  explicit FileInputStream(int file_descriptor, int block_size = -1);
71 
72  // Flushes any buffers and closes the underlying file. Returns false if
73  // an error occurs during the process; use GetErrno() to examine the error.
74  // Even if an error occurs, the file descriptor is closed when this returns.
75  bool Close();
76 
77  // By default, the file descriptor is not closed when the stream is
78  // destroyed. Call SetCloseOnDelete(true) to change that. WARNING:
79  // This leaves no way for the caller to detect if close() fails. If
80  // detecting close() errors is important to you, you should arrange
81  // to close the descriptor yourself.
82  void SetCloseOnDelete(bool value) { copying_input_.SetCloseOnDelete(value); }
83 
84  // If an I/O error has occurred on this file descriptor, this is the
85  // errno from that error. Otherwise, this is zero. Once an error
86  // occurs, the stream is broken and all subsequent operations will
87  // fail.
88  int GetErrno() const { return copying_input_.GetErrno(); }
89 
90  // implements ZeroCopyInputStream ----------------------------------
91  bool Next(const void** data, int* size) override;
92  void BackUp(int count) override;
93  bool Skip(int count) override;
94  int64 ByteCount() const override;
95 
96  private:
97  class PROTOBUF_EXPORT CopyingFileInputStream : public CopyingInputStream {
98  public:
99  CopyingFileInputStream(int file_descriptor);
100  ~CopyingFileInputStream() override;
101 
102  bool Close();
103  void SetCloseOnDelete(bool value) { close_on_delete_ = value; }
104  int GetErrno() const { return errno_; }
105 
106  // implements CopyingInputStream ---------------------------------
107  int Read(void* buffer, int size) override;
108  int Skip(int count) override;
109 
110  private:
111  // The file descriptor.
112  const int file_;
115 
116  // The errno of the I/O error, if one has occurred. Otherwise, zero.
117  int errno_;
118 
119  // Did we try to seek once and fail? If so, we assume this file descriptor
120  // doesn't support seeking and won't try again.
122 
124  };
125 
128 
130 };
131 
132 // ===================================================================
133 
134 // A ZeroCopyOutputStream which writes to a file descriptor.
135 //
136 // FileOutputStream is preferred over using an ofstream with
137 // OstreamOutputStream. The latter will introduce an extra layer of buffering,
138 // harming performance. Also, it's conceivable that FileOutputStream could
139 // someday be enhanced to use zero-copy file descriptors on OSs which
140 // support them.
141 class PROTOBUF_EXPORT FileOutputStream : public ZeroCopyOutputStream {
142  public:
143  // Creates a stream that writes to the given Unix file descriptor.
144  // If a block_size is given, it specifies the size of the buffers
145  // that should be returned by Next(). Otherwise, a reasonable default
146  // is used.
147  explicit FileOutputStream(int file_descriptor, int block_size = -1);
148  ~FileOutputStream() override;
149 
150  // Flushes any buffers and closes the underlying file. Returns false if
151  // an error occurs during the process; use GetErrno() to examine the error.
152  // Even if an error occurs, the file descriptor is closed when this returns.
153  bool Close();
154 
155  // Flushes FileOutputStream's buffers but does not close the
156  // underlying file. No special measures are taken to ensure that
157  // underlying operating system file object is synchronized to disk.
158  bool Flush();
159 
160  // By default, the file descriptor is not closed when the stream is
161  // destroyed. Call SetCloseOnDelete(true) to change that. WARNING:
162  // This leaves no way for the caller to detect if close() fails. If
163  // detecting close() errors is important to you, you should arrange
164  // to close the descriptor yourself.
165  void SetCloseOnDelete(bool value) { copying_output_.SetCloseOnDelete(value); }
166 
167  // If an I/O error has occurred on this file descriptor, this is the
168  // errno from that error. Otherwise, this is zero. Once an error
169  // occurs, the stream is broken and all subsequent operations will
170  // fail.
171  int GetErrno() const { return copying_output_.GetErrno(); }
172 
173  // implements ZeroCopyOutputStream ---------------------------------
174  bool Next(void** data, int* size) override;
175  void BackUp(int count) override;
176  int64 ByteCount() const override;
177 
178  private:
179  class PROTOBUF_EXPORT CopyingFileOutputStream : public CopyingOutputStream {
180  public:
181  CopyingFileOutputStream(int file_descriptor);
182  ~CopyingFileOutputStream() override;
183 
184  bool Close();
185  void SetCloseOnDelete(bool value) { close_on_delete_ = value; }
186  int GetErrno() const { return errno_; }
187 
188  // implements CopyingOutputStream --------------------------------
189  bool Write(const void* buffer, int size) override;
190 
191  private:
192  // The file descriptor.
193  const int file_;
196 
197  // The errno of the I/O error, if one has occurred. Otherwise, zero.
198  int errno_;
199 
201  };
202 
205 
207 };
208 
209 // ===================================================================
210 
211 // A ZeroCopyInputStream which reads from a C++ istream.
212 //
213 // Note that for reading files (or anything represented by a file descriptor),
214 // FileInputStream is more efficient.
215 class PROTOBUF_EXPORT IstreamInputStream : public ZeroCopyInputStream {
216  public:
217  // Creates a stream that reads from the given C++ istream.
218  // If a block_size is given, it specifies the number of bytes that
219  // should be read and returned with each call to Next(). Otherwise,
220  // a reasonable default is used.
221  explicit IstreamInputStream(std::istream* stream, int block_size = -1);
222 
223  // implements ZeroCopyInputStream ----------------------------------
224  bool Next(const void** data, int* size) override;
225  void BackUp(int count) override;
226  bool Skip(int count) override;
227  int64 ByteCount() const override;
228 
229  private:
230  class PROTOBUF_EXPORT CopyingIstreamInputStream : public CopyingInputStream {
231  public:
232  CopyingIstreamInputStream(std::istream* input);
233  ~CopyingIstreamInputStream() override;
234 
235  // implements CopyingInputStream ---------------------------------
236  int Read(void* buffer, int size) override;
237  // (We use the default implementation of Skip().)
238 
239  private:
240  // The stream.
241  std::istream* input_;
242 
244  };
245 
248 
250 };
251 
252 // ===================================================================
253 
254 // A ZeroCopyOutputStream which writes to a C++ ostream.
255 //
256 // Note that for writing files (or anything represented by a file descriptor),
257 // FileOutputStream is more efficient.
258 class PROTOBUF_EXPORT OstreamOutputStream : public ZeroCopyOutputStream {
259  public:
260  // Creates a stream that writes to the given C++ ostream.
261  // If a block_size is given, it specifies the size of the buffers
262  // that should be returned by Next(). Otherwise, a reasonable default
263  // is used.
264  explicit OstreamOutputStream(std::ostream* stream, int block_size = -1);
265  ~OstreamOutputStream() override;
266 
267  // implements ZeroCopyOutputStream ---------------------------------
268  bool Next(void** data, int* size) override;
269  void BackUp(int count) override;
270  int64 ByteCount() const override;
271 
272  private:
273  class PROTOBUF_EXPORT CopyingOstreamOutputStream
274  : public CopyingOutputStream {
275  public:
276  CopyingOstreamOutputStream(std::ostream* output);
277  ~CopyingOstreamOutputStream() override;
278 
279  // implements CopyingOutputStream --------------------------------
280  bool Write(const void* buffer, int size) override;
281 
282  private:
283  // The stream.
284  std::ostream* output_;
285 
287  };
288 
291 
293 };
294 
295 // ===================================================================
296 
297 // A ZeroCopyInputStream which reads from several other streams in sequence.
298 // ConcatenatingInputStream is unable to distinguish between end-of-stream
299 // and read errors in the underlying streams, so it assumes any errors mean
300 // end-of-stream. So, if the underlying streams fail for any other reason,
301 // ConcatenatingInputStream may do odd things. It is suggested that you do
302 // not use ConcatenatingInputStream on streams that might produce read errors
303 // other than end-of-stream.
304 class PROTOBUF_EXPORT ConcatenatingInputStream : public ZeroCopyInputStream {
305  public:
306  // All streams passed in as well as the array itself must remain valid
307  // until the ConcatenatingInputStream is destroyed.
308  ConcatenatingInputStream(ZeroCopyInputStream* const streams[], int count);
309  ~ConcatenatingInputStream() override = default;
310 
311  // implements ZeroCopyInputStream ----------------------------------
312  bool Next(const void** data, int* size) override;
313  void BackUp(int count) override;
314  bool Skip(int count) override;
315  int64 ByteCount() const override;
316 
317 
318  private:
319  // As streams are retired, streams_ is incremented and count_ is
320  // decremented.
323  int64 bytes_retired_; // Bytes read from previous streams.
324 
326 };
327 
328 // ===================================================================
329 
330 } // namespace io
331 } // namespace protobuf
332 } // namespace google
333 
334 #include <google/protobuf/port_undef.inc>
335 
336 #endif // GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_H__
google::protobuf::io::FileOutputStream::copying_output_
CopyingFileOutputStream copying_output_
Definition: zero_copy_stream_impl.h:203
google::protobuf::io::FileInputStream::CopyingFileInputStream::GetErrno
int GetErrno() const
Definition: zero_copy_stream_impl.h:104
zero_copy_stream.h
google::protobuf::io::CopyingOutputStream
Definition: zero_copy_stream_impl_lite.h:269
google::protobuf::io::FileOutputStream::CopyingFileOutputStream
Definition: zero_copy_stream_impl.h:179
google::protobuf::value
const Descriptor::ReservedRange value
Definition: src/google/protobuf/descriptor.h:1954
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
#define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName)
Definition: macros.h:40
google::protobuf::io::FileInputStream::CopyingFileInputStream::SetCloseOnDelete
void SetCloseOnDelete(bool value)
Definition: zero_copy_stream_impl.h:103
stream
GLuint GLuint stream
Definition: glcorearb.h:3946
google::protobuf::int64
int64_t int64
Definition: protobuf/src/google/protobuf/stubs/port.h:151
google::protobuf::io::FileInputStream::CopyingFileInputStream::is_closed_
bool is_closed_
Definition: zero_copy_stream_impl.h:114
google::protobuf::io::OstreamOutputStream::CopyingOstreamOutputStream::output_
std::ostream * output_
Definition: zero_copy_stream_impl.h:284
google::protobuf::io::FileOutputStream::CopyingFileOutputStream::SetCloseOnDelete
void SetCloseOnDelete(bool value)
Definition: zero_copy_stream_impl.h:185
input
std::string input
Definition: tokenizer_unittest.cc:197
google::protobuf::io::IstreamInputStream::CopyingIstreamInputStream
Definition: zero_copy_stream_impl.h:230
google::protobuf::io::FileInputStream::GetErrno
int GetErrno() const
Definition: zero_copy_stream_impl.h:88
google::protobuf::io::FileInputStream::impl_
CopyingInputStreamAdaptor impl_
Definition: zero_copy_stream_impl.h:127
google::protobuf::io::FileOutputStream::CopyingFileOutputStream::is_closed_
bool is_closed_
Definition: zero_copy_stream_impl.h:195
google::protobuf::io::FileInputStream::CopyingFileInputStream::file_
const int file_
Definition: zero_copy_stream_impl.h:112
google::protobuf::io::OstreamOutputStream::CopyingOstreamOutputStream
Definition: zero_copy_stream_impl.h:273
google::protobuf::io::ConcatenatingInputStream::bytes_retired_
int64 bytes_retired_
Definition: zero_copy_stream_impl.h:323
google::protobuf::io::ConcatenatingInputStream::streams_
ZeroCopyInputStream *const * streams_
Definition: zero_copy_stream_impl.h:321
google::protobuf::io::ConcatenatingInputStream::stream_count_
int stream_count_
Definition: zero_copy_stream_impl.h:322
testing::internal::posix::Read
int Read(int fd, void *buf, unsigned int count)
Definition: gtest-port.h:2126
google::protobuf::io::FileInputStream::CopyingFileInputStream
Definition: zero_copy_stream_impl.h:97
google::protobuf::io::FileInputStream::SetCloseOnDelete
void SetCloseOnDelete(bool value)
Definition: zero_copy_stream_impl.h:82
google::protobuf::io::OstreamOutputStream
Definition: zero_copy_stream_impl.h:258
google::protobuf::io::FileOutputStream::GetErrno
int GetErrno() const
Definition: zero_copy_stream_impl.h:171
google::protobuf::io::OstreamOutputStream::impl_
CopyingOutputStreamAdaptor impl_
Definition: zero_copy_stream_impl.h:290
google::protobuf::io::FileOutputStream::CopyingFileOutputStream::errno_
int errno_
Definition: zero_copy_stream_impl.h:198
google::protobuf::io::FileOutputStream::CopyingFileOutputStream::GetErrno
int GetErrno() const
Definition: zero_copy_stream_impl.h:186
buffer
Definition: buffer_processor.h:43
google::protobuf::io::FileInputStream
Definition: zero_copy_stream_impl.h:64
google::protobuf::io::FileInputStream::copying_input_
CopyingFileInputStream copying_input_
Definition: zero_copy_stream_impl.h:126
google::protobuf::io::ConcatenatingInputStream
Definition: zero_copy_stream_impl.h:304
google::protobuf::io::IstreamInputStream
Definition: zero_copy_stream_impl.h:215
google::protobuf::io::FileOutputStream::CopyingFileOutputStream::file_
const int file_
Definition: zero_copy_stream_impl.h:193
google::protobuf::io::CopyingOutputStreamAdaptor
Definition: zero_copy_stream_impl_lite.h:285
google::protobuf::io::FileOutputStream::SetCloseOnDelete
void SetCloseOnDelete(bool value)
Definition: zero_copy_stream_impl.h:165
google::protobuf::io::OstreamOutputStream::copying_output_
CopyingOstreamOutputStream copying_output_
Definition: zero_copy_stream_impl.h:289
google::protobuf::io::ZeroCopyInputStream
Definition: zero_copy_stream.h:126
google::protobuf::io::FileOutputStream
Definition: zero_copy_stream_impl.h:141
zero_copy_stream_impl_lite.h
testing::internal::posix::Close
int Close(int fd)
Definition: gtest-port.h:2132
google::protobuf::io::IstreamInputStream::CopyingIstreamInputStream::input_
std::istream * input_
Definition: zero_copy_stream_impl.h:241
testing::internal::posix::Write
int Write(int fd, const void *buf, unsigned int count)
Definition: gtest-port.h:2129
google::protobuf::io::FileOutputStream::CopyingFileOutputStream::close_on_delete_
bool close_on_delete_
Definition: zero_copy_stream_impl.h:194
google::protobuf::io::FileInputStream::CopyingFileInputStream::previous_seek_failed_
bool previous_seek_failed_
Definition: zero_copy_stream_impl.h:121
common.h
pump.Skip
def Skip(lines, pos, regex)
Definition: pump.py:261
google::protobuf::io::FileInputStream::CopyingFileInputStream::close_on_delete_
bool close_on_delete_
Definition: zero_copy_stream_impl.h:113
size
GLsizeiptr size
Definition: glcorearb.h:2943
google::protobuf::io::ZeroCopyOutputStream
Definition: zero_copy_stream.h:183
google::protobuf::io::FileInputStream::CopyingFileInputStream::errno_
int errno_
Definition: zero_copy_stream_impl.h:117
google::protobuf::io::IstreamInputStream::impl_
CopyingInputStreamAdaptor impl_
Definition: zero_copy_stream_impl.h:247
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: glcorearb.h:2879
google::protobuf::io::CopyingInputStream
Definition: zero_copy_stream_impl_lite.h:175
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition: ruby/ext/google/protobuf_c/upb.h:10503
count
GLint GLsizei count
Definition: glcorearb.h:2830
google::protobuf::io::IstreamInputStream::copying_input_
CopyingIstreamInputStream copying_input_
Definition: zero_copy_stream_impl.h:246
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf::io::FileOutputStream::impl_
CopyingOutputStreamAdaptor impl_
Definition: zero_copy_stream_impl.h:204
google::protobuf::io::CopyingInputStreamAdaptor
Definition: zero_copy_stream_impl_lite.h:201


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:07:02