AMF-Connection

 view release on metacpan or  search on metacpan

lib/AMF/Connection/OutputStream.pm  view on Meta::CPAN

  }
else
  {
    $storable_with_options = 1;
  }

eval "use Storable::AMF3 0.84";
if ($@)
  {
    $storable_with_options = 0;
  }
else
  {
    $storable_with_options = 1;
  }

sub new {
	my $proto = shift;
	my $class = ref($proto) || $proto;

 	my ($storable_amf_options) = @_;
	
	my $self = {
		'stream' => ''
		};

	if (defined $storable_amf_options)
          {
            if ($Storable::AMF::VERSION < 0.84)
              {
                croak "Storable::AMF 0.84 or newer needed to set stream options\n";
              }
            $self->{'options'} = Storable::AMF::parse_option ($storable_amf_options);
          }

	return bless($self, $class);
	};

sub writeBuffer {
	my ($class, $str) = @_;

	$class->{'stream'}.=$str;
	};

sub writeByte {
	my ($class, $byte) = @_;

	$class->{'stream'}.=pack("c",$byte);
	};

sub writeInt {
	my ($class, $int) = @_;

	$class->{'stream'}.=pack("n",$int);
	};

sub writeDouble {
	my ($class, $double) = @_;

        my $bin = pack("d",$double);
        my @testEndian = unpack("C*",pack("S*",256));
        my $bigEndian = !$testEndian[1]==1;
        $bin = reverse($bin)
                if ($bigEndian);

	$class->{'stream'}.=$bin;
	};

sub writeLong {
	my ($class, $long) = @_;

	$class->{'stream'}.=pack("N",$long);
	};

sub getStreamData {
	my ($class) = @_;
	
	return $class->{'stream'};
	};

# wrtie an AMF entity
sub writeAMFData {
        my ($class,$encoding,$data) = @_;

	local $@ = undef;

	my $bytes;
        if($encoding == 3 ) {
		if ($storable_with_options  == 0
		    || not defined $class->{'options'})
                  {
		    $bytes = Storable::AMF3::freeze($data);
		  }
		else
		  {
		    $bytes = Storable::AMF3::freeze($data, $class->{'options'});
		  }
		$class->writeByte(0x11);
        } else {
		if ($storable_with_options  == 0
		    || not defined $class->{'options'})
                  {
		    $bytes = Storable::AMF0::freeze($data);
		  }
		else
		  {
		    $bytes = Storable::AMF0::freeze($data, $class->{'options'});
		  }
                };

	croak "Can not write AMF".$encoding." data starting from position ".$class->{'cursor'}." of input - reason: ".$@ ."\n"
		if($@);

	$class->writeBuffer($bytes);
        };

1;
__END__

=head1 NAME

AMF::Connection::OutputStream - A simple pure perl implementation of an output binary stream



( run in 0.418 second using v1.01-cache-2.11-cpan-1edf4fed603 )