File Coverage

blib/lib/Flash/FLAP/IO/InputStream.pm
Criterion Covered Total %
statement 3 55 5.4
branch 0 8 0.0
condition n/a
subroutine 1 8 12.5
pod 3 7 42.8
total 7 78 8.9


line stmt bran cond sub pod time code
1             package Flash::FLAP::IO::InputStream;
2             # Copyright (c) 2003 by Vsevolod (Simon) Ilyushchenko. All rights reserved.
3             # This program is free software; you can redistribute it and/or modify it
4             # under the same terms as Perl itself.
5             # The code is based on the -PHP project (http://amfphp.sourceforge.net/)
6              
7              
8             =head1 NAME
9              
10             Flash::FLAP::IO::InputStream
11              
12             =head1 DESCRIPTION
13              
14             InputStream package built to handle getting the binary data from the raw input stream.
15              
16             =head1 CHANGES
17              
18             =head2 Sat Mar 13 16:39:29 EST 2004
19              
20             =item Changed calls to ord() in readByte() and concatenation readDouble()
21             to prevent the appearance of the "uninitialized" warning.
22              
23             =head2 Sun May 11 16:41:52 EDT 2003
24              
25             =item Rewrote readInt to get rid of the "uninitialized" warning when reading bytes of value 0.
26              
27             =cut
28              
29 1     1   5 use strict;
  1         2  
  1         791  
30              
31             #InputStream constructor
32             #arguments $rd raw data stream
33             sub new
34             {
35 0     0 0   my ($proto, $rd )=@_;
36 0           my $self={};
37 0           bless $self, $proto;
38 0           $self->{current_byte}=0;
39             # store the stream in this object
40 0           my @array = split //, $rd;
41 0           $self->{raw_data} = \@array;
42             # grab the total length of this stream
43 0           $self->{content_length} = length($self->{raw_data});
44 0           return $self;
45             }
46              
47              
48             # returns a single byte value.
49             sub readByte
50             {
51 0     0 1   my ($self)=@_;
52             # return the next byte
53 0           my $nextByte = $self->{raw_data}->[$self->{current_byte}];
54 0           my $result;
55 0 0         $result = ord($nextByte) if $nextByte;
56 0           $self->{current_byte} += 1;
57 0           return $result;
58             }
59              
60             # returns the value of 2 bytes
61             sub readInt
62             {
63 0     0 1   my ($self)=@_;
64             # read the next 2 bytes, shift and add
65            
66 0           my $thisByte = $self->{raw_data}->[$self->{current_byte}];
67 0           my $nextByte = $self->{raw_data}->[$self->{current_byte}+1];
68              
69 0 0         my $thisNum = $thisByte ? ord($thisByte) : 0;
70 0 0         my $nextNum = $nextByte ? ord($nextByte) : 0;
71              
72 0           my $result = (($thisNum) << 8) | $nextNum;
73              
74 0           $self->{current_byte} += 2;
75 0           return $result;
76             }
77              
78             # returns the value of 4 bytes
79             sub readLong
80             {
81 0     0 0   my ($self)=@_;
82 0           my $byte1 = $self->{current_byte};
83 0           my $byte2 = $self->{current_byte}+1;
84 0           my $byte3 = $self->{current_byte}+2;
85 0           my $byte4 = $self->{current_byte}+3;
86             # read the next 4 bytes, shift and add
87 0           my $result = ((ord($self->{raw_data}->[$byte1]) << 24) |
88             (ord($self->{raw_data}->[$byte2]) << 16) |
89             (ord($self->{raw_data}->[$byte3]) << 8) |
90             ord($self->{raw_data}->[$byte4]));
91 0           $self->{current_byte} = $self->{current_byte} + 4;
92 0           return $result;
93             }
94              
95             # returns the value of 8 bytes
96             sub readDouble
97             {
98 0     0 1   my ($self)=@_;
99             # container to store the reversed bytes
100 0           my $invertedBytes = "";
101             # create a loop with a backwards index
102 0           for(my $i = 7 ; $i >= 0 ; $i--)
103             {
104             # grab the bytes in reverse order from the backwards index
105 0           my $nextByte = $self->{raw_data}->[$self->{current_byte}+$i];
106 0 0         $invertedBytes .= $nextByte if $nextByte;
107             }
108             # move the seek head forward 8 bytes
109 0           $self->{current_byte} += 8;
110             # unpack the bytes
111 0           my @zz = unpack("d", $invertedBytes);
112             # return the number from the associative array
113 0           return $zz[0];
114             }
115              
116              
117             # returns a UTF string
118             sub readUTF
119             {
120 0     0 0   my ($self) = @_;
121             # get the length of the string (1st 2 bytes)
122 0           my $length = $self->readInt();
123             # grab the string
124 0           my @slice = @{$self->{raw_data}}[$self->{current_byte}.. $self->{current_byte}+$length-1];
  0            
125 0           my $val = join "", @slice;
126             # move the seek head to the end of the string
127 0           $self->{current_byte} += $length;
128             # return the string
129 0           return $val;
130             }
131              
132             # returns a UTF string with a LONG representing the length
133             sub readLongUTF
134             {
135 0     0 0   my ($self) = @_;
136             # get the length of the string (1st 4 bytes)
137 0           my $length = $self->readLong();
138             # grab the string
139 0           my @slice = @{$self->{raw_data}}[$self->{current_byte} .. $self->{current_byte}+$length-1];
  0            
140 0           my $val = join "", @slice;
141             # move the seek head to the end of the string
142 0           $self->{current_byte} += $length;
143             # return the string
144 0           return $val;
145             }
146              
147             1;