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