-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6916 from asterite/opt/gzip-zlib-flate
Let Zlib::Reader, Gzip::Reader and Flate::Reader include IO::Buffered for optimized performance
- Loading branch information
Showing
6 changed files
with
140 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,60 @@ | ||
require "spec" | ||
require "gzip" | ||
|
||
private SAMPLE_TIME = Time.utc(2016, 1, 2) | ||
private SAMPLE_OS = 4_u8 | ||
private SAMPLE_EXTRA = Bytes[1, 2, 3] | ||
private SAMPLE_NAME = "foo.txt" | ||
private SAMPLE_COMMENT = "some comment" | ||
private SAMPLE_CONTENTS = "hello world\nfoo bar" | ||
|
||
private def new_sample_io | ||
io = IO::Memory.new | ||
|
||
Gzip::Writer.open(io) do |gzip| | ||
header = gzip.header | ||
header.modification_time = SAMPLE_TIME | ||
header.os = SAMPLE_OS | ||
header.extra = SAMPLE_EXTRA | ||
header.name = SAMPLE_NAME | ||
header.comment = SAMPLE_COMMENT | ||
|
||
io.bytesize.should eq(0) | ||
gzip.flush | ||
io.bytesize.should_not eq(0) | ||
|
||
gzip.print SAMPLE_CONTENTS | ||
end | ||
|
||
io.rewind | ||
end | ||
|
||
describe Gzip do | ||
it "writes and reads to memory" do | ||
io = IO::Memory.new | ||
|
||
time = Time.utc(2016, 1, 2) | ||
os = 4_u8 | ||
extra = Bytes[1, 2, 3] | ||
name = "foo.txt" | ||
comment = "some comment" | ||
contents = "hello world" | ||
|
||
Gzip::Writer.open(io) do |gzip| | ||
header = gzip.header | ||
header.modification_time = time | ||
header.os = os | ||
header.extra = extra | ||
header.name = name | ||
header.comment = comment | ||
|
||
io.bytesize.should eq(0) | ||
gzip.flush | ||
io.bytesize.should_not eq(0) | ||
|
||
gzip.print contents | ||
end | ||
|
||
io.rewind | ||
io = new_sample_io | ||
|
||
Gzip::Reader.open(io) do |gzip| | ||
header = gzip.header.not_nil! | ||
header.modification_time.should eq(time) | ||
header.os.should eq(os) | ||
header.extra.should eq(extra) | ||
header.name.should eq(name) | ||
header.comment.should eq(comment) | ||
header.modification_time.should eq(SAMPLE_TIME) | ||
header.os.should eq(SAMPLE_OS) | ||
header.extra.should eq(SAMPLE_EXTRA) | ||
header.name.should eq(SAMPLE_NAME) | ||
header.comment.should eq(SAMPLE_COMMENT) | ||
|
||
# Reading zero bytes is OK | ||
gzip.read(Bytes.empty).should eq(0) | ||
|
||
gzip.gets_to_end.should eq(contents) | ||
gzip.gets_to_end.should eq(SAMPLE_CONTENTS) | ||
end | ||
end | ||
|
||
it "rewinds" do | ||
io = new_sample_io | ||
|
||
gzip = Gzip::Reader.new(io) | ||
gzip.gets.should eq(SAMPLE_CONTENTS.lines.first) | ||
|
||
gzip.rewind | ||
gzip.gets_to_end.should eq(SAMPLE_CONTENTS) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters