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
20require 'TTransport'
21
22TBufferedTransport = TTransportBase:new{
23  __type = 'TBufferedTransport',
24  rBufSize = 2048,
25  wBufSize = 2048,
26  wBuf = '',
27  rBuf = ''
28}
29
30function TBufferedTransport:new(obj)
31  if ttype(obj) ~= 'table' then
32    error(ttype(self) .. 'must be initialized with a table')
33  end
34
35  -- Ensure a transport is provided
36  if not obj.trans then
37    error('You must provide ' .. ttype(self) .. ' with a trans')
38  end
39
40  return TTransportBase.new(self, obj)
41end
42
43function TBufferedTransport:isOpen()
44  return self.trans:isOpen()
45end
46
47function TBufferedTransport:open()
48  return self.trans:open()
49end
50
51function TBufferedTransport:close()
52  return self.trans:close()
53end
54
55function TBufferedTransport:read(len)
56  return self.trans:read(len)
57end
58
59function TBufferedTransport:readAll(len)
60  return self.trans:readAll(len)
61end
62
63function TBufferedTransport:write(buf)
64  self.wBuf = self.wBuf .. buf
65  if string.len(self.wBuf) >= self.wBufSize then
66    self.trans:write(self.wBuf)
67    self.wBuf = ''
68  end
69end
70
71function TBufferedTransport:flush()
72  if string.len(self.wBuf) > 0 then
73    self.trans:write(self.wBuf)
74    self.wBuf = ''
75  end
76end
77
78TBufferedTransportFactory = TTransportFactoryBase:new{
79  __type = 'TBufferedTransportFactory'
80}
81
82function TBufferedTransportFactory:getTransport(trans)
83  if not trans then
84    terror(TTransportException:new{
85      message = 'Must supply a transport to ' .. ttype(self)
86    })
87  end
88  return TBufferedTransport:new{
89    trans = trans
90  }
91end
92