Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 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 | require 'TTransport' |
| 21 | |
| 22 | TMemoryBuffer = TTransportBase:new{ |
| 23 | __type = 'TMemoryBuffer', |
| 24 | buffer = '', |
| 25 | bufferSize = 1024, |
| 26 | wPos = 0, |
| 27 | rPos = 0 |
| 28 | } |
| 29 | function TMemoryBuffer:isOpen() |
| 30 | return 1 |
| 31 | end |
| 32 | function TMemoryBuffer:open() end |
| 33 | function TMemoryBuffer:close() end |
| 34 | |
| 35 | function TMemoryBuffer:peak() |
| 36 | return self.rPos < self.wPos |
| 37 | end |
| 38 | |
| 39 | function TMemoryBuffer:getBuffer() |
| 40 | return self.buffer |
| 41 | end |
| 42 | |
| 43 | function TMemoryBuffer:resetBuffer(buf) |
| 44 | if buf then |
| 45 | self.buffer = buf |
| 46 | self.bufferSize = string.len(buf) |
| 47 | else |
| 48 | self.buffer = '' |
| 49 | self.bufferSize = 1024 |
| 50 | end |
| 51 | self.wPos = string.len(buf) |
| 52 | self.rPos = 0 |
| 53 | end |
| 54 | |
| 55 | function TMemoryBuffer:available() |
| 56 | return self.wPos - self.rPos |
| 57 | end |
| 58 | |
| 59 | function TMemoryBuffer:read(len) |
| 60 | local avail = self:available() |
| 61 | if avail == 0 then |
| 62 | return '' |
| 63 | end |
| 64 | |
| 65 | if avail < len then |
| 66 | len = avail |
| 67 | end |
| 68 | |
winsweet | 38a1c66 | 2014-12-08 15:51:45 +0800 | [diff] [blame^] | 69 | local val = string.sub(self.buffer, self.rPos + 1, self.rPos + len) |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 70 | self.rPos = self.rPos + len |
| 71 | return val |
| 72 | end |
| 73 | |
| 74 | function TMemoryBuffer:readAll(len) |
| 75 | local avail = self:available() |
| 76 | |
| 77 | if avail < len then |
| 78 | local msg = string.format('Attempt to readAll(%d) found only %d available', |
| 79 | len, avail) |
| 80 | terror(TTransportException:new{message = msg}) |
| 81 | end |
| 82 | -- read should block so we don't need a loop here |
| 83 | return self:read(len) |
| 84 | end |
| 85 | |
| 86 | function TMemoryBuffer:write(buf) |
winsweet | 38a1c66 | 2014-12-08 15:51:45 +0800 | [diff] [blame^] | 87 | self.buffer = self.buffer .. buf |
| 88 | self.wPos = self.wPos + string.len(buf) |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 89 | end |
| 90 | |
| 91 | function TMemoryBuffer:flush() end |