package MyBuffer; use strict; use vars qw|$MYBUF $MYFILL|; sub new { my $this = shift; my $class = ref ($this) || $this; my $self = bless ({ buffer_size => 1024, fill_byte => "\0", debug => 0, @_ , }, $class); return $self->init (); } sub init { my $self = shift; $MYBUF = ""; $self->{'fill_byte'} .= "\0"; $self->{'fill_byte'} = substr ($self->{'fill_byte'}, 0, 1); $MYFILL = $self->{'fill_byte'} x $self->{'buffer_size'}; return $self; } sub read { my $self = shift; my $num_bytes = shift || $self->{'buffer_size'}; my $strlen = length ($MYBUF); my $retstr = ""; # print STDERR "reading " . $num_bytes . "."; if ($strlen < $num_bytes) { $retstr = $MYBUF . substr ($MYFILL, ($self->{'buffer_length'} - ($num_bytes - $strlen))); print STDERR "UNDERRUN\n"; $MYBUF = ""; } else { $retstr = substr ($MYBUF, 0, $num_bytes, ""); } # print STDERR " length is " . length ($MYBUF) . "\n"; return $retstr; } sub write { my $self = shift; my $bytes = shift; $MYBUF .= $bytes; print STDERR "wrote " . length ($bytes) . "."; print STDERR " length is " . length ($MYBUF) . "\n"; return $self; } sub remaining { my $self = shift; return (length ($MYBUF)); } sub contents { my $self = shift; my $buf = "$MYBUF"; return $buf; } 1;