Subversion Repositories SmartDukaan

Rev

Rev 30 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 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 <transport/TTransportUtils.h>
21
 
22
using std::string;
23
 
24
namespace apache { namespace thrift { namespace transport {
25
 
26
uint32_t TPipedTransport::read(uint8_t* buf, uint32_t len) {
27
  uint32_t need = len;
28
 
29
  // We don't have enough data yet
30
  if (rLen_-rPos_ < need) {
31
    // Copy out whatever we have
32
    if (rLen_-rPos_ > 0) {
33
      memcpy(buf, rBuf_+rPos_, rLen_-rPos_);
34
      need -= rLen_-rPos_;
35
      buf += rLen_-rPos_;
36
      rPos_ = rLen_;
37
    }
38
 
39
    // Double the size of the underlying buffer if it is full
40
    if (rLen_ == rBufSize_) {
41
      rBufSize_ *=2;
42
      rBuf_ = (uint8_t *)std::realloc(rBuf_, sizeof(uint8_t) * rBufSize_);
43
    }
44
 
45
    // try to fill up the buffer
46
    rLen_ += srcTrans_->read(rBuf_+rPos_, rBufSize_ - rPos_);
47
  }
48
 
49
 
50
  // Hand over whatever we have
51
  uint32_t give = need;
52
  if (rLen_-rPos_ < give) {
53
    give = rLen_-rPos_;
54
  }
55
  if (give > 0) {
56
    memcpy(buf, rBuf_+rPos_, give);
57
    rPos_ += give;
58
    need -= give;
59
  }
60
 
61
  return (len - need);
62
}
63
 
64
void TPipedTransport::write(const uint8_t* buf, uint32_t len) {
65
  if (len == 0) {
66
    return;
67
  }
68
 
69
  // Make the buffer as big as it needs to be
70
  if ((len + wLen_) >= wBufSize_) {
71
    uint32_t newBufSize = wBufSize_*2;
72
    while ((len + wLen_) >= newBufSize) {
73
      newBufSize *= 2;
74
    }
75
    wBuf_ = (uint8_t *)std::realloc(wBuf_, sizeof(uint8_t) * newBufSize);
76
    wBufSize_ = newBufSize;
77
  }
78
 
79
  // Copy into the buffer
80
  memcpy(wBuf_ + wLen_, buf, len);
81
  wLen_ += len;
82
}
83
 
84
void TPipedTransport::flush()  {
85
  // Write out any data waiting in the write buffer
86
  if (wLen_ > 0) {
87
    srcTrans_->write(wBuf_, wLen_);
88
    wLen_ = 0;
89
  }
90
 
91
  // Flush the underlying transport
92
  srcTrans_->flush();
93
}
94
 
95
TPipedFileReaderTransport::TPipedFileReaderTransport(boost::shared_ptr<TFileReaderTransport> srcTrans, boost::shared_ptr<TTransport> dstTrans)
96
  : TPipedTransport(srcTrans, dstTrans),
97
    srcTrans_(srcTrans) {
98
}
99
 
100
TPipedFileReaderTransport::~TPipedFileReaderTransport() {
101
}
102
 
103
bool TPipedFileReaderTransport::isOpen() {
104
  return TPipedTransport::isOpen();
105
}
106
 
107
bool TPipedFileReaderTransport::peek() {
108
  return TPipedTransport::peek();
109
}
110
 
111
void TPipedFileReaderTransport::open() {
112
  TPipedTransport::open();
113
}
114
 
115
void TPipedFileReaderTransport::close() {
116
  TPipedTransport::close();
117
}
118
 
119
uint32_t TPipedFileReaderTransport::read(uint8_t* buf, uint32_t len) {
120
  return TPipedTransport::read(buf, len);
121
}
122
 
123
uint32_t TPipedFileReaderTransport::readAll(uint8_t* buf, uint32_t len) {
124
  uint32_t have = 0;
125
  uint32_t get = 0;
126
 
127
  while (have < len) {
128
    get = read(buf+have, len-have);
129
    if (get <= 0) {
130
      throw TEOFException();
131
    }
132
    have += get;
133
  }
134
 
135
  return have;
136
}
137
 
138
void TPipedFileReaderTransport::readEnd() {
139
  TPipedTransport::readEnd();
140
}
141
 
142
void TPipedFileReaderTransport::write(const uint8_t* buf, uint32_t len) {
143
  TPipedTransport::write(buf, len);
144
}
145
 
146
void TPipedFileReaderTransport::writeEnd() {
147
  TPipedTransport::writeEnd();
148
}
149
 
150
void TPipedFileReaderTransport::flush() {
151
  TPipedTransport::flush();
152
}
153
 
154
int32_t TPipedFileReaderTransport::getReadTimeout() {
155
  return srcTrans_->getReadTimeout();
156
}
157
 
158
void TPipedFileReaderTransport::setReadTimeout(int32_t readTimeout) {
159
  srcTrans_->setReadTimeout(readTimeout);
160
}
161
 
162
uint32_t TPipedFileReaderTransport::getNumChunks() {
163
  return srcTrans_->getNumChunks();
164
}
165
 
166
uint32_t TPipedFileReaderTransport::getCurChunk() {
167
  return srcTrans_->getCurChunk();
168
}
169
 
170
void TPipedFileReaderTransport::seekToChunk(int32_t chunk) {
171
  srcTrans_->seekToChunk(chunk);
172
}
173
 
174
void TPipedFileReaderTransport::seekToEnd() {
175
  srcTrans_->seekToEnd();
176
}
177
 
178
}}} // apache::thrift::transport