📄 inputstream.pm
字号:
package Plucene::Store::InputStream;=head1 NAME Plucene::Store::InputStream - a random-access input stream=head1 SYNOPSIS # isa IO::File=head1 DESCRIPTIONA random-access input stream.Used for all Plucene index input operations.=head1 METHODS=cutuse strict;use warnings;use Encode qw(_utf8_on); # Magic=head2 new my $inputstream = Plucene::Store::InputStream->new($file);Create a new input stream.=cutsub new { my ($self, $filename) = @_; $self = ref $self || $self; open my $fh, '<', $filename or die "$self cannot open $filename for reading: $!"; binmode $fh; bless [ $fh, $filename ], $self;}sub DESTROY { CORE::close $_[0]->[0] }=head2 fh / read / seek / tell / getc / print / eof / closeFile operations=cutuse Carp 'croak';sub fh { croak "InputStream fh called" }sub read { CORE::read $_[0]->[0], $_[1], $_[2] }sub seek { CORE::seek $_[0]->[0], $_[1], $_[2] }sub tell { CORE::tell $_[0]->[0] }sub getc { CORE::getc $_[0]->[0] }sub print { croak "InputStream print called" }sub eof { CORE::eof $_[0]->[0] }sub close { CORE::close $_[0]->[0] }=head2 cloneThis will return a clone of this stream.=cutsub clone { my $orig = shift; my $clone = $orig->new($orig->[1]); CORE::seek($clone->[0], CORE::tell($orig->[0]), 0); return $clone;}=head2 read_byteThis will read and return a single byte.=cutsub read_byte { # unpack C ord CORE::getc $_[0]->[0];}=head2 read_intThis will read four bytes and return an integer.=cutsub read_int { # unpack N my $buf; CORE::read $_[0]->[0], $buf, 4; return unpack("N", $buf);}=head2 read_vintThis will read an integer stored in a variable-length format.=cutsub read_vint { # unpack w my $b = ord CORE::getc($_[0]->[0]); my $i = $b & 0x7F; for (my $s = 7 ; ($b & 0x80) != 0 ; $s += 7) { $b = ord CORE::getc $_[0]->[0]; $i |= ($b & 0x7F) << $s; } return $i;}=head2 read_vlongThis will read a long and stored in variable-length format=cut*read_vlong = *read_vint; # Perl is type-agnostic. ;) # Yes, but most Perls don't handle 64bit integers!=head2 read_stringThis will read a string.=cutsub read_string { # unpack w/a my $length = $_[0]->read_vint(); my $utf8; CORE::read $_[0]->[0], $utf8, $length; _utf8_on($utf8); return $utf8;}=head2 read_longThis will read eight bytes and return a long.=cutsub read_long { # unpack NN my $int_a = $_[0]->read_int; my $int_b = $_[0]->read_int; # Order is important! # and size matters! return (($int_a << 32) | ($int_b & 0xFFFFFFFF));}1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -