File Coverage

blib/lib/Data/ParseBinary/Streams.pm
Criterion Covered Total %
statement 72 94 76.6
branch 21 30 70.0
condition 7 12 58.3
subroutine 14 26 53.8
pod n/a
total 114 162 70.3


line stmt bran cond sub pod time code
1             package Data::ParseBinary::Stream::Reader;
2 5     5   28 use strict;
  5         8  
  5         186  
3 5     5   27 use warnings;
  5         8  
  5         6508531  
4            
5             sub _readBitsForByteStream {
6 9     9   12 my ($self, $bitcount) = @_;
7 9 50       32 my $count = int($bitcount / 8) + ($bitcount % 8 ? 1 : 0);
8 9         24 my $data = $self->ReadBytes($count);
9 9         25 my $fullbits = unpack "B*", $data;
10 9         18 my $string = substr($fullbits, -$bitcount);
11 9         23 return $string;
12             }
13            
14             sub _readBytesForBitStream {
15 2     2   6 my ($self, $count) = @_;
16 2         7 my $bitData = $self->ReadBits($count * 8);
17 2         8 my $data = pack "B*", $bitData;
18 2         7 return $data;
19             }
20            
21 0     0   0 sub isBitStream { die "unimplemented" }
22 0     0   0 sub ReadBytes { die "unimplemented" }
23 0     0   0 sub ReadBits { die "unimplemented" }
24 0     0   0 sub seek { die "unimplemented" }
25 0     0   0 sub tell { die "unimplemented" }
26            
27             our %_streamTypes;
28            
29             sub _registerStreamType {
30 35     35   66 my ($class, $typeName) = @_;
31 35         146 $_streamTypes{$typeName} = $class;
32             }
33            
34             sub CreateStreamReader {
35 191     191   6270614 my @params = @_;
36 191 50       576 if (@params == 0) {
37 0         0 die "CreateStreamReader: mush have a parameter";
38             }
39 191 100       461 if (@params == 1) {
40 140         201 my $source = $params[0];
41 140 100 33     654 if (not defined $source or not ref $source) {
42             # some value (string?). let's feed it to StringStreamWriter
43 112         624 return $_streamTypes{String}->new($source);
44             }
45 28 50       131 if (UNIVERSAL::isa($source, "Data::ParseBinary::Stream::Reader")) {
46 28         83 return $source;
47             }
48 0         0 die "Got unknown input to CreateStreamReader";
49             }
50            
51             # @params > 1
52 51         80 my $source = pop @params;
53 51         121 while (@params) {
54 52         71 my $opts = undef;
55 52         86 my $type = pop @params;
56 52 50 66     287 if ( defined( ref $type ) and @params and ( $params[-1] eq ' Opts' ) ) {
      66        
57 0         0 $opts = $type;
58 0         0 $type = pop @params;
59             }
60 52 50       159 if (not exists $_streamTypes{$type}) {
61 0         0 die "CreateStreamReader: Unrecognized type: $type";
62             }
63 52         379 $source = $_streamTypes{$type}->new($source, $opts);
64             }
65 51         182 return $source;
66             }
67            
68             sub DESTROY {
69 166     166   1199 my $self = shift;
70 166 100       1723 if ($self->can("disconnect")) {
71 35         83 $self->disconnect();
72             }
73             }
74            
75             package Data::ParseBinary::Stream::Writer;
76            
77 0     0   0 sub WriteBytes { die "unimplemented" }
78 0     0   0 sub WriteBits { die "unimplemented" }
79 0     0   0 sub Flush { die "unimplemented" }
80 0     0   0 sub isBitStream { die "unimplemented" }
81 0     0   0 sub seek { die "unimplemented" }
82 0     0   0 sub tell { die "unimplemented" }
83            
84             sub _writeBitsForByteStream {
85 9     9   12 my ($self, $bitdata) = @_;
86 9         10 my $data_len = length($bitdata);
87 9         12 my $zeros_to_add = (-$data_len) % 8;
88 9         28 my $binary = pack "B".($zeros_to_add + $data_len), ('0'x$zeros_to_add).$bitdata;
89 9         27 return $self->WriteBytes($binary);
90             }
91            
92             sub _writeBytesForBitStream {
93 2     2   5 my ($self, $data) = @_;
94 2         5 my $bitdata = unpack "B*", $data;
95 2         8 return $self->WriteBits($bitdata);
96             }
97            
98             our %_streamTypes;
99            
100             sub _registerStreamType {
101 35     35   59 my ($class, $typeName) = @_;
102 35         98 $_streamTypes{$typeName} = $class;
103             }
104            
105             sub CreateStreamWriter {
106 163     163   338 my @params = @_;
107 163 50       509 if (@params == 0) {
108 0         0 return $_streamTypes{String}->new();
109             }
110 163 100       399 if (@params == 1) {
111 129         177 my $source = $params[0];
112 129 100 66     414 if (not defined $source or not ref $source) {
113             # some value (string?). let's feed it to StringStreamWriter
114 117         2157 return $_streamTypes{String}->new($source);
115             }
116 12 50       41 if (UNIVERSAL::isa($source, "Data::ParseBinary::Stream::Writer")) {
117 12         31 return $source;
118             }
119 0         0 die "Got unknown input to CreateStreamWriter";
120             }
121            
122             # @params > 1
123 34         71 my $source = pop @params;
124 34         254 while (@params) {
125 66         102 my $type = pop @params;
126 66 50       169 if (not exists $_streamTypes{$type}) {
127 0         0 die "CreateStreamWriter: Unrecognized type: $type";
128             }
129 66         374 $source = $_streamTypes{$type}->new($source);
130             }
131 34         87 return $source;
132             }
133            
134             sub DESTROY {
135 198     198   743 my $self = shift;
136 198         575 $self->Flush();
137 198 100       1732 if ($self->can("disconnect")) {
138 62         123 $self->disconnect();
139             }
140             }
141            
142             package Data::ParseBinary::Stream::WrapperBase;
143             # this is a nixin class for streams that will warp other streams
144            
145             sub _warping {
146 97     97   133 my ($self, $sub_stream) = @_;
147 97 50       233 if ($sub_stream->{is_warped}) {
148 0         0 die "Wrapping Stream " . ref($self) . ": substream is already wraped!";
149             }
150 97         258 $self->{ss} = $sub_stream;
151 97         243 $sub_stream->{is_wraped} = 1;
152             }
153            
154             sub ss {
155 0     0   0 my $self = shift;
156 0         0 return $self->{ss};
157             }
158            
159             sub disconnect {
160 97     97   123 my ($self) = @_;
161 97         158 $self->{ss}->{is_wraped} = 0;
162 97         616 $self->{ss} = undef;
163             }
164            
165             1;