blob: 0b286878e47f4c1d31466ca2c2345aea94157a29 [file] [log] [blame]
Mark Slee7a2fb142007-05-31 02:30:34 +00001#
David Reissea2cba82009-03-30 21:35:00 +00002# 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
Mark Slee7a2fb142007-05-31 02:30:34 +00009#
David Reissea2cba82009-03-30 21:35:00 +000010# http://www.apache.org/licenses/LICENSE-2.0
Mark Slee7a2fb142007-05-31 02:30:34 +000011#
David Reissea2cba82009-03-30 21:35:00 +000012# 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.
Mark Slee7a2fb142007-05-31 02:30:34 +000018#
19
20require 5.6.0;
21use strict;
22use warnings;
23
24use Thrift;
25use Thrift::Transport;
26
27package Thrift::MemoryBuffer;
28use base('Thrift::Transport');
29
30sub new
31{
32 my $classname = shift;
33
34 my $bufferSize= shift || 1024;
35
36 my $self = {
37 buffer => '',
38 bufferSize=> $bufferSize,
39 wPos => 0,
40 rPos => 0,
41 };
42
43 return bless($self,$classname);
44}
45
46sub isOpen
47{
48 return 1;
49}
50
51sub open
52{
53
54}
55
56sub close
57{
58
59}
60
61sub peek
62{
63 my $self = shift;
64 return($self->{rPos} < $self->{wPos});
65}
66
67
68sub getBuffer
69{
70 my $self = shift;
71 return $self->{buffer};
72}
73
74sub resetBuffer
75{
76 my $self = shift;
77
78 my $new_buffer = shift || '';
79
80 $self->{buffer} = $new_buffer;
81 $self->{bufferSize} = length($new_buffer);
82 $self->{wPos} = length($new_buffer);
83 $self->{rPos} = 0;
84}
85
86sub available
87{
88 my $self = shift;
89 return ($self->{wPos} - $self->{rPos});
90}
91
92sub read
93{
94 my $self = shift;
95 my $len = shift;
96 my $ret;
97
98 my $avail = ($self->{wPos} - $self->{rPos});
99 return '' if $avail == 0;
100
101 #how much to give
102 my $give = $len;
103 $give = $avail if $avail < $len;
104
105 $ret = substr($self->{buffer},$self->{rPos},$give);
106
107 $self->{rPos} += $give;
108
109 return $ret;
110}
111
T Jake Luciani4184e2b2009-07-31 01:31:00 +0000112sub readAll
113{
114 my $self = shift;
115 my $len = shift;
116
117 my $avail = ($self->{wPos} - $self->{rPos});
118 if ($avail < $len) {
119 die new TTransportException("Attempt to readAll($len) found only $avail available");
120 }
121
122 my $data = '';
123 my $got = 0;
124
125 while (($got = length($data)) < $len) {
126 $data .= $self->read($len - $got);
127 }
128
129 return $data;
130}
131
Mark Slee7a2fb142007-05-31 02:30:34 +0000132sub write
133{
134 my $self = shift;
135 my $buf = shift;
136
137 $self->{buffer} .= $buf;
138 $self->{wPos} += length($buf);
139}
140
141sub flush
142{
143
144}
145
1461;