1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
use strict;
use warnings;
package Data::ParseBinary::Stream::FileReader;
our @ISA = qw{Data::ParseBinary::Stream::Reader};
__PACKAGE__->_registerStreamType("File");
sub new {
my ($class, $fh) = @_;
my $self = {
handle => $fh,
};
return bless $self, $class;
}
sub ReadBytes {
my ($self, $count) = @_;
my $buf = '';
while ((my $buf_len = length($buf)) < $count) {
my $bytes_read = read($self->{handle}, $buf, $count - $buf_len, $buf_len);
die "Error: End of file" if $bytes_read == 0;
}
return $buf;
}
sub ReadBits {
my ($self, $bitcount) = @_;
return $self->_readBitsForByteStream($bitcount);
}
sub tell {
my $self = shift;
return CORE::tell($self->{handle});
}
sub seek {
my ($self, $newpos) = @_;
CORE::seek($self->{handle}, $newpos, 0);
}
sub isBitStream { return 0 };
package Data::ParseBinary::Stream::FileWriter;
our @ISA = qw{Data::ParseBinary::Stream::Writer};
__PACKAGE__->_registerStreamType("File");
sub new {
my ($class, $fh) = @_;
my $self = {
handle => $fh,
};
return bless $self, $class;
}
sub WriteBytes {
my ($self, $data) = @_;
print { $self->{handle} } $data;
}
sub WriteBits {
my ($self, $bitdata) = @_;
return $self->_writeBitsForByteStream($bitdata);
}
sub tell {
my $self = shift;
return CORE::tell($self->{handle});
}
sub seek {
my ($self, $newpos) = @_;
CORE::seek($self->{handle}, $newpos, 0);
}
sub Flush {
my $self = shift;
}
sub isBitStream { return 0 };
1;
|