File Coverage

blib/lib/SWF/BinStream/File.pm
Criterion Covered Total %
statement 47 79 59.4
branch 6 24 25.0
condition 1 9 11.1
subroutine 11 19 57.8
pod n/a
total 65 131 49.6


line stmt bran cond sub pod time code
1             package SWF::BinStream::File;
2            
3 1     1   9 use strict;
  1         4  
  1         53  
4 1     1   8 use vars qw($VERSION);
  1         2  
  1         65  
5 1     1   17 use SWF::BinStream;
  1         3  
  1         55  
6            
7             $VERSION = '0.043';
8            
9             package SWF::BinStream::File::Read;
10            
11 1     1   7 use Carp;
  1         3  
  1         283  
12 1     1   8 use vars qw(@ISA);
  1         2  
  1         1366  
13            
14             @ISA = ('SWF::BinStream::Read');
15            
16             sub new {
17 0     0   0 my ($class, $file, $version) = @_;
18            
19 0         0 my $self = $class->SUPER::new('', \&_readfile, $version);
20 0 0       0 $self->open($file) if defined $file;
21 0         0 $self;
22             }
23            
24             sub _readfile {
25 0     0   0 my ($self, $bytes) = @_;
26 0         0 my $file = $self->{_file};
27 0         0 my $count = 0;
28            
29 0 0       0 return 0 unless defined $file;
30 0   0     0 while (not eof($file) and $count < $bytes) {
31 0         0 my $data;
32 0         0 $count += read($file, $data, 1024);
33 0         0 $self->SUPER::add_stream($data);
34             }
35 0         0 return ($count >= $bytes);
36             }
37            
38             sub open {
39 0     0   0 my ($self, $file) = @_;
40            
41 0 0       0 $self->close if defined $self->{_file};
42 0 0 0     0 unless (ref($file) or $file =~ /^\*[\w:]+$/) {
43             # Assume $file is a filename
44 0         0 local *F;
45 0 0       0 open(F, $file) or croak "Can't open $file: $!";
46 0         0 $file = *F;
47             }
48 0         0 binmode $file;
49 0         0 $self->{_file} = $file;
50 0         0 $self;
51             }
52            
53             sub close {
54 0     0   0 my $self = shift;
55 0         0 my $res;
56            
57 0         0 $self->close;
58 0 0       0 $res = close $self->{_file} if defined $self->{_file};
59 0         0 undef $self->{_file};
60 0         0 $res;
61             }
62            
63             sub add_stream {
64 0     0   0 croak "Can't add data to a file stream ";
65             }
66            
67             sub DESTROY {
68 0     0   0 shift->close;
69             }
70            
71             #####
72            
73             package SWF::BinStream::File::Write;
74            
75 1     1   10 use Carp;
  1         3  
  1         91  
76 1     1   8 use vars qw(@ISA);
  1         2  
  1         936  
77            
78             @ISA = ('SWF::BinStream::Write');
79            
80             sub new {
81 1     1   3 my ($class, $file, $version) = @_;
82            
83 1         13 my $self = $class->SUPER::new($version);
84 1         172 $self->SUPER::autoflush(1024, \&_writefile);
85 1 50       8 $self->open($file) if defined $file;
86 1         5 $self;
87             }
88            
89             sub _writefile {
90 2     2   16 my ($self, $data) = @_;
91 2         7 my $file = $self->{_file};
92            
93 2 50       7 croak "The file to write has not opened " unless defined $file;
94 2         22 print $file $data;
95             }
96            
97             sub open {
98 1     1   3 my ($self, $file) = @_;
99            
100 1 50       4 $self->close if defined $self->{_file};
101 1 50 33     12 unless (ref($file) or $file =~ /^\*[\w:]+$/) {
102             # Assume $file is a filename
103 1         2 local *F;
104 1 50       160 open(F, '>', $file) or croak "Can't open $file: $!";
105 1         6 $file = *F;
106             }
107 1         5 binmode $file;
108 1         4 $self->{_file} = $file;
109 1         3 $self;
110             }
111            
112             sub close {
113 1     1   3 my $self = shift;
114 1         4 my $file = $self->{_file};
115 1         3 my $res;
116            
117 1 50       5 if (defined $file) {
118 1         10 $self->SUPER::close;
119 1         89 $res = close $file;
120 1         5 undef $self->{_file};
121             }
122 1         11 $res;
123             }
124            
125 0     0     sub autoflush {
126             }
127            
128             sub DESTROY {
129 0     0     shift->close;
130             }
131            
132             1;
133             __END__