1 // Copyright 2007 Google Inc.
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License
5 // as published by the Free Software Foundation; either version 2
6 // of the License, or (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 #include <algorithm> // for copy
23 void OutStreamBuffer::FlushAndWrite(const char* bytes
, size_t n
) {
25 if (n
< buffer_
.size() / 2) {
26 WriteToBuffer(bytes
, n
);
28 master_
->Write(bytes
, n
);
32 void OutStreamBuffer::Flush() {
33 assert(IsConnected());
34 if (buffer_
.begin() < next_free_slot_
) {
35 master_
->Write(&buffer_
[0], next_free_slot_
- buffer_
.begin());
36 next_free_slot_
= buffer_
.begin();
40 void OutStreamBuffer::Reset(size_t size
) {
42 if (size
<= 0) size
= 1;
43 std::vector
<char>(size
).swap(buffer_
);
44 next_free_slot_
= buffer_
.begin();
48 void InStreamBuffer::ReadAndRefill(char* bytes
, size_t n
) {
49 size_t available
= AvailableInBuffer();
50 assert(n
> available
);
51 ReadFromBuffer(bytes
, available
);
54 if (n
< buffer_
.size() / 2) {
56 ReadFromBuffer(bytes
, n
);
58 master_
->Read(bytes
, n
);
62 void InStreamBuffer::Refill() {
63 assert(AvailableInBuffer() == 0);
64 master_
->Read(&buffer_
[0], buffer_
.size());
65 next_unused_byte_
= buffer_
.begin();
68 void InStreamBuffer::Reset(size_t size
) {
69 if (size
<= 0) size
= 1;
70 std::vector
<char>(size
).swap(buffer_
);
71 next_unused_byte_
= buffer_
.end();