File Coverage

blib/lib/Data/ParseBinary/Stream/StringBuffer.pm
Criterion Covered Total %
statement 39 39 100.0
branch 4 4 100.0
condition n/a
subroutine 7 7 100.0
pod n/a
total 50 50 100.0


line stmt bran cond sub pod time code
1 5     5   24 use strict;
  5         7  
  5         147  
2 5     5   23 use warnings;
  5         7  
  5         2292  
3            
4             package Data::ParseBinary::Stream::StringBufferReader;
5             our @ISA = qw{Data::ParseBinary::Stream::StringRefReader Data::ParseBinary::Stream::WrapperBase};
6            
7             __PACKAGE__->_registerStreamType("StringBuffer");
8            
9             sub new {
10 3     3   6 my ($class, $sub_stream) = @_;
11 3         3 my $string = '';
12 3         22 my $self = $class->SUPER::new(\$string);
13 3         32 $self->_warping($sub_stream);
14 3         10 return $self;
15             }
16            
17             sub ReadBytes {
18 10     10   13 my ($self, $count) = @_;
19 10 100       28 if ($self->{location} + $count > $self->{length}) {
20 8         14 my $more_needed = $count - ($self->{length} - $self->{location});
21 8         26 my $new_bytes = $self->{ss}->ReadBytes($more_needed);
22 7         8 ${ $self->{data} } .= $new_bytes;
  7         15  
23 7         11 $self->{length} += $more_needed;
24             }
25 9         39 return $self->SUPER::ReadBytes($count);
26             }
27            
28             sub seek {
29 6     6   9 my ($self, $newpos) = @_;
30 6 100       17 if ($newpos > $self->{length}) {
31 2         4 my $more_needed = $newpos - $self->{length};
32 2         16 my $new_bytes = $self->{ss}->ReadBytes($more_needed);
33 2         3 ${ $self->{data} } .= $new_bytes;
  2         3  
34 2         4 $self->{length} += $more_needed;
35             }
36 6         24 $self->SUPER::seek($newpos);
37             }
38            
39             package Data::ParseBinary::Stream::StringBufferWriter;
40             our @ISA = qw{Data::ParseBinary::Stream::StringRefWriter Data::ParseBinary::Stream::WrapperBase};
41            
42             __PACKAGE__->_registerStreamType("StringBuffer");
43            
44             sub new {
45 1     1   2 my ($class, $sub_stream) = @_;
46 1         2 my $source = '';
47 1         8 my $self = $class->SUPER::new(\$source);
48 1         9 $self->_warping($sub_stream);
49 1         4 return $self;
50             }
51            
52             sub Flush {
53 2     2   4 my $self = shift;
54 2         11 my $data = $self->SUPER::Flush();
55 2         13 $self->{ss}->WriteBytes($$data);
56 2         3 my $empty_string = '';
57 2         4 $self->{data} = \$empty_string;
58 2         3 $self->{offset} = 0;
59 2         9 return $self->{ss}->Flush();
60             }
61            
62            
63             1;