1 package IO
::Compress
::Brotli
;
7 use IO
::Uncompress
::Brotli
;
9 use parent qw
/Exporter/;
11 our @EXPORT = qw
/bro/;
12 our @EXPORT_OK = @EXPORT;
14 our $VERSION = '0.001001';
18 my $state = BrotliEncoderCreateInstance
();
24 BrotliEncoderDestroyInstance
($$self)
28 my ($self, $quality) = @_;
29 BrotliEncoderSetQuality
($$self, $quality)
33 my ($self, $window) = @_;
34 BrotliEncoderSetWindow
($$self, $window)
37 my %BROTLI_ENCODER_MODE = ( generic
=> 0, text
=> 1, font
=> 2 );
39 my ($self, $mode) = @_;
41 die "Invalid encoder mode"
42 unless $BROTLI_ENCODER_MODE{$mode};
44 BrotliEncoderSetMode
($$self, $mode)
48 BROTLI_OPERATION_PROCESS
=> 0,
49 BROTLI_OPERATION_FLUSH
=> 1,
50 BROTLI_OPERATION_FINISH
=> 2
53 my ($self, $data) = @_;
54 BrotliEncoderCompressStream
($$self, $data, BROTLI_OPERATION_PROCESS
)
59 BrotliEncoderCompressStream
($$self, '', BROTLI_OPERATION_FLUSH
)
64 BrotliEncoderCompressStream
($$self, '', BROTLI_OPERATION_FINISH
)
67 # Untested, probably not working
69 my ($self, $dict) = @_;
70 BrotliEncoderSetCustomDictionary
($$self, $dict)
80 IO::Compress::Brotli - [Not yet implemented] Write Brotli buffers/streams
84 use IO::Compress::Brotli;
87 my $encoded = bro $encoded;
90 my $bro = IO::Compress::Brotli->create;
92 my $block = get_input_block();
93 my $encoded_block = $bro->compress($block);
94 handle_output_block($encoded_block);
96 # Need to finish the steam
97 handle_output_block($bro->finish());
101 IO::Compress::Brotli is a module that compressed Brotli buffers
102 and streams. Despite its name, it is not a subclass of
103 L<IO::Compress::Base> and does not implement its interface. This
104 will be rectified in a future release.
106 =head2 One-shot interface
108 If you have the whole buffer in a Perl scalar use the B<bro>
113 =item B<bro>(I<$input>)
115 Takes a whole uncompressed buffer as input and returns the compressed
122 =head2 Streaming interface
124 If you want to process the data in blocks use the object oriented
125 interface. The available methods are:
129 =item IO::Compress::Brotli->B<create>
131 Returns a IO::Compress::Brotli instance. Please note that a single
132 instance cannot be used to decompress multiple streams.
134 =item $bro->B<window>(I<$window>)
136 Sets the window parameter on the brotli encoder.
137 Defaults to BROTLI_DEFAULT_WINDOW (22).
139 =item $bro->B<quality>(I<$quality>)
141 Sets the quality paremeter on the brotli encoder.
142 Defaults to BROTLI_DEFAULT_QUALITY (11).
144 =item $bro->B<mode>(I<$mode>)
146 Sets the brotli encoder mode, which can be any of "generic",
147 "text" or "font". Defaults to "generic".
149 =item $bro->B<compress>(I<$block>)
151 Takes the a block of uncompressed data and returns a block of
152 compressed data. Dies on error.
154 =item $bro->B<flush>()
156 Flushes any pending output from the encoder.
158 =item $bro->B<finish>()
160 Tells the encoder to start the finish operation, and flushes
161 any remaining compressed output.
163 Once finish is called, the encoder cannot be used to compress
166 B<NOTE>: Calling finish is B<required>, or the output might
167 remain unflushed, and the be missing termination marks.
173 Brotli Compressed Data Format Internet-Draft:
174 L<https://www.ietf.org/id/draft-alakuijala-brotli-08.txt>
176 Brotli source code: L<https://github.com/google/brotli/>
180 Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
182 =head1 COPYRIGHT AND LICENSE
184 Copyright (C) 2015 by Marius Gavrilescu
186 This library is free software; you can redistribute it and/or modify
187 it under the same terms as Perl itself, either Perl version 5.20.2 or,
188 at your option, any later version of Perl 5 you may have available.