bzip2-ruby-rb20 0.2.7

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.
@@ -0,0 +1,22 @@
1
+ #ifndef _RB_BZIP2_WRITER_H_
2
+ #define _RB_BZIP2_WRITER_H_
3
+
4
+ #include <ruby.h>
5
+ #include "common.h"
6
+
7
+ int bz_writer_internal_flush(struct bz_file *bzf);
8
+
9
+ /* Instance methods */
10
+ VALUE bz_writer_close(VALUE obj);
11
+ VALUE bz_writer_close_bang(VALUE obj);
12
+ VALUE bz_writer_closed(VALUE obj);
13
+ VALUE bz_writer_flush(VALUE obj);
14
+ VALUE bz_writer_init(int argc, VALUE *argv, VALUE obj);
15
+ VALUE bz_writer_write(VALUE obj, VALUE a);
16
+ VALUE bz_writer_putc(VALUE obj, VALUE a);
17
+
18
+ /* Class methods */
19
+ VALUE bz_writer_s_alloc(VALUE obj);
20
+ VALUE bz_writer_s_open(int argc, VALUE *argv, VALUE obj);
21
+
22
+ #endif
@@ -0,0 +1,13 @@
1
+ require 'bzip2/bzip2'
2
+ require 'bzip2/version'
3
+
4
+ # This is the base module for the +bzip2-ruby+ gem. Beneath it are the classes
5
+ # for writing and reading data from bzip2 compressed and uncompressed streams.
6
+ #
7
+ # For example usage, see the Bzip2::Reader or Bzip2::Writer or the
8
+ # {README}[link:docs/file/README.rdoc]
9
+ #
10
+ # @see Bzip2::Writer
11
+ # @see Bzip2::Reader
12
+ module Bzip2
13
+ end
@@ -0,0 +1,13 @@
1
+ # This file is mostly here for documentation purposes, do not require this
2
+
3
+ module Bzip2
4
+ class << self
5
+ alias :bzip2 :compress
6
+ alias :bunzip2 :uncompress
7
+ alias :decompress :uncompress
8
+ end
9
+
10
+ # @private
11
+ class InternalStr
12
+ end
13
+ end
@@ -0,0 +1,27 @@
1
+ # This file is mostly here for documentation purposes, do not require this
2
+
3
+ #
4
+ module Bzip2
5
+ # Bzip2::Reader is meant to read streams of bz2 compressed bytes. It behaves
6
+ # like an IO object with many similar methods. It also includes the Enumerable
7
+ # module and each element is a 'line' in the stream.
8
+ #
9
+ # It can both decompress files:
10
+ #
11
+ # reader = Bzip2::Reader.open('file')
12
+ # puts reader.read
13
+ #
14
+ # reader = Bzip2::Reader.new File.open('file')
15
+ # put reader.gets
16
+ #
17
+ # And it may just decompress raw strings
18
+ #
19
+ # reader = Bzip2::Reader.new compressed_string
20
+ # reader = Bzip2::Reader.new Bzip2.compress('compress-me')
21
+ class Reader
22
+ alias :each_line :each
23
+ alias :closed :closed?
24
+ alias :eoz :eoz?
25
+ alias :eof :eof?
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module Bzip2
2
+ VERSION = "0.2.7"
3
+ end
@@ -0,0 +1,64 @@
1
+ # This file is mostly here for documentation purposes, do not require this
2
+
3
+ #
4
+ module Bzip2
5
+ # A Bzip2::Writer represents a stream which compresses data written to it.
6
+ # It can be constructed with another IO object (a File) which data can be
7
+ # written to. Otherwise, data is all stored internally as a string and can
8
+ # be retrieved via the Bzip2::Writer#flush method
9
+ #
10
+ # It can both write to files:
11
+ #
12
+ # writer = Bzip2::Writer.open('file')
13
+ # writer << data
14
+ # writer.close
15
+ #
16
+ # Bzip2::Writer.open('file'){ |f| f << data }
17
+ #
18
+ # writer = Bzip2::Writer.new File.open('file')
19
+ #
20
+ # And output data as a string
21
+ #
22
+ # writer = Bzip2::Writer.new
23
+ # writer << data
24
+ # writer.flush # => data compressed via bz2
25
+ #
26
+ # @see Bzip2::Writer#initialize The initialize method for examples
27
+ class Writer
28
+
29
+ alias :finish :flush
30
+ alias :closed :closed?
31
+
32
+ # Append some data to this buffer, returning the buffer so this method can
33
+ # be chained
34
+ #
35
+ # writer = Bzip2::Writer.new
36
+ # writer << 'asdf' << 1 << obj << 'a'
37
+ # writer.flush
38
+ #
39
+ # @param [#to_s] data anything responding to #to_s
40
+ # @see IO#<<
41
+ def << data
42
+ end
43
+
44
+ # Adds a number of strings to this buffer. A newline is also inserted into
45
+ # the buffer after each object
46
+ # @see IO#puts
47
+ def puts *objs
48
+ end
49
+
50
+ # Similar to Bzip2::Writer#puts except a newline is not appended after each
51
+ # object appended to this buffer
52
+ #
53
+ # @see IO#print
54
+ def print *objs
55
+ end
56
+
57
+ # Prints data to this buffer with the specified format.
58
+ #
59
+ # @see Kernel#sprintf
60
+ def printf format, *ojbs
61
+ end
62
+
63
+ end
64
+ end
@@ -0,0 +1,261 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe Bzip2::Writer do
5
+ before(:each) do
6
+ @file = "_10lines_"
7
+ @data = [
8
+ "00: This is a line\n",
9
+ "01: This is a line\n",
10
+ "02: This is a line\n",
11
+ "03: This is a line\n",
12
+ "04: This is a line\n",
13
+ "05: This is a line\n",
14
+ "06: This is a line\n",
15
+ "07: This is a line\n",
16
+ "08: This is a line\n",
17
+ "09: This is a line\n"
18
+ ]
19
+
20
+ open("|bzip2 > #{@file}", "w") do |f|
21
+ @data.each { |l| f.puts l }
22
+ end
23
+ end
24
+
25
+ after(:each) do
26
+ File.delete(@file) if File.exists?(@file)
27
+ end
28
+
29
+ it "iterate over each line of the file via the foreach method" do
30
+ lines = []
31
+ Bzip2::Reader.foreach(@file){ |line| lines << line }
32
+ lines.should == @data
33
+
34
+ lines.clear
35
+ Bzip2::Reader.foreach(@file, nil) do |file|
36
+ file.split(/\n/).each{ |line| lines << line + "\n" }
37
+ end
38
+ lines.should == @data
39
+
40
+ count = 0
41
+ Bzip2::Reader.foreach(@file, ' ') do |thing|
42
+ count += 1
43
+ end
44
+ count.should == 41
45
+ end
46
+
47
+ it "returns an array of the lines read via #readlines" do
48
+ lines = Bzip2::Reader.readlines(@file)
49
+ lines.should == @data
50
+
51
+ lines = Bzip2::Reader.readlines(@file, nil)
52
+ lines.should == [@data.join]
53
+ end
54
+
55
+ it "track when the stream has been closed" do
56
+ f = Bzip2::Reader.open(@file)
57
+ f.should_not be_closed
58
+ f.close
59
+ f.should be_closed
60
+ end
61
+
62
+ shared_examples_for 'a line iterator' do |method|
63
+ it "iterates over the lines when using #each" do
64
+ Bzip2::Reader.open(@file) do |file|
65
+ list = []
66
+ file.send(method){ |l| list << l }
67
+ list.should == @data
68
+ end
69
+
70
+ Bzip2::Reader.open(@file) do |file|
71
+ file.send(method, nil) do |contents|
72
+ contents.should == @data.join
73
+ end
74
+ end
75
+
76
+ count = 0
77
+ Bzip2::Reader.open(@file) do |file|
78
+ file.send(method, ' ') do |thing|
79
+ count += 1
80
+ end
81
+ end
82
+ 41.should == count
83
+ end
84
+ end
85
+
86
+ it_should_behave_like 'a line iterator', :each
87
+ it_should_behave_like 'a line iterator', :each_line
88
+
89
+ it "iterates over the decompressed bytes via #each_byte" do
90
+ bytes = @data.join.bytes.to_a
91
+
92
+ Bzip2::Reader.open(@file) do |file|
93
+ file.each_byte do |b|
94
+ b.should == bytes.shift
95
+ end
96
+ end
97
+
98
+ bytes.size.should == 0
99
+ end
100
+
101
+ it "keeps track of when eof has been reached" do
102
+ Bzip2::Reader.open(@file) do |file|
103
+ @data.size.times do
104
+ file.should_not be_eof
105
+ file.gets
106
+ end
107
+
108
+ file.should be_eof
109
+ end
110
+ end
111
+
112
+ it "gets only one byte at a time via getc and doesn't raise an exception" do
113
+ bytes = @data.join.bytes.to_a
114
+
115
+ Bzip2::Reader.open(@file) do |file|
116
+ while ch = file.getc
117
+ ch.should == bytes.shift
118
+ end
119
+
120
+ file.getc.should be_nil
121
+ end
122
+
123
+ bytes.size.should == 0
124
+ end
125
+
126
+ it "reads an entire line via gets" do
127
+ Bzip2::Reader.open(@file) do |file|
128
+ lines = []
129
+ while line = file.gets
130
+ lines << line
131
+ end
132
+ lines.should == @data
133
+
134
+ file.gets.should be_nil
135
+ end
136
+
137
+ Bzip2::Reader.open(@file) do |file|
138
+ lines = []
139
+ while line = file.gets("line\n")
140
+ lines << line
141
+ end
142
+ lines.should == @data
143
+
144
+ file.gets.should be_nil
145
+ end
146
+
147
+ lines = ''
148
+ Bzip2::Reader.open(@file) do |file|
149
+ while contents = file.gets(nil)
150
+ lines << contents
151
+ end
152
+ end
153
+ lines.should == @data.join
154
+
155
+ count = 0
156
+ Bzip2::Reader.open(@file) do |file|
157
+ count += 1 while file.gets(' ')
158
+ end
159
+ 41.should == count
160
+ end
161
+
162
+ it "reads the entire file or a specified length when using #read" do
163
+ Bzip2::Reader.open(@file) do |file|
164
+ file.read.should == @data.join
165
+ end
166
+
167
+ Bzip2::Reader.open(@file) do |file|
168
+ file.read(12).should == "00: This is "
169
+ file.read(12).should == "a line\n01: T"
170
+ end
171
+ end
172
+
173
+ it "reads one character and returns the byte value of the character read" do
174
+ count = 0
175
+ data = @data.join
176
+ Bzip2::Reader.open(@file) do |file|
177
+ @data.join.bytes do |byte|
178
+ file.readchar.should == byte
179
+ end
180
+
181
+ lambda { file.readchar }.should raise_error(Bzip2::EOZError)
182
+ end
183
+ end
184
+
185
+ it "reads one line at a time and raises and exception when no more" do
186
+ count = 0
187
+ Bzip2::Reader.open(@file) do |file|
188
+ lines = []
189
+ @data.size.times do |count|
190
+ lines << file.readline
191
+ end
192
+
193
+ lines.should == @data
194
+ lambda { file.readline }.should raise_error(Bzip2::EOZError)
195
+ end
196
+
197
+ Bzip2::Reader.open(@file) do |file|
198
+ file.readline(nil).should == @data.join
199
+
200
+ lambda { file.readline }.should raise_error(Bzip2::EOZError)
201
+ end
202
+
203
+ Bzip2::Reader.open(@file) do |file|
204
+ 41.times { |count| file.readline(' ') }
205
+ lambda { file.readline }.should raise_error(Bzip2::EOZError)
206
+ end
207
+ end
208
+
209
+ it "returns an array of lines in the file" do
210
+ Bzip2::Reader.open(@file) do |file|
211
+ file.readlines.should == @data
212
+ end
213
+
214
+ Bzip2::Reader.open(@file) do |file|
215
+ file.readlines(nil).should == [@data.join]
216
+ end
217
+ end
218
+
219
+ it "rewinds the stream when #ungetc is called and returns that byte next" do
220
+ Bzip2::Reader.open(@file) do |file|
221
+ '0'.bytes.first.should == file.getc
222
+ '0'.bytes.first.should == file.getc
223
+ ':'.bytes.first.should == file.getc
224
+ ' '.bytes.first.should == file.getc
225
+
226
+ file.ungetc(':'.bytes.first).should be_nil
227
+ ':'.bytes.first.should == file.getc
228
+
229
+ file.read
230
+
231
+ file.ungetc('A'.bytes.first).should be_nil
232
+ 'A'.bytes.first.should == file.getc
233
+ end
234
+ end
235
+
236
+ it "rewinds the stream when #ungets is called" do
237
+ Bzip2::Reader.open(@file) do |file|
238
+ @data[0].should == file.gets
239
+ 1.should == file.lineno
240
+ file.ungets(@data[0]).should be_nil
241
+ @data[0].should == file.gets
242
+ end
243
+ end
244
+
245
+ it "reads entire lines via readline and throws an exception when there is" do
246
+ string = File.read(@file)
247
+ file = Bzip2::Reader.new(string)
248
+ lines = []
249
+ @data.size.times do |count|
250
+ lines << file.readline
251
+ end
252
+ lines.should == @data
253
+ lambda { file.readline }.should raise_error(Bzip2::EOZError)
254
+ file.close
255
+
256
+ file = Bzip2::Reader.new(string)
257
+ file.readline(nil).should == @data.join
258
+ lambda { file.readline }.should raise_error(Bzip2::EOZError)
259
+ file.close
260
+ end
261
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: UTF-8
2
+ require 'bundler/setup'
3
+
4
+ require 'rspec/core'
5
+ require 'bzip2'
6
+
7
+
8
+ RSpec.configure do |config|
9
+ config.color_enabled = true
10
+ end
11
+
12
+ # back-port 1.9 method so the tests will pass in 1.8 as well
13
+ if RUBY_VERSION.include?("1.8")
14
+ class String
15
+ def getbyte(idx)
16
+ self[idx]
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,134 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe Bzip2::Writer do
5
+ let(:file){ File.expand_path('../_10lines_', __FILE__) }
6
+
7
+ class Dummy
8
+ def to_s
9
+ "dummy"
10
+ end
11
+ end
12
+
13
+ after(:each) do
14
+ File.delete(file) if File.exists?(file)
15
+ end
16
+
17
+ it "performs like IO#<< when using the #<< method" do
18
+ Bzip2::Writer.open(file, "w") do |file|
19
+ file << 1 << "\n" << Dummy.new << "\n" << "cat\n"
20
+ end
21
+ expected = [ "1\n", "dummy\n", "cat\n"]
22
+ actual = []
23
+ Bzip2::Reader.foreach(file){ |line| actual.push line }
24
+ actual.should == expected
25
+ end
26
+
27
+ it "doesn't immediately flush the data when written to" do
28
+ io = File.new(file, "w")
29
+ bz2 = Bzip2::Writer.new(io)
30
+ bz2 << 1 << "\n" << Dummy.new << "\n" << "cat\n"
31
+ bz = Bzip2::Reader.new(file)
32
+ lambda { bz.gets }.should raise_error(Bzip2::Error)
33
+ bz = Bzip2::Reader.open(file)
34
+ lambda { bz.gets }.should raise_error(Bzip2::Error)
35
+ io.close
36
+ lambda { Bzip2::Reader.new(io) }.should raise_error(IOError)
37
+ end
38
+
39
+ it "behaves the same as IO#print when using #print" do
40
+ Bzip2::Writer.open(file) do |file|
41
+ file.print "foo\n" * 4096, "\n" * 4096,
42
+ "bar" * 4096, "\n" * 4096, "zot\n" * 1024
43
+ end
44
+
45
+ Bzip2::Reader.open(file) do |file|
46
+ file.gets('').should == "foo\n" * 4096 + "\n"
47
+ file.gets('').should == "bar" * 4096 + "\n\n"
48
+ file.gets('').should == "zot\n" * 1024
49
+ end
50
+ end
51
+
52
+ it "respects specific global variables like IO#print does via #print" do
53
+ Bzip2::Writer.open(file) do |file|
54
+ file.print "hello"
55
+ file.print 1,2
56
+ $_ = "wombat\n"
57
+ file.print
58
+ $\ = ":"
59
+ $, = ","
60
+ file.print 3, 4
61
+ file.print 5, 6
62
+ $\ = nil
63
+ file.print "\n"
64
+ $, = nil
65
+ end
66
+
67
+ Bzip2::Reader.open(file) do |file|
68
+ file.gets(nil).should == "hello12wombat\n3,4:5,6:\n"
69
+ end
70
+ end
71
+
72
+ it "only writes one byte via the #putc method" do
73
+ Bzip2::Writer.open(file, "wb") do |file|
74
+ file.putc "A"
75
+ 0.upto(255) { |ch| file.putc ch }
76
+ end
77
+
78
+ Bzip2::Reader.open(file, "rb") do |file|
79
+ file.getc.should == 'A'.bytes.first
80
+ 0.upto(255) { |ch| file.getc.should == ch }
81
+ end
82
+ end
83
+
84
+ it "behaves the same as IO#puts when using #puts" do
85
+ Bzip2::Writer.open(file, "w") do |file|
86
+ file.puts "line 1", "line 2"
87
+ file.puts [ Dummy.new, 4 ]
88
+ end
89
+
90
+ Bzip2::Reader.open(file) do |file|
91
+ file.gets.should == "line 1\n"
92
+ file.gets.should == "line 2\n"
93
+ file.gets.should == "dummy\n"
94
+ file.gets.should == "4\n"
95
+ end
96
+ end
97
+
98
+ it "writes data successfully to a file and returns the length of the data" do
99
+ Bzip2::Writer.open(file, "w") do |file|
100
+ file.write('*' * 10).should == 10
101
+ file.write('!' * 5).should == 5
102
+ file.write('').should == 0
103
+ file.write(1).should == 1
104
+ file.write(2.30000).should == 3
105
+ file.write("\n").should == 1
106
+ end
107
+
108
+ Bzip2::Reader.open(file) do |file|
109
+ file.gets.should == "**********!!!!!12.3\n"
110
+ end
111
+ end
112
+
113
+ it "returns the compressed data when no constructor argument is specified" do
114
+ file = Bzip2::Writer.new
115
+ file << ('*' * 10) << ('!' * 5) << '' << 1 << 2.3000 << "\n"
116
+ Bzip2::bunzip2(file.flush).should == "**********!!!!!12.3\n"
117
+ end
118
+
119
+ it "compresses data via the #bzip2 shortcut" do
120
+ data = ["**********!!!!!12.3\n"]
121
+ data << "foo\n"*4096 + "\n"*4096 + "bar"*4096 + "\n"*4096 + "zot\n"*1024
122
+
123
+ data.each do |test|
124
+ Bzip2::bunzip2(Bzip2::bzip2(test)).should == test
125
+ end
126
+ end
127
+
128
+ it "correctly reports when a writer is closed" do
129
+ writer = Bzip2::Writer.open(file, 'w')
130
+ writer.should_not be_closed
131
+ writer.close
132
+ writer.should be_closed
133
+ end
134
+ end