]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | */ | |
19 | ||
20 | #include <thrift/transport/TTransportUtils.h> | |
21 | ||
22 | using std::string; | |
23 | ||
24 | namespace apache { | |
25 | namespace thrift { | |
26 | namespace transport { | |
27 | ||
28 | uint32_t TPipedTransport::read(uint8_t* buf, uint32_t len) { | |
29 | uint32_t need = len; | |
30 | ||
31 | // We don't have enough data yet | |
32 | if (rLen_ - rPos_ < need) { | |
33 | // Copy out whatever we have | |
34 | if (rLen_ - rPos_ > 0) { | |
35 | memcpy(buf, rBuf_ + rPos_, rLen_ - rPos_); | |
36 | need -= rLen_ - rPos_; | |
37 | buf += rLen_ - rPos_; | |
38 | rPos_ = rLen_; | |
39 | } | |
40 | ||
41 | // Double the size of the underlying buffer if it is full | |
42 | if (rLen_ == rBufSize_) { | |
43 | rBufSize_ *= 2; | |
44 | auto *tmpBuf = (uint8_t*)std::realloc(rBuf_, sizeof(uint8_t) * rBufSize_); | |
45 | if (tmpBuf == nullptr) { | |
46 | throw std::bad_alloc(); | |
47 | } | |
48 | rBuf_ = tmpBuf; | |
49 | } | |
50 | ||
51 | // try to fill up the buffer | |
52 | rLen_ += srcTrans_->read(rBuf_ + rPos_, rBufSize_ - rPos_); | |
53 | } | |
54 | ||
55 | // Hand over whatever we have | |
56 | uint32_t give = need; | |
57 | if (rLen_ - rPos_ < give) { | |
58 | give = rLen_ - rPos_; | |
59 | } | |
60 | if (give > 0) { | |
61 | memcpy(buf, rBuf_ + rPos_, give); | |
62 | rPos_ += give; | |
63 | need -= give; | |
64 | } | |
65 | ||
66 | return (len - need); | |
67 | } | |
68 | ||
69 | void TPipedTransport::write(const uint8_t* buf, uint32_t len) { | |
70 | if (len == 0) { | |
71 | return; | |
72 | } | |
73 | ||
74 | // Make the buffer as big as it needs to be | |
75 | if ((len + wLen_) >= wBufSize_) { | |
76 | uint32_t newBufSize = wBufSize_ * 2; | |
77 | while ((len + wLen_) >= newBufSize) { | |
78 | newBufSize *= 2; | |
79 | } | |
80 | auto *tmpBuf= (uint8_t*)std::realloc(wBuf_, sizeof(uint8_t) * newBufSize); | |
81 | if (tmpBuf == nullptr) { | |
82 | throw std::bad_alloc(); | |
83 | } | |
84 | wBuf_ = tmpBuf; | |
85 | ||
86 | wBufSize_ = newBufSize; | |
87 | } | |
88 | ||
89 | // Copy into the buffer | |
90 | memcpy(wBuf_ + wLen_, buf, len); | |
91 | wLen_ += len; | |
92 | } | |
93 | ||
94 | void TPipedTransport::flush() { | |
95 | // Write out any data waiting in the write buffer | |
96 | if (wLen_ > 0) { | |
97 | srcTrans_->write(wBuf_, wLen_); | |
98 | wLen_ = 0; | |
99 | } | |
100 | ||
101 | // Flush the underlying transport | |
102 | srcTrans_->flush(); | |
103 | } | |
104 | ||
105 | TPipedFileReaderTransport::TPipedFileReaderTransport( | |
106 | std::shared_ptr<TFileReaderTransport> srcTrans, | |
107 | std::shared_ptr<TTransport> dstTrans) | |
108 | : TPipedTransport(srcTrans, dstTrans), srcTrans_(srcTrans) { | |
109 | } | |
110 | ||
111 | TPipedFileReaderTransport::~TPipedFileReaderTransport() = default; | |
112 | ||
113 | bool TPipedFileReaderTransport::isOpen() const { | |
114 | return TPipedTransport::isOpen(); | |
115 | } | |
116 | ||
117 | bool TPipedFileReaderTransport::peek() { | |
118 | return TPipedTransport::peek(); | |
119 | } | |
120 | ||
121 | void TPipedFileReaderTransport::open() { | |
122 | TPipedTransport::open(); | |
123 | } | |
124 | ||
125 | void TPipedFileReaderTransport::close() { | |
126 | TPipedTransport::close(); | |
127 | } | |
128 | ||
129 | uint32_t TPipedFileReaderTransport::read(uint8_t* buf, uint32_t len) { | |
130 | return TPipedTransport::read(buf, len); | |
131 | } | |
132 | ||
133 | uint32_t TPipedFileReaderTransport::readAll(uint8_t* buf, uint32_t len) { | |
134 | uint32_t have = 0; | |
135 | uint32_t get = 0; | |
136 | ||
137 | while (have < len) { | |
138 | get = read(buf + have, len - have); | |
139 | if (get <= 0) { | |
140 | throw TEOFException(); | |
141 | } | |
142 | have += get; | |
143 | } | |
144 | ||
145 | return have; | |
146 | } | |
147 | ||
148 | uint32_t TPipedFileReaderTransport::readEnd() { | |
149 | return TPipedTransport::readEnd(); | |
150 | } | |
151 | ||
152 | void TPipedFileReaderTransport::write(const uint8_t* buf, uint32_t len) { | |
153 | TPipedTransport::write(buf, len); | |
154 | } | |
155 | ||
156 | uint32_t TPipedFileReaderTransport::writeEnd() { | |
157 | return TPipedTransport::writeEnd(); | |
158 | } | |
159 | ||
160 | void TPipedFileReaderTransport::flush() { | |
161 | TPipedTransport::flush(); | |
162 | } | |
163 | ||
164 | int32_t TPipedFileReaderTransport::getReadTimeout() { | |
165 | return srcTrans_->getReadTimeout(); | |
166 | } | |
167 | ||
168 | void TPipedFileReaderTransport::setReadTimeout(int32_t readTimeout) { | |
169 | srcTrans_->setReadTimeout(readTimeout); | |
170 | } | |
171 | ||
172 | uint32_t TPipedFileReaderTransport::getNumChunks() { | |
173 | return srcTrans_->getNumChunks(); | |
174 | } | |
175 | ||
176 | uint32_t TPipedFileReaderTransport::getCurChunk() { | |
177 | return srcTrans_->getCurChunk(); | |
178 | } | |
179 | ||
180 | void TPipedFileReaderTransport::seekToChunk(int32_t chunk) { | |
181 | srcTrans_->seekToChunk(chunk); | |
182 | } | |
183 | ||
184 | void TPipedFileReaderTransport::seekToEnd() { | |
185 | srcTrans_->seekToEnd(); | |
186 | } | |
187 | } | |
188 | } | |
189 | } // apache::thrift::transport |