Subject: | make ::Zlib::Stream compatible with ::Zlib |
POE::Filter::Zlib::Stream is incompatible with POE::Filter::Zlib. The
attached patch makes ::Stream able to read ::Zlib output, and adds an
option to make its output compatible. Also snuck in an update for the
tests to use is() instead of ok() where applicable for better test error
reporting.
Subject: | pofizlib-flush.diff |
diff --git a/lib/POE/Filter/Zlib/Stream.pm b/lib/POE/Filter/Zlib/Stream.pm
index a2949c6..87b814a 100644
--- a/lib/POE/Filter/Zlib/Stream.pm
+++ b/lib/POE/Filter/Zlib/Stream.pm
@@ -27,6 +27,9 @@ sub new {
warn "Failed to create inflate stream\n";
return;
}
+ if (not defined $buffer->{flushtype}) {
+ $buffer->{flushtype} = Z_SYNC_FLUSH;
+ }
return bless $buffer, $type;
}
@@ -46,6 +49,9 @@ sub get_one {
warn "Couldn\'t inflate buffer\n";
return [ ];
}
+ if ($status == Z_STREAM_END) {
+ $self->{i} = inflateInit( %{ $self->{inflateopts} } );
+ }
return [ $out ];
}
@@ -64,11 +70,14 @@ sub put {
warn "Couldn\'t deflate: $event\n";
next;
}
- my ($fout,$fstat) = $self->{d}->flush( Z_SYNC_FLUSH );
+ my ($fout,$fstat) = $self->{d}->flush( $self->{flushtype} );
unless ( $fstat == Z_OK ) {
warn "Couldn\'t flush/deflate: $event\n";
next;
}
+ if ($self->{flushtype} == Z_FINISH) {
+ $self->{d} = deflateInit( %{ $self->{deflateopts} } );
+ }
push @$raw_lines, $dout . $fout;
}
return $raw_lines;
@@ -122,8 +131,23 @@ Ideal for streaming compressed data over sockets.
Creates a new POE::Filter::Zlib::Stream object. Takes some optional arguments:
- "deflateopts", a hashref of options to be passed to deflateInit();
- "inflateopts", a hashref of options to be passed to inflateInit();
+=over 4
+
+=item "deflateopts"
+
+a hashref of options to be passed to deflateInit();
+
+=item "inflateopts"
+
+a hashref of options to be passed to inflateInit();
+
+=item "flushtype"
+
+The type of flush to use when flushing the compressed data. Defaults to
+Z_SYNC_FLUSH so you get a single stream, but if there is a
+L<POE::Filter::Zlib> on the other end, you want to set this to Z_FINISH.
+
+=back
Consult L<Compress::Zlib> for more detail regarding these options.
diff --git a/t/02_stream.t b/t/02_stream.t
index b26bf41..5cac51b 100644
--- a/t/02_stream.t
+++ b/t/02_stream.t
@@ -14,7 +14,7 @@ foreach my $filter ( $original, $clone ) {
my $teststring = "All the little fishes";
my $compressed = $filter->put( [ $teststring ] );
my $answer = $filter->get( [ $compressed->[0] ] );
- ok( $teststring eq $answer->[0], 'Round trip test' );
+ is( $teststring, $answer->[0], 'Round trip test' );
}
@@ -32,5 +32,5 @@ my $back = $stack->get( $out );
while ( my $thing = shift @input ) {
my $thang = shift @$back;
- ok( $thing eq $thang, $thing );
+ is( $thing, $thang, $thing );
}
diff --git a/t/03_compat.t b/t/03_compat.t
new file mode 100644
index 0000000..b7f342c
--- /dev/null
+++ b/t/03_compat.t
@@ -0,0 +1,30 @@
+use Test::More tests => 4;
+use strict;
+use warnings;
+
+use Compress::Zlib;
+use POE::Filter::Zlib;
+use POE::Filter::Zlib::Stream;
+
+my @data = qw(foo bar baz);
+
+my $zl = POE::Filter::Zlib->new;
+my $zls = POE::Filter::Zlib::Stream->new;
+
+my ($zdata, $result);
+
+$zdata = $zl->put(\@data);
+$result = $zls->get($zdata);
+is_deeply (\@data, $result, "match!");
+$zdata = $zl->put(\@data);
+$result = $zls->get($zdata);
+is_deeply (\@data, $result, "match!");
+
+$zls = POE::Filter::Zlib::Stream->new (FlushType => Z_FINISH);
+$zdata = $zls->put(\@data);
+$result = $zl->get($zdata);
+is_deeply (\@data, $result, "match!");
+$zdata = $zls->put(\@data);
+$result = $zl->get($zdata);
+is_deeply (\@data, $result, "match!");
+