| Plucene documentation | Contained in the Plucene distribution. |
Plucene::Store::InputStream - a random-access input stream
# isa IO::File
A random-access input stream.Used for all Plucene index input operations.
my $inputstream = Plucene::Store::InputStream->new($file);
Create a new input stream.
File operations
This will return a clone of this stream.
This will read and return a single byte.
This will read four bytes and return an integer.
This will read an integer stored in a variable-length format.
This will read a long and stored in variable-length format
This will read a string.
This will read eight bytes and return a long.
| Plucene documentation | Contained in the Plucene distribution. |
package Plucene::Store::InputStream;
use strict; use warnings; use Encode qw(_utf8_on); # Magic
sub 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] }
use 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] }
sub clone { my $orig = shift; my $clone = $orig->new($orig->[1]); CORE::seek($clone->[0], CORE::tell($orig->[0]), 0); return $clone; }
sub read_byte { # unpack C ord CORE::getc $_[0]->[0]; }
sub read_int { # unpack N my $buf; CORE::read $_[0]->[0], $buf, 4; return unpack("N", $buf); }
sub 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; }
*read_vlong = *read_vint; # Perl is type-agnostic. ;) # Yes, but most Perls don't handle 64bit integers!
sub read_string { # unpack w/a my $length = $_[0]->read_vint(); my $utf8; CORE::read $_[0]->[0], $utf8, $length; _utf8_on($utf8); return $utf8; }
sub 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;