1package SWF::BinStream::File;
2
3use strict;
4use vars qw($VERSION);
5use SWF::BinStream;
6
7$VERSION = '0.043';
8
9package SWF::BinStream::File::Read;
10
11use Carp;
12use vars qw(@ISA);
13
14@ISA = ('SWF::BinStream::Read');
15
16sub new {
17    my ($class, $file, $version) = @_;
18
19    my $self = $class->SUPER::new('', \&_readfile, $version);
20    $self->open($file) if defined $file;
21    $self;
22}
23
24sub _readfile {
25    my ($self, $bytes) = @_;
26    my $file = $self->{_file};
27    my $count = 0;
28
29    return 0 unless defined $file;
30    while (not eof($file) and $count < $bytes) {
31	my $data;
32	$count += read($file, $data, 1024);
33	$self->SUPER::add_stream($data);
34    }
35    return ($count >= $bytes);
36}
37
38sub open {
39    my ($self, $file) = @_;
40
41    $self->close if defined $self->{_file};
42    unless (ref($file) or $file =~ /^\*[\w:]+$/) {
43	# Assume $file is a filename
44	local *F;
45	open(F, $file) or croak "Can't open $file: $!";
46	$file = *F;
47    }
48    binmode $file;
49    $self->{_file} = $file;
50    $self;
51}
52
53sub close {
54    my $self = shift;
55    my $res;
56
57    $self->close;
58    $res = close $self->{_file} if defined $self->{_file};
59    undef $self->{_file};
60    $res;
61}
62
63sub add_stream {
64    croak "Can't add data to a file stream ";
65}
66
67sub DESTROY {
68    shift->close;
69}
70
71#####
72
73package SWF::BinStream::File::Write;
74
75use Carp;
76use vars qw(@ISA);
77
78@ISA = ('SWF::BinStream::Write');
79
80sub new {
81    my ($class, $file, $version) = @_;
82
83    my $self = $class->SUPER::new($version);
84    $self->SUPER::autoflush(1024, \&_writefile);
85    $self->open($file) if defined $file;
86    $self;
87}
88
89sub _writefile {
90    my ($self, $data) = @_;
91    my $file = $self->{_file};
92
93    croak "The file to write has not opened " unless defined $file;
94    print $file $data;
95}
96
97sub open {
98    my ($self, $file) = @_;
99
100    $self->close if defined $self->{_file};
101    unless (ref($file) or $file =~ /^\*[\w:]+$/) {
102	# Assume $file is a filename
103	local *F;
104	open(F, '>', $file) or croak "Can't open $file: $!";
105	$file = *F;
106    }
107    binmode $file;
108    $self->{_file} = $file;
109    $self;
110}
111
112sub close {
113    my $self = shift;
114    my $file = $self->{_file};
115    my $res;
116
117    if (defined $file) {
118	$self->SUPER::close;
119	$res = close $file;
120	undef $self->{_file};
121    }
122    $res;
123}
124
125sub autoflush {
126}
127
128sub DESTROY {
129    shift->close;
130}
131
1321;
133__END__
134
135=head1 NAME
136
137SWF::BinStream::File - Read and write file as binary stream.
138
139=head1 SYNOPSIS
140
141  use SWF::BinStream::File;
142
143  $read_file = SWF::BinStream::File::Read->new('test.swf');
144  $byte = $read_file->get_UI8;
145  ....
146  $read_file->close;
147
148  $write_file = SWF::BinStream::Write->new('new.swf');
149  $write_file->set_UI8($byte);
150  ....
151  $write_file->close;
152
153=head1 DESCRIPTION
154
155I<SWF::BinStream::File> module provides reading and writing binary
156files as a binary stream.
157
158=head2 SWF::BinStream::File::Read
159
160is a subclass of SWF::BinStream::Read. You can get byte and bit
161data from files.
162
163=head2 METHODS
164
165You can use the methods of I<SWF::BinStream::Read> except I<add_atream>.
166
167=over 4
168
169=item SWF::BinStream::File::Read->new( [ $file, $version ] )
170
171creates a read stream connected with I<$file>.
172I<$file> is a file name or a file handle.
173I<$version> is SWF version number.  Default is 5.
174
175=item $stream->open( $file )
176
177opens another file and connect to the stream.
178Even though the previous file is automatically closed
179and the stream is cleared, I<$stream-E<gt>tell> number is
180continued.
181
182=item $stream->close
183
184closes the file and clears the stream.
185
186=back
187
188=head2 SWF::BinStream::File::Write
189
190is a subclass of SWF::BinStream::Write. You can write byte and bit
191data to a file.
192
193=head2 METHODS
194
195You can use the methods of I<SWF::BinStream::Write> except I<autoflush>.
196
197=over 4
198
199=item SWF::BinStream::File::Write->new( [ $file, $version ] )
200
201creates a stream writing to a file I<$file>.
202I<$file> is a file name or a file handle.
203I<$version> is SWF version number.  Default is 5.
204
205=item $stream->open( $file )
206
207opens another file and connect to the stream.
208The stream is flushed and the previous file is closed.
209
210=item $stream->close
211
212flushes the stream and closes the file.
213
214=back
215
216=head1 COPYRIGHT
217
218Copyright 2001 Yasuhiro Sasama (ySas), <ysas@nmt.ne.jp>
219
220This library is free software; you can redistribute it
221and/or modify it under the same terms as Perl itself.
222
223=head1 SEE ALSO
224
225L<SWF::BinStream>
226
227=cut
228