AMF-Perl

 view release on metacpan or  search on metacpan

lib/AMF/Perl/IO/Deserializer.pm  view on Meta::CPAN

AMF::Perl::IO::Deserializer

=head1 DESCRIPTION    

    Package used to turn the binary data into physical perl objects.

=head1 CHANGES

=head2 Sun Sep 19 13:01:35 EDT 2004

=item Patch from Kostas Chatzikokolakis about error checking of input data length.

=head2 Sat Mar 13 16:31:31 EST 2004

=item Patch from Kostas Chatzikokolakis handling encoding.

=head2 Sun Mar  9 18:17:31 EST 2003

=item The return value of readArray should be \@ret, not @ret.

=head2 Tue Mar 11 21:55:41 EST 2003

=item Fixed reading keys of objects.

=item Added floor(), as Perl lacks it.

=head2 Sun Apr  6 14:24:00 2003

=item Added code to read objects of type 8. Useful for decoding real AMF server packages, but hardly anywhere else.

=cut

use strict;

use Encode qw/from_to/;

# the number of headers in the packet
my $header_count;
# the content of the headers
my $headers;
# the number of body elements
my $body_count;
# the content of the body
my $body;

sub floor 
{
  my $n = shift;

  return int($n) - ($n < 0 ? 1: 0) * ($n != int($n) ? 1 : 0);
}


#******************** PUBLIC METHODS ****************************/

# constructor that also dserializes the raw data
sub new
{
    my ($proto, $is, $encoding)=@_;
    my $self = {};
    bless $self, $proto;
    # the object to store the deserialized data
    $self->{amfdata} = new AMF::Perl::Util::Object();
    # save the input stream in this object
    $self->{inputStream} = $is;
	# save the encoding in this object
	$self->{encoding} = $encoding;
    # read the binary header
    $self->readHeader();
    # read the binary body
    $self->readBody();
    return $self;
}

# returns the instance of the Object package
sub getObject
{
    my ($self)=@_;
    return $self->{amfdata};
}

#******************** PRIVATE METHODS ****************************/

sub readHeader
{
    my ($self)=@_;
    # ignore the first two bytes -- version or something
    $self->{inputStream}->readInt();
    # find the total number of header elements
    $self->{header_count} = $self->{inputStream}->readInt();
    # loop over all of the header elements
    while($self->{header_count}--)
    {
        my $name = $self->{inputStream}->readUTF();
        # find the must understand flag
        my $required = $self->readBoolean();
        # grab the length of the header element
        my $length = $self->{inputStream}->readLong();
        # grab the type of the element
        my $type = $self->{inputStream}->readByte();
        # turn the element into real data
        my $content = $self->readData($type);
        # save the name/value into the headers array
        $self->{amfdata}->addHeader($name, $required, $content);
    }
}

sub readBody
{
    my ($self)=@_;
    # find the total number of body elements
    $self->{body_count} = $self->{inputStream}->readInt();
    # loop over all of the body elements
    while($self->{body_count}--)
    {	
        my $method = $self->readString();
        # the target that the client understands
        my $target = $self->readString();
        # grab the length of the body element
        my $length = $self->{inputStream}->readLong();
        



( run in 0.886 second using v1.01-cache-2.11-cpan-39bf76dae61 )