bzip2-ffi 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- checksums.yaml.gz.sig +1 -2
- data.tar.gz.sig +0 -0
- data/CHANGES.md +23 -2
- data/Gemfile +33 -2
- data/LICENSE +13 -13
- data/README.md +70 -61
- data/Rakefile +24 -0
- data/bzip2-ffi.gemspec +9 -0
- data/lib/bzip2/ffi.rb +9 -6
- data/lib/bzip2/ffi/error.rb +5 -2
- data/lib/bzip2/ffi/io.rb +59 -47
- data/lib/bzip2/ffi/libbz2.rb +7 -3
- data/lib/bzip2/ffi/reader.rb +204 -104
- data/lib/bzip2/ffi/version.rb +4 -1
- data/lib/bzip2/ffi/writer.rb +77 -62
- data/test/error_test.rb +19 -20
- data/test/fixtures/{bzipped → compressed.bz2} +0 -0
- data/test/fixtures/lorem-4096-bytes-compressed.txt.bz2 +0 -0
- data/test/fixtures/lorem-first-structure-4096-bytes.txt.bz2 +0 -0
- data/test/fixtures/two_structures.bz2 +0 -0
- data/test/io_test.rb +34 -32
- data/test/reader_test.rb +335 -111
- data/test/test_helper.rb +45 -8
- data/test/version_test.rb +4 -1
- data/test/writer_test.rb +95 -73
- metadata +31 -25
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 03b164babce536cf85cdf6fa03a4baa85651c8a9463284eeb633a0db4ffcc6fa
|
4
|
+
data.tar.gz: 42a231de039d293fe25cf5dc98c22d4650225a73fb363cee7fb3c16d69af0355
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 12a699a68f65e217b5c958697709bc65f3a64e2d627ab1ee16bf7519c43a0571321bb435a38f199639b6b1a9842a0e179022d91fd033ebf8ac907897c9e76c27
|
7
|
+
data.tar.gz: 0dc68221d4c9ee943efd3bffaea0584cefd6d5b30f5ad4d998384177a3d6fe83e29691f344e4ee110786a0558355f2a90a74aed2624ff30257e72c66ad90727f
|
checksums.yaml.gz.sig
CHANGED
@@ -1,2 +1 @@
|
|
1
|
-
|
2
|
-
��!���O�>g<��f�d���{s�L}V0>O�x�cp��8��L�91��ʯh��e���ˑ�
|
1
|
+
|��k���\s��u�G�y�_�|R�l�M?�;5�+j����.�ѩR;yfo[� 1�LU�|x��H�*I��9}_�X�ȭҨ��e��y@ʭCƐ߀��W����N>ޤj�E�!��A�Sy�#(h?b3�7�3�E�-ˇ�����SZ�N��۳�T3��`�O����:�F�r�}�A�D��M����|\����i��Q�$EZ�`� �^�q`��#]'�w�K�~��}�(��ͷ~�ک`��V�
|
data.tar.gz.sig
CHANGED
Binary file
|
data/CHANGES.md
CHANGED
@@ -1,4 +1,25 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# Changes
|
2
|
+
|
3
|
+
## Version 1.1.0 - 27-Feb-2021
|
4
|
+
|
5
|
+
* `Bzip2::FFI::Reader` will now read all consecutive bzip2 compressed structures
|
6
|
+
in the input by default instead of just the first (the same as the
|
7
|
+
bzip2/bunzip2 commands). A new `:first_only` option has been added to allow
|
8
|
+
the version 1.0.0 behaviour to be retained. #1.
|
9
|
+
* Added `#eof?` and `#eof` to `Bzip2::FFI::Reader`, indicating when
|
10
|
+
decompression has completed.
|
11
|
+
* Added `Bzip2::FFI::Reader#pos`, returning the number of decompressed bytes
|
12
|
+
that have been read.
|
13
|
+
* Added `Bzip2::FFI::Writer#pos`, returning the number of uncompressed bytes
|
14
|
+
that have been written.
|
15
|
+
* Support using Bzip2::FFI with frozen string literals enabled (and enable in
|
16
|
+
all files with `# frozen_string_literal: true`).
|
17
|
+
* Constants documented as private in version 1.0.0 are now set as private using
|
18
|
+
`Module#private_constant`.
|
19
|
+
* `require_relative` is now used when loading dependencies for a minor
|
20
|
+
performance gain.
|
21
|
+
|
22
|
+
|
23
|
+
## Version 1.0.0 - 28-Feb-2015
|
3
24
|
|
4
25
|
* First release.
|
data/Gemfile
CHANGED
@@ -3,12 +3,43 @@ source "https://rubygems.org"
|
|
3
3
|
gemspec
|
4
4
|
|
5
5
|
group :development do
|
6
|
-
gem 'rake', '
|
6
|
+
gem 'rake', ['>= 12.2.1', '< 14']
|
7
7
|
gem 'git', '~> 1.2', require: false
|
8
8
|
end
|
9
9
|
|
10
10
|
group :test do
|
11
11
|
gem 'minitest', '~> 5.0'
|
12
12
|
gem 'simplecov', '~> 0.9', require: false
|
13
|
-
|
13
|
+
|
14
|
+
# coveralls is no longer maintained, but supports Ruby < 2.3.
|
15
|
+
# coveralls_reborn is maintained, but requires Ruby >= 2.3.
|
16
|
+
gem 'coveralls', '~> 0.8', require: false if RUBY_VERSION < '2.3'
|
17
|
+
gem 'coveralls_reborn', '~> 0.13', require: false if RUBY_VERSION >= '2.3'
|
18
|
+
|
19
|
+
|
20
|
+
# json is a dependency of simplecov. Version 2.3.0 is declared as compatible
|
21
|
+
# with Ruby >= 1.9, but actually fails with a syntax error:
|
22
|
+
# https://travis-ci.org/tzinfo/tzinfo/jobs/625092293#L605
|
23
|
+
#
|
24
|
+
# 2.5.1 is declared as compatible with Ruby >= 2.0, but actually fails to
|
25
|
+
# compile with an undefined reference to `rb_funcallv` on Windows:
|
26
|
+
# https://github.com/tzinfo/tzinfo/runs/1664656059#step:3:757
|
27
|
+
#
|
28
|
+
# 2.3.0 also fails to build the native extension with Rubinius:
|
29
|
+
# https://travis-ci.org/tzinfo/tzinfo/jobs/625092305#L1310
|
30
|
+
#
|
31
|
+
# Limit to earlier compatible versions.
|
32
|
+
if RUBY_VERSION < '2.0' || RUBY_ENGINE == 'rbx'
|
33
|
+
gem 'json', '< 2.3.0'
|
34
|
+
elsif RUBY_VERSION < '2.1' && RUBY_PLATFORM =~ /mingw/
|
35
|
+
gem 'json', '< 2.5.0'
|
36
|
+
end
|
37
|
+
|
38
|
+
# ffi 1.9.15 is declared as compatible with Ruby >= 1.8.7, but doesn't compile
|
39
|
+
# on Ruby 1.9.3 on Windows.
|
40
|
+
#
|
41
|
+
# Limit to earlier compatible versions.
|
42
|
+
if RUBY_VERSION < '2.0' && RUBY_PLATFORM =~ /mingw/
|
43
|
+
gem 'ffi', '< 1.9.0'
|
44
|
+
end
|
14
45
|
end
|
data/LICENSE
CHANGED
@@ -1,19 +1,19 @@
|
|
1
|
-
Copyright (c) 2015 Philip Ross
|
1
|
+
Copyright (c) 2015-2021 Philip Ross
|
2
2
|
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
|
-
this software and associated documentation files (the "Software"), to deal in
|
5
|
-
the Software without restriction, including without limitation the rights to
|
6
|
-
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
7
|
-
of the Software, and to permit persons to whom the Software is furnished to do
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
5
|
+
the Software without restriction, including without limitation the rights to
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
7
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
8
8
|
so, subject to the following conditions:
|
9
9
|
|
10
|
-
The above copyright notice and this permission notice shall be included in all
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
11
|
copies or substantial portions of the Software.
|
12
12
|
|
13
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
19
|
THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
# Bzip2::FFI
|
1
|
+
# Bzip2::FFI
|
2
2
|
|
3
|
-
[](https://rubygems.org/gems/bzip2-ffi) [](https://github.com/philr/bzip2-ffi/actions?query=workflow%3ATests+branch%3Amaster+event%3Apush) [](https://coveralls.io/github/philr/bzip2-ffi?branch=master)
|
4
4
|
|
5
5
|
Bzip2::FFI is a Ruby wrapper for libbz2 using FFI bindings.
|
6
6
|
|
@@ -8,24 +8,19 @@ The Bzip2::FFI Reader and Writer classes support reading and writing bzip2
|
|
8
8
|
compressed data as an `IO`-like stream.
|
9
9
|
|
10
10
|
|
11
|
-
## Installation
|
11
|
+
## Installation
|
12
12
|
|
13
|
-
|
13
|
+
The Bzip2::FFI gem can be installed by running `gem install bzip2-ffi` or by
|
14
|
+
adding `gem 'bzip2-ffi'` to your `Gemfile` and running `bundle install`.
|
14
15
|
|
15
|
-
gem install bzip2-ffi
|
16
16
|
|
17
|
-
|
18
|
-
`Gemfile`:
|
17
|
+
## Compatibility
|
19
18
|
|
20
|
-
|
19
|
+
Bzip2::FFI requires a minimum of Ruby MRI 1.9.3 or JRuby 1.7 (in 1.9 mode or
|
20
|
+
later).
|
21
21
|
|
22
22
|
|
23
|
-
##
|
24
|
-
|
25
|
-
Bzip2::FFI is tested on Ruby MRI 1.9.3+, JRuby 1.7+ and Rubinius 2+.
|
26
|
-
|
27
|
-
|
28
|
-
## Runtime Dependencies ##
|
23
|
+
## Runtime Dependencies
|
29
24
|
|
30
25
|
Bzip2::FFI is a pure-Ruby library that uses
|
31
26
|
[Ruby-FFI](https://rubygems.org/gems/ffi) (Foreign Function Interface) to load
|
@@ -36,7 +31,7 @@ libbz2 is available as a package on most UNIX-based systems (for example,
|
|
36
31
|
CentOS).
|
37
32
|
|
38
33
|
|
39
|
-
### Windows
|
34
|
+
### Windows
|
40
35
|
|
41
36
|
On Windows, you will need to have `libbz2.dll` or `bz2.dll` available on the
|
42
37
|
`PATH` or in the Ruby `bin` directory.
|
@@ -46,99 +41,113 @@ Suitable builds of `libbz2.dll` are available from the
|
|
46
41
|
Download the DLL only package that matches your Ruby installation (x86 or x64)
|
47
42
|
and extract to your `ruby\bin` directory.
|
48
43
|
|
49
|
-
Builds from the bzip2-windows project depend on the Visual Studio
|
50
|
-
Library
|
51
|
-
[Visual C++ Redistributable Packages for Visual Studio 2013 installer](http://www.microsoft.com/en-gb/download/details.aspx?id=40784).
|
44
|
+
Builds from the bzip2-windows project depend on the Visual Studio C Runtime
|
45
|
+
Library. Links to the installer can be found on the bzip2-windows release page.
|
52
46
|
|
53
47
|
|
54
|
-
## Usage
|
48
|
+
## Usage
|
55
49
|
|
56
50
|
To use Bzip2::FFI, it must first be loaded with:
|
57
51
|
|
58
|
-
|
52
|
+
```ruby
|
53
|
+
require 'bzip2/ffi'
|
54
|
+
```
|
59
55
|
|
60
56
|
|
61
|
-
### Compressing
|
57
|
+
### Compressing
|
62
58
|
|
63
59
|
Data can be compressed using the `Bzip2::FFI::Writer` class. For example, the
|
64
|
-
following compresses lines read from standard input
|
60
|
+
following compresses lines read from `ARGF` (either standard input, or file
|
61
|
+
names given as command-line arguments:
|
65
62
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
63
|
+
```ruby
|
64
|
+
Bzip2::FFI::Writer.open(io_or_path) do |writer|
|
65
|
+
ARGF.each_line do |line|
|
66
|
+
writer.write(line)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
```
|
71
70
|
|
72
71
|
Alternatively, without passing a block to `open`:
|
73
72
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
73
|
+
```ruby
|
74
|
+
writer = Bzip2::FFI::Writer.open(io_or_path)
|
75
|
+
begin
|
76
|
+
ARGF.each_line do |line|
|
77
|
+
writer.write(line)
|
78
|
+
end
|
79
|
+
ensure
|
80
|
+
writer.close
|
81
|
+
end
|
82
|
+
```
|
82
83
|
|
83
84
|
An entire bzip2 structure can also be written in a single step:
|
84
85
|
|
85
|
-
|
86
|
+
```ruby
|
87
|
+
Bzip2::FFI::Writer.write(io_or_path, 'Hello, World!')
|
88
|
+
```
|
86
89
|
|
87
90
|
In each of the examples above, `io_or_path` can either be a path to a file to
|
88
|
-
write to or an `IO`-like object that has a
|
91
|
+
write to or an `IO`-like object that has a `#write` method.
|
89
92
|
|
90
93
|
|
91
|
-
### Decompressing
|
94
|
+
### Decompressing
|
92
95
|
|
93
96
|
Data can be decompressed using the `Bzip2::FFI::Reader` class. For example:
|
94
97
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
98
|
+
```ruby
|
99
|
+
Bzip2::FFI::Reader.open(io_or_path) do |reader|
|
100
|
+
while buffer = reader.read(1024) do
|
101
|
+
# process uncompressed bytes in buffer
|
102
|
+
end
|
103
|
+
end
|
104
|
+
```
|
100
105
|
|
101
106
|
Alternatively, without passing a block to `open`:
|
102
107
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
108
|
+
```ruby
|
109
|
+
reader = Bzip2::FFI::Reader.open(io_or_path)
|
110
|
+
begin
|
111
|
+
while buffer = reader.read(1024) do
|
112
|
+
# process uncompressed bytes in buffer
|
113
|
+
end
|
114
|
+
ensure
|
115
|
+
reader.close
|
116
|
+
end
|
117
|
+
```
|
111
118
|
|
112
|
-
|
119
|
+
All the available bzipped data can be read and decompressed in a single step:
|
113
120
|
|
114
|
-
|
121
|
+
```ruby
|
122
|
+
uncompressed = Bzip2::FFI::Reader.read(io_or_path)
|
123
|
+
```
|
115
124
|
|
116
125
|
In each of the examples above, `io_or_path` can either be a path to a file to
|
117
|
-
read from or an `IO`-like object that has a
|
126
|
+
read from or an `IO`-like object that has a `#read` method.
|
118
127
|
|
119
128
|
|
120
|
-
### Character Encoding
|
129
|
+
### Character Encoding
|
121
130
|
|
122
131
|
Bzip2::FFI does not perform any encoding conversion when reading or writing.
|
123
132
|
Data read using `Bzip2::FFI::Reader` is returned as `String` instances with
|
124
133
|
ASCII-8BIT (BINARY) encoding representing the raw decompressed bytes.
|
125
|
-
`Bzip2::FFI::Writer` compresses the raw bytes from the `
|
126
|
-
|
134
|
+
`Bzip2::FFI::Writer` compresses the raw bytes from the `String` instances passed
|
135
|
+
to the `#write` method (using the encoding of the `String`).
|
127
136
|
|
128
137
|
|
129
|
-
## Documentation
|
138
|
+
## Documentation
|
130
139
|
|
131
140
|
Documentation for Bzip2::FFI is available on
|
132
|
-
[RubyDoc.info](
|
141
|
+
[RubyDoc.info](https://www.rubydoc.info/gems/bzip2-ffi).
|
133
142
|
|
134
143
|
|
135
|
-
## License
|
144
|
+
## License
|
136
145
|
|
137
146
|
Bzip2::FFI is distributed under the terms of the MIT license. A copy of this
|
138
147
|
license can be found in the included LICENSE file.
|
139
148
|
|
140
149
|
|
141
|
-
## GitHub Project
|
150
|
+
## GitHub Project
|
142
151
|
|
143
152
|
Source code, release information and the issue tracker can be found on the
|
144
153
|
[Bzip2::FFI GitHub project page](https://github.com/philr/bzip2-ffi).
|
data/Rakefile
CHANGED
@@ -60,3 +60,27 @@ Rake::TestTask.new do |t|
|
|
60
60
|
t.pattern = File.join(BASE_DIR, 'test', '**', '*_test.rb')
|
61
61
|
t.warning = true
|
62
62
|
end
|
63
|
+
|
64
|
+
# Coveralls expects an sh compatible shell when running git commands with Kernel#`
|
65
|
+
# On Windows, the results end up wrapped in single quotes.
|
66
|
+
# Patch Coveralls::Configuration to remove the quotes.
|
67
|
+
if RUBY_PLATFORM =~ /mingw/
|
68
|
+
module CoverallsFixConfigurationOnWindows
|
69
|
+
def self.included(base)
|
70
|
+
base.instance_eval do
|
71
|
+
class << self
|
72
|
+
alias_method :git_without_windows_fix, :git
|
73
|
+
|
74
|
+
def git
|
75
|
+
git_without_windows_fix.tap do |hash|
|
76
|
+
hash[:head] = hash[:head].map {|k, v| [k, v =~ /\A'(.*)'\z/ ? $1 : v] }.to_h
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
require 'coveralls'
|
85
|
+
Coveralls::Configuration.send(:include, CoverallsFixConfigurationOnWindows)
|
86
|
+
end
|
data/bzip2-ffi.gemspec
CHANGED
@@ -14,6 +14,15 @@ Gem::Specification.new do |s|
|
|
14
14
|
s.email = 'phil.ross@gmail.com'
|
15
15
|
s.homepage = 'https://github.com/philr/bzip2-ffi'
|
16
16
|
s.license = 'MIT'
|
17
|
+
if s.respond_to? :metadata=
|
18
|
+
s.metadata = {
|
19
|
+
'bug_tracker_uri' => 'https://github.com/philr/bzip2-ffi/issues',
|
20
|
+
'changelog_uri' => 'https://github.com/philr/bzip2-ffi/blob/master/CHANGES.md',
|
21
|
+
'documentation_uri' => "https://rubydoc.info/gems/#{s.name}/#{s.version}",
|
22
|
+
'homepage_uri' => s.homepage,
|
23
|
+
'source_code_uri' => "https://github.com/philr/bzip2-ffi/tree/v#{s.version}"
|
24
|
+
}
|
25
|
+
end
|
17
26
|
s.files = %w(CHANGES.md Gemfile LICENSE README.md Rakefile bzip2-ffi.gemspec .yardopts) +
|
18
27
|
Dir['lib/**/*.rb'] +
|
19
28
|
Dir['test/**/*.rb'] +
|
data/lib/bzip2/ffi.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
1
4
|
module Bzip2
|
2
5
|
# Bzip2::FFI is a wrapper for libbz2 using FFI bindings. Bzip2 compressed data
|
3
6
|
# can be read and written as a stream using the Reader and Writer classes.
|
@@ -5,10 +8,10 @@ module Bzip2
|
|
5
8
|
end
|
6
9
|
end
|
7
10
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
require_relative 'ffi/libbz2'
|
12
|
+
require_relative 'ffi/error'
|
13
|
+
require_relative 'ffi/io'
|
14
|
+
require_relative 'ffi/reader'
|
15
|
+
require_relative 'ffi/writer'
|
16
|
+
require_relative 'ffi/version'
|
14
17
|
|
data/lib/bzip2/ffi/error.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
1
4
|
module Bzip2
|
2
5
|
module FFI
|
3
|
-
# The
|
4
|
-
#
|
6
|
+
# The {Error} module contains exception classes that are raised if an error
|
7
|
+
# occurs whilst compressing or decompressing data.
|
5
8
|
module Error
|
6
9
|
# Base class for Bzip2::FFI exceptions.
|
7
10
|
class Bzip2Error < IOError
|
data/lib/bzip2/ffi/io.rb
CHANGED
@@ -1,19 +1,22 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
1
4
|
module Bzip2
|
2
5
|
module FFI
|
3
|
-
#
|
6
|
+
# {IO} is a base class providing common functionality for the {Reader} and
|
4
7
|
# {Writer} subclasses.
|
5
8
|
#
|
6
|
-
#
|
7
|
-
#
|
9
|
+
# Holds a reference to an underlying IO-like stream representing the bzip2
|
10
|
+
# compressed data to be read from or written to.
|
8
11
|
class IO
|
9
12
|
class << self
|
10
13
|
protected :new
|
11
14
|
|
12
15
|
protected
|
13
16
|
|
14
|
-
# If no block is provided, returns a new
|
15
|
-
# a new
|
16
|
-
# executed, the
|
17
|
+
# If no block is provided, returns a new {IO}. If a block is provided,
|
18
|
+
# a new {IO} is created and yielded to the block. After the block has
|
19
|
+
# executed, the {IO} is closed and the result of the block is returned.
|
17
20
|
#
|
18
21
|
# If `io_or_proc` is a `Proc`, it is called to obtain an IO-like
|
19
22
|
# instance to pass to `new`. Otherwise `io_or_proc` is passed directly
|
@@ -22,6 +25,11 @@ module Bzip2
|
|
22
25
|
# @param io_or_proc [Object] An IO-like object or a `Proc` that returns
|
23
26
|
# an IO-like object when called.
|
24
27
|
# @param options [Hash] Options to pass to `new`.
|
28
|
+
# @yield [io] If a block is given, it is yielded to.
|
29
|
+
# @yieldparam io [IO] The new {IO} instance.
|
30
|
+
# @yieldresult [Object] A result to be returned as the result of {open}.
|
31
|
+
# @return [Object] The result of the block if a block is given,
|
32
|
+
# otherwise the new {IO} instance.
|
25
33
|
def open(io_or_proc, options = {})
|
26
34
|
if io_or_proc.kind_of?(Proc)
|
27
35
|
io = io_or_proc.call
|
@@ -36,7 +44,7 @@ module Bzip2
|
|
36
44
|
end
|
37
45
|
|
38
46
|
if block_given?
|
39
|
-
begin
|
47
|
+
begin
|
40
48
|
yield bz_io
|
41
49
|
ensure
|
42
50
|
bz_io.close unless bz_io.closed?
|
@@ -46,7 +54,7 @@ module Bzip2
|
|
46
54
|
end
|
47
55
|
end
|
48
56
|
|
49
|
-
# Opens and returns a bzip
|
57
|
+
# Opens and returns a bzip file using the specified mode. The system
|
50
58
|
# is advised that the file will be accessed once sequentially.
|
51
59
|
#
|
52
60
|
# @param path [String] The path to open.
|
@@ -67,9 +75,9 @@ module Bzip2
|
|
67
75
|
|
68
76
|
private
|
69
77
|
|
70
|
-
# Advises the system that an
|
78
|
+
# Advises the system that an `::IO` will be accessed once sequentially.
|
71
79
|
#
|
72
|
-
# @param io [IO] An
|
80
|
+
# @param io [::IO] An `::IO` instance to advise.
|
73
81
|
def after_open_file(io)
|
74
82
|
# JRuby 1.7.18 doesn't have a File#advise method (in any mode).
|
75
83
|
if io.respond_to?(:advise)
|
@@ -79,67 +87,69 @@ module Bzip2
|
|
79
87
|
end
|
80
88
|
end
|
81
89
|
|
82
|
-
# Returns `true` if the underlying compressed
|
83
|
-
# when {#close} is called, otherwise `false`.
|
90
|
+
# Returns `true` if the underlying compressed IO-like instance will be
|
91
|
+
# closed when {#close} is called, otherwise `false`.
|
84
92
|
#
|
85
|
-
# @return [Boolean] `true` if the underlying compressed IO instance
|
86
|
-
# be closed when {#close} is closed, otherwise
|
87
|
-
#
|
93
|
+
# @return [Boolean] `true` if the underlying compressed IO-like instance
|
94
|
+
# will be closed when {#close} is closed, otherwise
|
95
|
+
# `false`.
|
96
|
+
# @raise [IOError] If the {IO} instance has been closed.
|
88
97
|
def autoclose?
|
89
98
|
check_closed
|
90
99
|
@autoclose
|
91
100
|
end
|
92
101
|
|
93
|
-
# Sets whether the underlying compressed
|
102
|
+
# Sets whether the underlying compressed IO-like instance should be closed
|
94
103
|
# when {#close} is called (`true`) or left open (`false`).
|
95
104
|
#
|
96
105
|
# @param autoclose [Boolean] `true` if the underlying compressed `IO`
|
97
106
|
# instance should be closed when {#close} is
|
98
107
|
# called, or `false` if it should be left open.
|
99
|
-
# @raise [IOError] If the instance has been closed.
|
108
|
+
# @raise [IOError] If the {IO} instance has been closed.
|
100
109
|
def autoclose=(autoclose)
|
101
110
|
check_closed
|
102
111
|
@autoclose = !!autoclose
|
103
112
|
end
|
104
113
|
|
105
|
-
# Returns `true` to indicate that the
|
106
|
-
# (as is always the case).
|
114
|
+
# Returns `true` to indicate that the {IO} instance is operating in binary
|
115
|
+
# mode (as is always the case).
|
107
116
|
#
|
108
117
|
# @return [Boolean] `true`.
|
109
|
-
# @raise [IOError] If the
|
118
|
+
# @raise [IOError] If the {IO} instance has been closed.
|
110
119
|
def binmode?
|
111
120
|
check_closed
|
112
121
|
true
|
113
122
|
end
|
114
123
|
|
115
|
-
# Puts the
|
124
|
+
# Puts the {IO} instance into binary mode.
|
116
125
|
#
|
117
|
-
# Note that
|
118
|
-
#
|
126
|
+
# Note that {IO} and subclasses always operate in binary mode, so calling
|
127
|
+
# `binmode` has no effect.
|
119
128
|
#
|
120
129
|
# @return [IO] `self`.
|
121
|
-
# @raise [IOError] If the
|
130
|
+
# @raise [IOError] If the {IO} instance has been closed.
|
122
131
|
def binmode
|
123
132
|
check_closed
|
124
133
|
self
|
125
134
|
end
|
126
135
|
|
127
|
-
# Closes the
|
136
|
+
# Closes the {IO} instance.
|
128
137
|
#
|
129
|
-
# If {#autoclose?} is true and the underlying compressed
|
130
|
-
# `close`, it will also be closed.
|
138
|
+
# If {#autoclose?} is true and the underlying compressed IO-like instance
|
139
|
+
# responds to `close`, it will also be closed.
|
131
140
|
#
|
132
141
|
# @return [NilClass] `nil`.
|
133
|
-
# @raise [IOError] If the
|
142
|
+
# @raise [IOError] If the {IO} instance has already been closed.
|
134
143
|
def close
|
135
144
|
check_closed
|
136
145
|
@io.close if autoclose? && @io.respond_to?(:close)
|
137
146
|
@stream = nil
|
138
147
|
end
|
139
148
|
|
140
|
-
# Indicates whether the
|
149
|
+
# Indicates whether the {IO} instance has been closed by calling {#close}.
|
141
150
|
#
|
142
|
-
# @return [Boolean] `true` if the
|
151
|
+
# @return [Boolean] `true` if the {IO} instance has been closed, otherwise
|
152
|
+
# `false`.
|
143
153
|
def closed?
|
144
154
|
!@stream
|
145
155
|
end
|
@@ -151,7 +161,7 @@ module Bzip2
|
|
151
161
|
# returns `Encoding::ASCII_8BIT` (also known as `Encoding::BINARY`).
|
152
162
|
#
|
153
163
|
# @return [Encoding] `Encoding::ASCII_8BIT`.
|
154
|
-
# @raise [IOError] If the
|
164
|
+
# @raise [IOError] If the {IO} instance has been closed.
|
155
165
|
def external_encoding
|
156
166
|
check_closed
|
157
167
|
Encoding::ASCII_8BIT
|
@@ -168,32 +178,33 @@ module Bzip2
|
|
168
178
|
check_closed
|
169
179
|
Encoding::ASCII_8BIT
|
170
180
|
end
|
171
|
-
|
181
|
+
|
172
182
|
protected
|
173
183
|
|
174
|
-
# The underlying compressed
|
184
|
+
# The underlying compressed IO-like instance.
|
175
185
|
attr_reader :io
|
176
186
|
|
177
|
-
# Initializes a new {
|
178
|
-
#
|
187
|
+
# Initializes a new {IO} instance with an underlying compressed IO-like
|
188
|
+
# instance and `options` `Hash`.
|
179
189
|
#
|
180
190
|
# `binmode` is called on `io` if `io` responds to `binmode`.
|
181
191
|
#
|
182
|
-
# A single `:autoclose` option is supported. Set `:autoclose` to true
|
183
|
-
#
|
192
|
+
# A single `:autoclose` option is supported. Set `:autoclose` to true to
|
193
|
+
# close the underlying compressed IO-like instance when {#close} is
|
184
194
|
# called.
|
185
195
|
#
|
186
|
-
# @param io [
|
196
|
+
# @param io [Object] An IO-like object that represents the compressed
|
197
|
+
# data.
|
187
198
|
# @param options [Hash] Optional parameters (:autoclose).
|
188
199
|
# @raise [ArgumentError] If `io` is nil.
|
189
200
|
def initialize(io, options = {})
|
190
201
|
raise ArgumentError, 'io is required' unless io
|
191
|
-
|
202
|
+
|
192
203
|
@io = io
|
193
|
-
@io.binmode if @io.respond_to?(:binmode)
|
204
|
+
@io.binmode if @io.respond_to?(:binmode)
|
194
205
|
|
195
206
|
@autoclose = !!options[:autoclose]
|
196
|
-
|
207
|
+
|
197
208
|
@stream = Libbz2::BzStream.new
|
198
209
|
end
|
199
210
|
|
@@ -202,22 +213,23 @@ module Bzip2
|
|
202
213
|
#
|
203
214
|
# @return [Libbz2::BzStream] The {Libbz2::BzStream} instance being used
|
204
215
|
# to interface with libbz2.
|
205
|
-
# @raise [IOError] If the
|
216
|
+
# @raise [IOError] If the {IO} instance has been closed.
|
206
217
|
def stream
|
207
218
|
check_closed
|
208
219
|
@stream
|
209
|
-
end
|
220
|
+
end
|
210
221
|
|
211
|
-
# Raises an `IOError` if {#close} has been called to close the {IO}
|
222
|
+
# Raises an `IOError` if {#close} has been called to close the {IO}
|
223
|
+
# instance.
|
212
224
|
#
|
213
|
-
# @raise [IOError] If the
|
225
|
+
# @raise [IOError] If the {IO} instance has been closed.
|
214
226
|
def check_closed
|
215
227
|
raise IOError, 'closed stream' if closed?
|
216
228
|
end
|
217
229
|
|
218
230
|
# Checks a return code from a libbz2 function. If it is greater than or
|
219
231
|
# equal to 0 (success), the return code is returned. If it is less than
|
220
|
-
# zero (an error), the appropriate {
|
232
|
+
# zero (an error), the appropriate {Error::Bzip2Error} sub-class is
|
221
233
|
# raised.
|
222
234
|
#
|
223
235
|
# @param res [Integer] The result of a call to a libbz2 function.
|
@@ -244,7 +256,7 @@ module Bzip2
|
|
244
256
|
end
|
245
257
|
|
246
258
|
raise error_class.new
|
247
|
-
end
|
259
|
+
end
|
248
260
|
end
|
249
261
|
end
|
250
262
|
end
|