Mark Slee | 7a2fb14 | 2007-05-31 02:30:34 +0000 | [diff] [blame] | 1 | # |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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 |
Mark Slee | 7a2fb14 | 2007-05-31 02:30:34 +0000 | [diff] [blame] | 9 | # |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
Mark Slee | 7a2fb14 | 2007-05-31 02:30:34 +0000 | [diff] [blame] | 11 | # |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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. |
Mark Slee | 7a2fb14 | 2007-05-31 02:30:34 +0000 | [diff] [blame] | 18 | # |
| 19 | |
| 20 | require 5.6.0; |
| 21 | use strict; |
| 22 | use warnings; |
| 23 | |
| 24 | use Thrift; |
| 25 | use Thrift::Transport; |
| 26 | |
| 27 | package Thrift::MemoryBuffer; |
| 28 | use base('Thrift::Transport'); |
| 29 | |
| 30 | sub 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 | |
| 46 | sub isOpen |
| 47 | { |
| 48 | return 1; |
| 49 | } |
| 50 | |
| 51 | sub open |
| 52 | { |
| 53 | |
| 54 | } |
| 55 | |
| 56 | sub close |
| 57 | { |
| 58 | |
| 59 | } |
| 60 | |
| 61 | sub peek |
| 62 | { |
| 63 | my $self = shift; |
| 64 | return($self->{rPos} < $self->{wPos}); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | sub getBuffer |
| 69 | { |
| 70 | my $self = shift; |
| 71 | return $self->{buffer}; |
| 72 | } |
| 73 | |
| 74 | sub 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 | |
| 86 | sub available |
| 87 | { |
| 88 | my $self = shift; |
| 89 | return ($self->{wPos} - $self->{rPos}); |
| 90 | } |
| 91 | |
| 92 | sub 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 Luciani | 4184e2b | 2009-07-31 01:31:00 +0000 | [diff] [blame] | 112 | sub 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 Slee | 7a2fb14 | 2007-05-31 02:30:34 +0000 | [diff] [blame] | 132 | sub write |
| 133 | { |
| 134 | my $self = shift; |
| 135 | my $buf = shift; |
| 136 | |
| 137 | $self->{buffer} .= $buf; |
| 138 | $self->{wPos} += length($buf); |
| 139 | } |
| 140 | |
| 141 | sub flush |
| 142 | { |
| 143 | |
| 144 | } |
| 145 | |
| 146 | 1; |