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
10 -- http://www.apache.org/licenses/LICENSE-2.0
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
22 TMemoryBuffer = TTransportBase:new{
23 __type = 'TMemoryBuffer',
29 function TMemoryBuffer:isOpen()
32 function TMemoryBuffer:open() end
33 function TMemoryBuffer:close() end
35 function TMemoryBuffer:peak()
36 return self.rPos < self.wPos
39 function TMemoryBuffer:getBuffer()
43 function TMemoryBuffer:resetBuffer(buf)
46 self.bufferSize = string.len(buf)
49 self.bufferSize = 1024
51 self.wPos = string.len(buf)
55 function TMemoryBuffer:available()
56 return self.wPos - self.rPos
59 function TMemoryBuffer:read(len)
60 local avail = self:available()
69 local val = string.sub(self.buffer, self.rPos + 1, self.rPos + len)
70 self.rPos = self.rPos + len
74 function TMemoryBuffer:readAll(len)
75 local avail = self:available()
78 local msg = string.format('Attempt to readAll(%d) found only %d available',
80 terror(TTransportException:new{message = msg})
82 -- read should block so we don't need a loop here
86 function TMemoryBuffer:write(buf)
87 self.buffer = self.buffer .. buf
88 self.wPos = self.wPos + string.len(buf)
91 function TMemoryBuffer:flush() end