File Coverage

blib/lib/AMF/Perl/IO/OutputStream.pm
Criterion Covered Total %
statement 3 37 8.1
branch 0 8 0.0
condition n/a
subroutine 1 9 11.1
pod 1 8 12.5
total 5 62 8.0


line stmt bran cond sub pod time code
1             package AMF::Perl::IO::OutputStream;
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             =head1 NAME
8              
9             AMF::Perl::IO::OutputStream
10              
11             =head1 DESCRIPTION
12              
13             Class used to convert the perl stuff into binary
14              
15             ==head1 CHANGES
16              
17             =head2 Sun Sep 19 12:59:11 EDT 2004
18             =item Check for (defined $s) and not just ($s) in writeUTF.
19             =item Write string length as long if it is over 65535.
20              
21             =head2 Sun Jun 20 13:32:31 EDT 2004
22             =item Added $s="" unless $s in writeUTF to avoid warnings.
23              
24             =head2 Sun Jul 11 18:45:40 EDT 2004
25              
26             =item Added the check for endianness.
27              
28              
29             =cut
30              
31 1     1   6 use strict;
  1         2  
  1         539  
32              
33              
34             #OutputStream constructor
35             sub new
36             {
37 0     0 0   my ($proto)=@_;
38             # the buffer
39 0           my $self = {};
40 0           bless $self, $proto;
41 0           $self->{outBuffer} = "";
42 0 0         if (unpack("h*", pack("s", 1)) =~ /01/)
43             {
44 0           $self->{byteorder} = 'big-endian';
45             }
46             else
47             {
48 0           $self->{byteorder} = 'little-endian';
49             }
50 0           return $self;
51             }
52              
53             # write a single byte
54             sub writeByte
55             {
56 0     0 0   my ($self, $b)=@_;
57             # use pack with the c flag
58 0           $self->{outBuffer} .= pack("c", $b);
59             }
60             # write 2 bytes
61             sub writeInt
62             {
63 0     0 0   my ($self, $n) = @_;
64             # use pack with the n flag
65 0           $self->{outBuffer} .= pack("n", $n);
66             }
67             # write 4 bytes
68             sub writeLong
69             {
70 0     0 0   my ($self, $l)=@_;
71             # use pack with the N flag
72 0           $self->{outBuffer} .= pack("N", $l);
73             }
74             # write a string
75             sub writeUTF
76             {
77 0     0 1   my ($self, $s)=@_;
78 0 0         $s = "" unless defined($s);
79             # write the string length - max 65536
80 0 0         if (length($s) <= 65535)
81             {
82 0           $self->writeInt(length($s));
83             }
84             else
85             {
86 0           $self->writeLong(length($s));
87             }
88             # write the string chars
89 0           $self->{outBuffer} .= $s;
90             }
91             #write a long string
92             sub writeLongUTF
93             {
94 0     0 0   my ($self, $s)=@_;
95             # write the string length - max 65536
96 0           $self->writeLong(length($s));
97             # write the string chars
98 0           $self->{outBuffer} .= $s;
99             }
100              
101             sub writeDouble
102             {
103 0     0 0   my ($self, $d)=@_;
104             # pack the bytes
105 0           my $b = pack("d", $d);
106 0           my @b = split //, $b;
107             # atleast on *nix the bytes have to be reversed
108             # maybe not on windows, in php there in not flag to
109             # force whether the bytes are little or big endian
110             # for a double
111 0           my $r = "";
112             # reverse the bytes
113 0 0         if ($self->{byteorder} eq 'little-endian')
114             {
115 0           for(my $byte = 7 ; $byte >= 0 ; $byte--)
116             {
117 0           $r .= $b[$byte];
118             }
119             }
120             else
121             {
122 0           $r = $b;
123             }
124             # add the bytes to the output
125 0           $self->{outBuffer} .= $r;
126             }
127              
128             # send the output buffer
129             sub flush
130             {
131 0     0 0   my ($self) = @_;
132             # flush typically empties the buffer
133             # but this is not a persistent pipe so it's not needed really here
134             # plus it's useful to be able to flush to a file and to the client simultaneously
135             # with out have to create another method just to peek at the buffer contents.
136 0           return $self->{outBuffer};
137             }
138              
139             1;