File Coverage

blib/lib/Thrift/MemoryBuffer.pm
Criterion Covered Total %
statement 33 56 58.9
branch 2 6 33.3
condition 1 4 25.0
subroutine 8 17 47.0
pod 0 12 0.0
total 44 95 46.3


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