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
20use 5.10.0;
21use strict;
22use warnings;
23
24use Thrift;
25use Thrift::Transport;
26
27package Thrift::MemoryBuffer;
28use base('Thrift::Transport');
29use version 0.77; our $VERSION = version->declare("$Thrift::VERSION");
30
31sub new
32{
33    my $classname = shift;
34
35    my $bufferSize= shift || 1024;
36
37    my $self = {
38        buffer     => '',
39        bufferSize => $bufferSize,
40        wPos       => 0,
41        rPos       => 0,
42    };
43
44    return bless($self,$classname);
45}
46
47sub isOpen
48{
49    return 1;
50}
51
52sub open
53{
54
55}
56
57sub close
58{
59
60}
61
62sub peek
63{
64    my $self = shift;
65    return($self->{rPos} < $self->{wPos});
66}
67
68
69sub getBuffer
70{
71    my $self = shift;
72    return $self->{buffer};
73}
74
75sub resetBuffer
76{
77    my $self = shift;
78
79    my $new_buffer  = shift || '';
80
81    $self->{buffer}     = $new_buffer;
82    $self->{bufferSize} = length($new_buffer);
83    $self->{wPos}       = length($new_buffer);
84    $self->{rPos}       = 0;
85}
86
87sub available
88{
89    my $self = shift;
90    return ($self->{wPos} - $self->{rPos});
91}
92
93sub read
94{
95    my $self = shift;
96    my $len  = shift;
97    my $ret;
98
99    my $avail = ($self->{wPos} - $self->{rPos});
100    return '' if $avail == 0;
101
102    #how much to give
103    my $give = $len;
104    $give = $avail if $avail < $len;
105
106    $ret = substr($self->{buffer},$self->{rPos},$give);
107
108    $self->{rPos} += $give;
109
110    return $ret;
111}
112
113sub readAll
114{
115    my $self = shift;
116    my $len  = shift;
117
118    my $avail = ($self->{wPos} - $self->{rPos});
119    if ($avail < $len) {
120        die Thrift::TTransportException->new("Attempt to readAll($len) found only $avail available",
121                                    Thrift::TTransportException::END_OF_FILE);
122    }
123
124    my $data = '';
125    my $got = 0;
126
127    while (($got = length($data)) < $len) {
128        $data .= $self->read($len - $got);
129    }
130
131    return $data;
132}
133
134sub write
135{
136    my $self = shift;
137    my $buf  = shift;
138
139    $self->{buffer} .= $buf;
140    $self->{wPos}   += length($buf);
141}
142
143sub flush
144{
145
146}
147
1481;
149