package IO::Zlib;
-$VERSION = "1.04_02";
+$VERSION = "1.05_01";
=head1 NAME
We must know how much to read.
-=item IO::Zlib::READ: OFFSET is not supported
-
-Offsets of gzipped streams are not supported.
-
=item IO::Zlib::WRITE: too long LENGTH
The LENGTH must be less than or equal to the buffer size.
*IO::Handle::gzread = \&gzread_external;
*IO::Handle::gzwrite = \&gzwrite_external;
*IO::Handle::gzreadline = \&gzreadline_external;
+ *IO::Handle::gzeof = \&gzeof_external;
*IO::Handle::gzclose = \&gzclose_external;
$gzip_used = 1;
} else {
*gzread = \&Compress::Zlib::gzread;
*gzwrite = \&Compress::Zlib::gzwrite;
*gzreadline = \&Compress::Zlib::gzreadline;
+ *gzeof = \&Compress::Zlib::gzeof;
}
$aliased = 1;
}
croak "IO::Zlib::open: needs a filename" unless defined($filename);
$self->{'file'} = gzopen($filename,$mode);
- $self->{'eof'} = 0;
return defined($self->{'file'}) ? $self : undef;
}
my $status = $self->{'file'}->gzclose();
delete $self->{'file'};
- delete $self->{'eof'};
return ($status == 0) ? 1 : undef;
}
my $self = shift;
my $bufref = \$_[0];
my $nbytes = $_[1];
- my $offset = $_[2];
+ my $offset = $_[2] || 0;
croak "IO::Zlib::READ: NBYTES must be specified" unless defined($nbytes);
- croak "IO::Zlib::READ: OFFSET is not supported" if defined($offset) && $offset != 0;
- return 0 if $self->{'eof'};
+ $$bufref = "" unless defined($$bufref);
- my $bytesread = $self->{'file'}->gzread($$bufref,$nbytes);
+ my $bytesread = $self->{'file'}->gzread(substr($$bufref,$offset),$nbytes);
return undef if $bytesread < 0;
- $self->{'eof'} = 1 if $bytesread < $nbytes;
-
return $bytesread;
}
my $length = shift;
my $offset = shift;
- croak "IO::Zlib::WRITE: too long LENGTH" unless $length <= length($buf);
- croak "IO::Zlib::WRITE: OFFSET not supported" if defined($offset) && $offset != 0;
+ croak "IO::Zlib::WRITE: too long LENGTH" unless $offset + $length <= length($buf);
- return $self->{'file'}->gzwrite(substr($buf,0,$length));
+ return $self->{'file'}->gzwrite(substr($buf,$offset,$length));
}
sub EOF
{
my $self = shift;
- return $self->{'eof'};
+ return $self->{'file'}->gzeof();
+}
+
+sub FILENO
+{
+ return undef;
}
sub new
return defined $_[1] ? length($_[1]) : -1;
}
+sub gzeof_external {
+ return eof($_[0]);
+}
+
sub gzclose_external {
close($_[0]);
# I am not entirely certain why this is needed but it seems
$name="test.gz";
-print "1..15\n";
+print "1..17\n";
$hello = <<EOM ;
hello world
ok(7, $file->open($name, "rb"));
ok(8, !$file->eof());
ok(9, $file->read($uncomp, 1024) == length($hello));
-ok(10, $file->eof());
-ok(11, $file->opened());
-ok(12, $file->close());
-ok(13, !$file->opened());
+ok(10, $uncomp eq $hello);
+ok(11, $file->eof());
+ok(12, $file->opened());
+ok(13, $file->close());
+ok(14, !$file->opened());
-unlink($name);
+$file = IO::Zlib->new($name, "rb");
+ok(15, $file->read($uncomp, 1024, length($uncomp)) == length($hello));
+ok(16, $uncomp eq $hello . $hello);
+$file->close();
-ok(14, $hello eq $uncomp);
+unlink($name);
-ok(15, !defined(IO::Zlib->new($name, "rb")));
+ok(17, !defined(IO::Zlib->new($name, "rb")));
$name="test.gz";
-print "1..19\n";
+print "1..23\n";
@text = (<<EOM, <<EOM, <<EOM, <<EOM) ;
this is line 1
ok(3, $file->close());
ok(4, $file = IO::Zlib->new($name, "rb"));
-ok(5, $file->getline() eq $text[0]);
-ok(6, $file->getline() eq $text[1]);
-ok(7, $file->getline() eq $text[2]);
-ok(8, $file->getline() eq $text[3]);
-ok(9, !defined($file->getline()));
-ok(10, $file->close());
-
-ok(11, $file = IO::Zlib->new($name, "rb"));
+ok(5, !$file->eof());
+ok(6, $file->getline() eq $text[0]);
+ok(7, $file->getline() eq $text[1]);
+ok(8, $file->getline() eq $text[2]);
+ok(9, $file->getline() eq $text[3]);
+ok(10, !defined($file->getline()));
+ok(11, $file->eof());
+ok(12, $file->close());
+
+ok(13, $file = IO::Zlib->new($name, "rb"));
+ok(14, !$file->eof());
eval '$file->getlines';
-ok(12, $@ =~ /^IO::Zlib::getlines: must be called in list context /);
-ok(13, @lines = $file->getlines());
-ok(14, @lines == @text);
-ok(15, $lines[0] eq $text[0]);
-ok(16, $lines[1] eq $text[1]);
-ok(17, $lines[2] eq $text[2]);
-ok(18, $lines[3] eq $text[3]);
-ok(19, $file->close());
+ok(15, $@ =~ /^IO::Zlib::getlines: must be called in list context /);
+ok(16, @lines = $file->getlines());
+ok(17, @lines == @text);
+ok(18, $lines[0] eq $text[0]);
+ok(19, $lines[1] eq $text[1]);
+ok(20, $lines[2] eq $text[2]);
+ok(21, $lines[3] eq $text[3]);
+ok(22, $file->eof());
+ok(23, $file->close());
unlink($name);