bytestream.cc
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 
32 
33 #include <string.h>
34 #include <algorithm>
35 
37 
38 namespace google {
39 namespace protobuf {
40 namespace strings {
41 
42 void ByteSource::CopyTo(ByteSink* sink, size_t n) {
43  while (n > 0) {
44  StringPiece fragment = Peek();
45  if (fragment.empty()) {
46  GOOGLE_LOG(DFATAL) << "ByteSource::CopyTo() overran input.";
47  break;
48  }
49  std::size_t fragment_size = std::min<std::size_t>(n, fragment.size());
50  sink->Append(fragment.data(), fragment_size);
51  Skip(fragment_size);
52  n -= fragment_size;
53  }
54 }
55 
56 void ByteSink::Flush() {}
57 
58 void UncheckedArrayByteSink::Append(const char* data, size_t n) {
59  if (data != dest_) {
60  // Catch cases where the pointer returned by GetAppendBuffer() was modified.
61  GOOGLE_DCHECK(!(dest_ <= data && data < (dest_ + n)))
62  << "Append() data[] overlaps with dest_[]";
63  memcpy(dest_, data, n);
64  }
65  dest_ += n;
66 }
67 
68 CheckedArrayByteSink::CheckedArrayByteSink(char* outbuf, size_t capacity)
69  : outbuf_(outbuf), capacity_(capacity), size_(0), overflowed_(false) {
70 }
71 
72 void CheckedArrayByteSink::Append(const char* bytes, size_t n) {
73  size_t available = capacity_ - size_;
74  if (n > available) {
75  n = available;
76  overflowed_ = true;
77  }
78  if (n > 0 && bytes != (outbuf_ + size_)) {
79  // Catch cases where the pointer returned by GetAppendBuffer() was modified.
81  << "Append() bytes[] overlaps with outbuf_[]";
82  memcpy(outbuf_ + size_, bytes, n);
83  }
84  size_ += n;
85 }
86 
88  : capacity_(estimated_size),
89  buf_(new char[estimated_size]),
90  size_(0) {
91 }
92 
94  delete[] buf_; // Just in case the user didn't call GetBuffer.
95 }
96 
97 void GrowingArrayByteSink::Append(const char* bytes, size_t n) {
98  size_t available = capacity_ - size_;
99  if (bytes != (buf_ + size_)) {
100  // Catch cases where the pointer returned by GetAppendBuffer() was modified.
101  // We need to test for this before calling Expand() which may reallocate.
102  GOOGLE_DCHECK(!(buf_ <= bytes && bytes < (buf_ + capacity_)))
103  << "Append() bytes[] overlaps with buf_[]";
104  }
105  if (n > available) {
106  Expand(n - available);
107  }
108  if (n > 0 && bytes != (buf_ + size_)) {
109  memcpy(buf_ + size_, bytes, n);
110  }
111  size_ += n;
112 }
113 
114 char* GrowingArrayByteSink::GetBuffer(size_t* nbytes) {
115  ShrinkToFit();
116  char* b = buf_;
117  *nbytes = size_;
118  buf_ = nullptr;
119  size_ = capacity_ = 0;
120  return b;
121 }
122 
123 void GrowingArrayByteSink::Expand(size_t amount) { // Expand by at least 50%.
124  size_t new_capacity = std::max(capacity_ + amount, (3 * capacity_) / 2);
125  char* bigger = new char[new_capacity];
126  memcpy(bigger, buf_, size_);
127  delete[] buf_;
128  buf_ = bigger;
129  capacity_ = new_capacity;
130 }
131 
133  // Shrink only if the buffer is large and size_ is less than 3/4
134  // of capacity_.
135  if (capacity_ > 256 && size_ < (3 * capacity_) / 4) {
136  char* just_enough = new char[size_];
137  memcpy(just_enough, buf_, size_);
138  delete[] buf_;
139  buf_ = just_enough;
140  capacity_ = size_;
141  }
142 }
143 
144 void StringByteSink::Append(const char* data, size_t n) {
145  dest_->append(data, n);
146 }
147 
149  return input_.size();
150 }
151 
153  return input_;
154 }
155 
156 void ArrayByteSource::Skip(size_t n) {
159 }
160 
162  : source_(source),
163  limit_(limit) {
164 }
165 
167  size_t available = source_->Available();
168  if (available > limit_) {
169  available = limit_;
170  }
171 
172  return available;
173 }
174 
176  StringPiece piece(source_->Peek());
177  if (piece.size() > limit_) {
178  piece.set(piece.data(), limit_);
179  }
180 
181  return piece;
182 }
183 
184 void LimitByteSource::Skip(size_t n) {
186  source_->Skip(n);
187  limit_ -= n;
188 }
189 
190 void LimitByteSource::CopyTo(ByteSink *sink, size_t n) {
192  source_->CopyTo(sink, n);
193  limit_ -= n;
194 }
195 
196 } // namespace strings
197 } // namespace protobuf
198 } // namespace google
google::protobuf::strings::StringByteSink::dest_
string * dest_
Definition: bytestream.h:263
google::protobuf::strings::GrowingArrayByteSink::GrowingArrayByteSink
GrowingArrayByteSink(size_t estimated_size)
Definition: bytestream.cc:87
google::protobuf::strings::ByteSink::Flush
virtual void Flush()
Definition: bytestream.cc:56
google::protobuf::strings::LimitByteSource::Peek
virtual StringPiece Peek() override
Definition: bytestream.cc:175
google::protobuf::strings::ByteSink::Append
virtual void Append(const char *bytes, size_t n)=0
google::protobuf::strings::GrowingArrayByteSink::Append
virtual void Append(const char *bytes, size_t n) override
Definition: bytestream.cc:97
google::protobuf::StringPiece::data
const char * data() const
Definition: stringpiece.h:247
GOOGLE_DCHECK_LE
#define GOOGLE_DCHECK_LE
Definition: logging.h:199
GOOGLE_DCHECK
#define GOOGLE_DCHECK
Definition: logging.h:194
google::protobuf::strings::CheckedArrayByteSink::capacity_
const size_t capacity_
Definition: bytestream.h:205
strings
GLsizei const GLchar *const * strings
Definition: glcorearb.h:4046
google::protobuf::strings::LimitByteSource::limit_
size_t limit_
Definition: bytestream.h:342
google::protobuf::strings::ByteSource::Available
virtual size_t Available() const =0
google::protobuf::strings::ArrayByteSource::Peek
virtual StringPiece Peek() override
Definition: bytestream.cc:152
bytes
uint8 bytes[10]
Definition: coded_stream_unittest.cc:153
b
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:3228
google::protobuf::strings::LimitByteSource::Available
virtual size_t Available() const override
Definition: bytestream.cc:166
google::protobuf::strings::ArrayByteSource::Available
virtual size_t Available() const override
Definition: bytestream.cc:148
google::protobuf::strings::LimitByteSource::Skip
virtual void Skip(size_t n) override
Definition: bytestream.cc:184
google::protobuf::StringPiece::set
void set(const char *data, stringpiece_ssize_type len)
Definition: stringpiece.h:257
google::protobuf::strings::LimitByteSource::LimitByteSource
LimitByteSource(ByteSource *source, size_t limit)
Definition: bytestream.cc:161
google::protobuf::StringPiece::empty
bool empty() const
Definition: stringpiece.h:250
google::protobuf::strings::GrowingArrayByteSink::Expand
void Expand(size_t amount)
Definition: bytestream.cc:123
google::protobuf::strings::ByteSource::Peek
virtual StringPiece Peek()=0
google::protobuf::StringPiece
Definition: stringpiece.h:180
GOOGLE_LOG
#define GOOGLE_LOG(LEVEL)
Definition: logging.h:146
google::protobuf::strings::GrowingArrayByteSink::capacity_
size_t capacity_
Definition: bytestream.h:241
google::protobuf::strings::GrowingArrayByteSink::~GrowingArrayByteSink
virtual ~GrowingArrayByteSink()
Definition: bytestream.cc:93
source
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:3072
google::protobuf::strings::ByteSource::Skip
virtual void Skip(size_t n)=0
google::protobuf::strings::StringByteSink::Append
virtual void Append(const char *data, size_t n) override
Definition: bytestream.cc:144
google::protobuf::StringPiece::remove_prefix
void remove_prefix(stringpiece_ssize_type n)
Definition: stringpiece.h:282
google::protobuf::strings::LimitByteSource::CopyTo
virtual void CopyTo(ByteSink *sink, size_t n) override
Definition: bytestream.cc:190
n
GLdouble n
Definition: glcorearb.h:4153
google::protobuf::strings::CheckedArrayByteSink::outbuf_
char * outbuf_
Definition: bytestream.h:204
google::protobuf::strings::ByteSource::CopyTo
virtual void CopyTo(ByteSink *sink, size_t n)
Definition: bytestream.cc:42
google::protobuf::strings::CheckedArrayByteSink::Append
virtual void Append(const char *bytes, size_t n) override
Definition: bytestream.cc:72
google::protobuf::strings::UncheckedArrayByteSink::dest_
char * dest_
Definition: bytestream.h:175
google::protobuf::strings::CheckedArrayByteSink::size_
size_t size_
Definition: bytestream.h:206
google::protobuf::strings::ArrayByteSource::Skip
virtual void Skip(size_t n) override
Definition: bytestream.cc:156
logging.h
google::protobuf::strings::GrowingArrayByteSink::GetBuffer
char * GetBuffer(size_t *nbytes)
Definition: bytestream.cc:114
google::protobuf::strings::GrowingArrayByteSink::buf_
char * buf_
Definition: bytestream.h:242
google::protobuf::strings::ByteSink
Definition: bytestream.h:78
google::protobuf::strings::GrowingArrayByteSink::ShrinkToFit
void ShrinkToFit()
Definition: bytestream.cc:132
google::protobuf::strings::CheckedArrayByteSink::CheckedArrayByteSink
CheckedArrayByteSink(char *outbuf, size_t capacity)
Definition: bytestream.cc:68
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: glcorearb.h:2879
google::protobuf::strings::ByteSource
Definition: bytestream.h:107
google::protobuf::strings::ArrayByteSource::input_
StringPiece input_
Definition: bytestream.h:305
google::protobuf::strings::GrowingArrayByteSink::size_
size_t size_
Definition: bytestream.h:243
bytestream.h
false
#define false
Definition: cJSON.c:70
google::protobuf::StringPiece::size
stringpiece_ssize_type size() const
Definition: stringpiece.h:248
google::protobuf::strings::UncheckedArrayByteSink::Append
virtual void Append(const char *data, size_t n) override
Definition: bytestream.cc:58
google::protobuf::strings::CheckedArrayByteSink::overflowed_
bool overflowed_
Definition: bytestream.h:207
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf::strings::LimitByteSource::source_
ByteSource * source_
Definition: bytestream.h:341


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