File Coverage

blib/lib/PerlIO/via/gzip.pm
Criterion Covered Total %
statement 65 69 94.2
branch 18 30 60.0
condition 2 3 66.6
subroutine 15 15 100.0
pod 0 5 0.0
total 100 122 81.9


line stmt bran cond sub pod time code
1             #$Id: gzip.pm 517 2009-10-23 15:52:21Z maj $
2              
3             package PerlIO::via::gzip;
4 1     1   21834 use strict;
  1         2  
  1         26  
5 1     1   5 use warnings;
  1         2  
  1         24  
6 1     1   4 use PerlIO;
  1         19  
  1         7  
7 1     1   486 use IO::Compress::Gzip qw(:constants);
  1         46440  
  1         302  
8 1     1   820 use IO::Uncompress::Gunzip;
  1         17161  
  1         64  
9 1     1   13 use Carp;
  1         3  
  1         165  
10             our $VERSION = '0.03';
11             our $COMPRESSION_LEVEL = Z_DEFAULT_COMPRESSION;
12             our $COMPRESSION_STRATEGY = Z_DEFAULT_STRATEGY;
13             our $BLOCK_SIZE = 4096;
14             our $INSTANCE = 128;
15              
16             sub PUSHED {
17 1     1   9 no strict qw(refs);
  1         3  
  1         858  
18 2     2 0 13123 my ($class, $mode) = @_;
19 2         7 my $stat;
20 2         9 my $self = {
21             instance => $INSTANCE++
22             };
23 2         10 $mode =~ s/\+//;
24 2         8 $self->{mode} = $mode;
25 2         119 bless $self, $_[0];
26             }
27              
28              
29             # open hook
30             sub FILENO {
31 2     2   11 my ($self, $fh) = @_;
32 2 50       15 if ( !defined $self->{inited} ) {
33 2         26 my $via = grep (/via/, PerlIO::get_layers($fh));
34             my $compress = ($self->{mode} =~ /w|a/ and !$via) ||
35 2   66     38 ($self->{mode} =~ /r/ and $via);
36 2         9 $self->{fileno} = fileno($fh); # nec. to kick fileno hooks
37 2         7 $self->{inited} = 1;
38 2 100       9 if ($compress) {
39 1         13 $self->{gzip} = IO::Compress::Gzip->new(
40             $fh,
41             AutoClose => 1,
42             Level => $COMPRESSION_LEVEL,
43             Strategy => $COMPRESSION_STRATEGY,
44             );
45 1 50       2808 croak "via(gzip) [OPEN]: Couldn't create compression stream" unless ($self->{gzip});
46 1         11 $self->{gzip}->autoflush(1);
47             }
48             else {
49 1         9 $self->{gunzip} = IO::Uncompress::Gunzip->new(
50             $fh,
51             BlockSize => $BLOCK_SIZE
52             );
53              
54 1 50       1651 croak "via(gzip) [OPEN]: Couldn't create decompression stream" unless ($self->{gunzip});
55             }
56              
57             }
58 2         202 $self->{fileno};
59             }
60              
61             sub FILL {
62 2     2 0 97 my ($self, $fh) = @_;
63 2         9 return $self->Readline($fh);
64             }
65              
66             sub Readline {
67 2     2 0 4 my $self = shift;
68 2 50       18 if ($self->{gzip}) {
    50          
69 0         0 return $self->{gzip}->getline;
70             }
71             elsif ($self->{gunzip}) {
72 2         17 return $self->{gunzip}->getline;
73             }
74             else {
75 0         0 croak "via(gzip) [FILL]: handle not initialized";
76             }
77             }
78              
79             sub WRITE {
80 2006     2006   156174 my ($self, $buf, $fh) = @_;
81 2006         4609 return $self->Write($fh, $buf);
82             }
83              
84             sub Write {
85 2006     2006 0 3822 my ($self, $fh, $buf) = @_;
86 2006         3054 my $ret;
87 2006 50       5485 if ($self->{gunzip}) {
    50          
88 0         0 return $self->{gunzip}->write($buf);
89             }
90             elsif ($self->{gzip}) {
91 2006         5143 return $self->{gzip}->print($buf);
92             }
93             else {
94 0         0 croak "via(gzip) [WRITE]: handle not initialized";
95             }
96             }
97              
98             sub FLUSH {
99 2     2 0 667 my ($self, $fh) = @_;
100 2 50       11 return -1 unless $self->{inited} == 1; # not open yet
101 2 50       23 $fh && $fh->flush;
102 2 100       9 if ($self->{gzip}) {
103 1         16 $self->{gzip}->flush;
104             # to get a valid gzip file, the Gzip handle must
105             # be closed before the source handle.
106             # if FLUSH is called on via handle close,
107             # the source handle is closed before we
108             # can get to it in via::gzip::CLOSE.
109             # So we are closing the Gzip handle here.
110 1         499 $self->{gzip}->close;
111 1         199 1;
112             }
113 2         19 return 0;
114             }
115              
116             sub CLOSE {
117 2     2   6 my ($self, $fh) = @_;
118 2 50       11 return -1 unless $self->{inited}; # not open yet
119 2 100       6 if ($self->{gzip}) {
120             # the $self->{gzip} handle was already flushed and
121             # closed by FLUSH
122 1 50       18 return $fh ? $fh->close : 0;
123             }
124             else {
125 1         6 $self->{gunzip}->close;
126 1 50       29 return $fh->close if $fh;
127             }
128             }
129              
130             1;
131             __END__