1 /** @file
2 
3   A brief file description
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 /**************************************************************************
25   MIOBufferWriter.cc
26 
27 **************************************************************************/
28 
29 #include "I_MIOBufferWriter.h"
30 
31 //
32 // MIOBufferWriter
33 //
34 MIOBufferWriter &
write(const void * data_,size_t length)35 MIOBufferWriter::write(const void *data_, size_t length)
36 {
37   const char *data = static_cast<const char *>(data_);
38 
39   while (length) {
40     IOBufferBlock *iobbPtr = _miob->first_write_block();
41 
42     if (!iobbPtr) {
43       addBlock();
44 
45       iobbPtr = _miob->first_write_block();
46 
47       ink_assert(iobbPtr);
48     }
49 
50     size_t writeSize = iobbPtr->write_avail();
51 
52     if (length < writeSize) {
53       writeSize = length;
54     }
55 
56     std::memcpy(iobbPtr->end(), data, writeSize);
57     iobbPtr->fill(writeSize);
58 
59     data += writeSize;
60     length -= writeSize;
61 
62     _numWritten += writeSize;
63   }
64 
65   return *this;
66 }
67 
68 #if defined(UNIT_TEST_BUFFER_WRITER)
69 
70 // Dummys just for linkage (never called).
71 
72 std::ostream &
operator >>(std::ostream & stream) const73 MIOBufferWriter::operator>>(std::ostream &stream) const
74 {
75   return stream;
76 }
77 
78 ssize_t
operator >>(int fd) const79 MIOBufferWriter::operator>>(int fd) const
80 {
81   return 0;
82 }
83 
84 #else
85 
86 std::ostream &
operator >>(std::ostream & stream) const87 MIOBufferWriter::operator>>(std::ostream &stream) const
88 {
89   IOBufferReader *r = _miob->alloc_reader();
90   if (r) {
91     IOBufferBlock *b;
92     while (nullptr != (b = r->get_current_block())) {
93       auto n = b->read_avail();
94       stream.write(b->start(), n);
95       r->consume(n);
96     }
97     _miob->dealloc_reader(r);
98   }
99   return stream;
100 }
101 
102 ssize_t
operator >>(int fd) const103 MIOBufferWriter::operator>>(int fd) const
104 {
105   ssize_t zret           = 0;
106   IOBufferReader *reader = _miob->alloc_reader();
107   if (reader) {
108     IOBufferBlock *b;
109     while (nullptr != (b = reader->get_current_block())) {
110       auto n = b->read_avail();
111       auto r = ::write(fd, b->start(), n);
112       if (r <= 0) {
113         break;
114       } else {
115         reader->consume(r);
116         zret += r;
117       }
118     }
119     _miob->dealloc_reader(reader);
120   }
121   return zret;
122 }
123 
124 #endif // defined(UNIT_TEST_BUFFER_WRITER)
125