File Coverage

blib/lib/POE/Filter/Zlib/Stream.pm
Criterion Covered Total %
statement 71 83 85.5
branch 17 26 65.3
condition 2 3 66.6
subroutine 11 12 91.6
pod 6 6 100.0
total 107 130 82.3


line stmt bran cond sub pod time code
1             package POE::Filter::Zlib::Stream;
2             $POE::Filter::Zlib::Stream::VERSION = '2.04';
3 3     3   22764 use strict;
  3         7  
  3         78  
4 3     3   12 use warnings;
  3         4  
  3         77  
5 3     3   12 use Carp;
  3         3  
  3         217  
6 3     3   1641 use Compress::Raw::Zlib qw(Z_OK Z_STREAM_END Z_FINISH Z_SYNC_FLUSH);
  3         10632  
  3         262  
7 3     3   16 use vars qw($VERSION);
  3         4  
  3         138  
8 3     3   14 use base qw(POE::Filter);
  3         3  
  3         1670  
9              
10             $VERSION = '2.02';
11              
12             sub new {
13 7     7 1 917 my $type = shift;
14 7 50       23 croak "$type requires an even number of parameters" if @_ % 2;
15 7         13 my $buffer = { @_ };
16 7         9 $buffer->{ lc $_ } = delete $buffer->{ $_ } for keys %{ $buffer };
  7         37  
17 7         23 $buffer->{BUFFER} = '';
18 7 100       22 delete $buffer->{deflateopts} unless ref ( $buffer->{deflateopts} ) eq 'HASH';
19 7         9 $buffer->{d} = Compress::Raw::Zlib::Deflate->new( %{ $buffer->{deflateopts} } );
  7         37  
20 7 50       2430 unless ( $buffer->{d} ) {
21 0         0 warn "Failed to create deflate stream\n";
22 0         0 return;
23             }
24 7 50       21 delete $buffer->{inflateopts} unless ref ( $buffer->{inflateopts} ) eq 'HASH';
25 7         9 $buffer->{i} = Compress::Raw::Zlib::Inflate->new ( %{ $buffer->{inflateopts} } );
  7         39  
26 7 50       1041 unless ( $buffer->{i} ) {
27 0         0 warn "Failed to create inflate stream\n";
28 0         0 return;
29             }
30 7 100       16 if (not defined $buffer->{flushtype}) {
31 3         13 $buffer->{flushtype} = Z_SYNC_FLUSH;
32             }
33 7         76 return bless $buffer, $type;
34             }
35              
36             # use inherited get() from POE::Filter
37              
38             sub get_one_start {
39 10     10 1 104 my ($self, $raw_lines) = @_;
40 10         14 $self->{BUFFER} .= join '', @{ $raw_lines };
  10         33  
41             }
42              
43             sub get_one {
44 32     32 1 340 my $self = shift;
45              
46 32 100       72 return [ ] unless length $self->{BUFFER};
47 20         15 my ($status, $out);
48 20         131 $status = $self->{i}->inflate( \$self->{BUFFER}, $out );
49              
50 20 50 66     40 unless ( $status == Z_OK or $status == Z_STREAM_END ) {
51 0         0 warn "Couldn\'t inflate buffer\n";
52 0         0 return [ ];
53             }
54 20 100       292 if ($status == Z_STREAM_END) {
55 17         39 $self->{i} = Compress::Raw::Zlib::Inflate->new ( %{ $self->{inflateopts} } );
  17         46  
56             }
57 20         2336 return [ $out ];
58             }
59              
60             sub get_pending {
61 0     0 1 0 my $self = shift;
62 0 0       0 return $self->{BUFFER} ? [ $self->{BUFFER} ] : undef;
63             }
64              
65             sub put {
66 10     10 1 5389 my ($self, $events) = @_;
67 10         14 my $raw_lines = [];
68              
69 10         43 foreach my $event (@$events) {
70 22         22 my ($dstat, $dout);
71 22         166 $dstat = $self->{d}->deflate( $event, $dout );
72 22 50       73 unless ( $dstat == Z_OK ) {
73 0         0 warn "(data) Couldn\'t deflate: $event\n($dstat)";
74 0         0 next;
75             }
76 22         113 my ($fout,$fstat);
77 22         290 $fstat = $self->{d}->flush( $fout, $self->{flushtype} );
78 22 50       38 unless ( $fstat == Z_OK ) {
79 0         0 warn "(flush) Couldn\'t flush/deflate: $event\n";
80 0         0 next;
81             }
82 22 100       79 if ($self->{flushtype} == Z_FINISH) {
83 17         43 $self->{d} = Compress::Raw::Zlib::Deflate->new ( %{ $self->{deflateopts} } );
  17         46  
84             }
85 22         4569 push @$raw_lines, $dout . $fout;
86             }
87 10         25 return $raw_lines;
88             }
89              
90             sub clone {
91 2     2 1 9 my $self = shift;
92 2         6 my $nself = { };
93 2         2 $nself->{$_} = $self->{$_} for keys %{ $self };
  2         19  
94 2         4 $nself->{d} = Compress::Raw::Zlib::Deflate->new( %{ $nself->{deflateopts} } );
  2         7  
95 2         605 $nself->{i} = Compress::Raw::Zlib::Inflate->new( %{ $nself->{inflateopts} } );
  2         9  
96 2         281 $nself->{BUFFER} = '';
97 2         6 return bless $nself, ref $self;
98             }
99              
100             1;
101              
102             __END__