File Coverage

blib/lib/AMF/Connection/OutputStream.pm
Criterion Covered Total %
statement 12 50 24.0
branch 0 14 0.0
condition 0 9 0.0
subroutine 4 12 33.3
pod 0 8 0.0
total 16 93 17.2


line stmt bran cond sub pod time code
1             package AMF::Connection::OutputStream;
2              
3 1     1   6 use strict;
  1         2  
  1         37  
4 1     1   6 use Carp;
  1         6  
  1         1006  
5              
6             our $storable_with_options;
7              
8 1     1   1021 eval "use Storable::AMF0 0.84";
  1         4316  
  1         41  
9             if ($@)
10             {
11             $storable_with_options = 0;
12             }
13             else
14             {
15             $storable_with_options = 1;
16             }
17              
18 1     1   2379 eval "use Storable::AMF3 0.84";
  1         1080  
  1         30  
19             if ($@)
20             {
21             $storable_with_options = 0;
22             }
23             else
24             {
25             $storable_with_options = 1;
26             }
27              
28             sub new {
29 0     0 0   my $proto = shift;
30 0   0       my $class = ref($proto) || $proto;
31              
32 0           my ($storable_amf_options) = @_;
33            
34 0           my $self = {
35             'stream' => ''
36             };
37              
38 0 0         if (defined $storable_amf_options)
39             {
40 0 0         if ($Storable::AMF::VERSION < 0.84)
41             {
42 0           croak "Storable::AMF 0.84 or newer needed to set stream options\n";
43             }
44 0           $self->{'options'} = Storable::AMF::parse_option ($storable_amf_options);
45             }
46              
47 0           return bless($self, $class);
48             };
49              
50             sub writeBuffer {
51 0     0 0   my ($class, $str) = @_;
52              
53 0           $class->{'stream'}.=$str;
54             };
55              
56             sub writeByte {
57 0     0 0   my ($class, $byte) = @_;
58              
59 0           $class->{'stream'}.=pack("c",$byte);
60             };
61              
62             sub writeInt {
63 0     0 0   my ($class, $int) = @_;
64              
65 0           $class->{'stream'}.=pack("n",$int);
66             };
67              
68             sub writeDouble {
69 0     0 0   my ($class, $double) = @_;
70              
71 0           my $bin = pack("d",$double);
72 0           my @testEndian = unpack("C*",pack("S*",256));
73 0           my $bigEndian = !$testEndian[1]==1;
74 0 0         $bin = reverse($bin)
75             if ($bigEndian);
76              
77 0           $class->{'stream'}.=$bin;
78             };
79              
80             sub writeLong {
81 0     0 0   my ($class, $long) = @_;
82              
83 0           $class->{'stream'}.=pack("N",$long);
84             };
85              
86             sub getStreamData {
87 0     0 0   my ($class) = @_;
88            
89 0           return $class->{'stream'};
90             };
91              
92             # wrtie an AMF entity
93             sub writeAMFData {
94 0     0 0   my ($class,$encoding,$data) = @_;
95              
96 0           local $@ = undef;
97              
98 0           my $bytes;
99 0 0         if($encoding == 3 ) {
100 0 0 0       if ($storable_with_options == 0
101             || not defined $class->{'options'})
102             {
103 0           $bytes = Storable::AMF3::freeze($data);
104             }
105             else
106             {
107 0           $bytes = Storable::AMF3::freeze($data, $class->{'options'});
108             }
109 0           $class->writeByte(0x11);
110             } else {
111 0 0 0       if ($storable_with_options == 0
112             || not defined $class->{'options'})
113             {
114 0           $bytes = Storable::AMF0::freeze($data);
115             }
116             else
117             {
118 0           $bytes = Storable::AMF0::freeze($data, $class->{'options'});
119             }
120             };
121              
122 0 0         croak "Can not write AMF".$encoding." data starting from position ".$class->{'cursor'}." of input - reason: ".$@ ."\n"
123             if($@);
124              
125 0           $class->writeBuffer($bytes);
126             };
127              
128             1;
129             __END__