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
|
|
27509
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
45
|
|
5
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
6
|
1
|
|
|
1
|
|
463
|
use PerlIO::Util; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use IO::Compress::Gzip qw(:constants); |
8
|
|
|
|
|
|
|
use IO::Uncompress::Gunzip; |
9
|
|
|
|
|
|
|
use Carp; |
10
|
|
|
|
|
|
|
our $VERSION = '0.021'; |
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
|
|
|
|
|
|
|
no strict qw(refs); |
18
|
|
|
|
|
|
|
my ($class, $mode) = @_; |
19
|
|
|
|
|
|
|
my $stat; |
20
|
|
|
|
|
|
|
my $self = { |
21
|
|
|
|
|
|
|
instance => $INSTANCE++ |
22
|
|
|
|
|
|
|
}; |
23
|
|
|
|
|
|
|
$mode =~ s/\+//; |
24
|
|
|
|
|
|
|
$self->{mode} = $mode; |
25
|
|
|
|
|
|
|
bless $self, $_[0]; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# open hook |
30
|
|
|
|
|
|
|
sub FILENO { |
31
|
|
|
|
|
|
|
my ($self, $fh) = @_; |
32
|
|
|
|
|
|
|
if ( !defined $self->{inited} ) { |
33
|
|
|
|
|
|
|
my $via = grep (/via/, $fh->get_layers); |
34
|
|
|
|
|
|
|
my $compress = ($self->{mode} =~ /w|a/ and !$via) || |
35
|
|
|
|
|
|
|
($self->{mode} =~ /r/ and $via); |
36
|
|
|
|
|
|
|
$self->{fileno} = fileno($fh); # nec. to kick fileno hooks |
37
|
|
|
|
|
|
|
$self->{inited} = 1; |
38
|
|
|
|
|
|
|
if ($compress) { |
39
|
|
|
|
|
|
|
$self->{gzip} = IO::Compress::Gzip->new( |
40
|
|
|
|
|
|
|
$fh, |
41
|
|
|
|
|
|
|
AutoClose => 1, |
42
|
|
|
|
|
|
|
Level => $COMPRESSION_LEVEL, |
43
|
|
|
|
|
|
|
Strategy => $COMPRESSION_STRATEGY, |
44
|
|
|
|
|
|
|
); |
45
|
|
|
|
|
|
|
croak "via(gzip) [OPEN]: Couldn't create compression stream" unless ($self->{gzip}); |
46
|
|
|
|
|
|
|
$self->{gzip}->autoflush(1); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
else { |
49
|
|
|
|
|
|
|
$self->{gunzip} = IO::Uncompress::Gunzip->new( |
50
|
|
|
|
|
|
|
$fh, |
51
|
|
|
|
|
|
|
BlockSize => $BLOCK_SIZE |
52
|
|
|
|
|
|
|
); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
croak "via(gzip) [OPEN]: Couldn't create decompression stream" unless ($self->{gunzip}); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
$self->{fileno}; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub FILL { |
62
|
|
|
|
|
|
|
my ($self, $fh) = @_; |
63
|
|
|
|
|
|
|
return $self->Readline($fh); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub Readline { |
67
|
|
|
|
|
|
|
my $self = shift; |
68
|
|
|
|
|
|
|
if ($self->{gzip}) { |
69
|
|
|
|
|
|
|
return $self->{gzip}->getline; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
elsif ($self->{gunzip}) { |
72
|
|
|
|
|
|
|
return $self->{gunzip}->getline; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
else { |
75
|
|
|
|
|
|
|
croak "via(gzip) [FILL]: handle not initialized"; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub WRITE { |
80
|
|
|
|
|
|
|
my ($self, $buf, $fh) = @_; |
81
|
|
|
|
|
|
|
return $self->Write($fh, $buf); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub Write { |
85
|
|
|
|
|
|
|
my ($self, $fh, $buf) = @_; |
86
|
|
|
|
|
|
|
my $ret; |
87
|
|
|
|
|
|
|
if ($self->{gunzip}) { |
88
|
|
|
|
|
|
|
return $self->{gunzip}->write($buf); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
elsif ($self->{gzip}) { |
91
|
|
|
|
|
|
|
return $self->{gzip}->print($buf); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
else { |
94
|
|
|
|
|
|
|
croak "via(gzip) [WRITE]: handle not initialized"; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub FLUSH { |
99
|
|
|
|
|
|
|
my ($self, $fh) = @_; |
100
|
|
|
|
|
|
|
return -1 unless $self->{inited} == 1; # not open yet |
101
|
|
|
|
|
|
|
$fh && $fh->flush; |
102
|
|
|
|
|
|
|
if ($self->{gzip}) { |
103
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
$self->{gzip}->close; |
111
|
|
|
|
|
|
|
1; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
return 0; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub CLOSE { |
117
|
|
|
|
|
|
|
my ($self, $fh) = @_; |
118
|
|
|
|
|
|
|
return -1 unless $self->{inited}; # not open yet |
119
|
|
|
|
|
|
|
if ($self->{gzip}) { |
120
|
|
|
|
|
|
|
# the $self->{gzip} handle was already flushed and |
121
|
|
|
|
|
|
|
# closed by FLUSH |
122
|
|
|
|
|
|
|
return $fh ? $fh->close : 0; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
else { |
125
|
|
|
|
|
|
|
$self->{gunzip}->close; |
126
|
|
|
|
|
|
|
return $fh->close if $fh; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
1; |
131
|
|
|
|
|
|
|
__END__ |