-
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
231 additions
and
302 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
module Ferrum | ||
class Page | ||
module Stream | ||
STREAM_CHUNK = 128 * 1024 | ||
|
||
def stream_to(path:, encoding:, handle:) | ||
if path.nil? | ||
stream_to_memory(encoding: encoding, handle: handle) | ||
else | ||
stream_to_file(path: path, handle: handle) | ||
end | ||
end | ||
|
||
def stream_to_file(path:, handle:) | ||
File.open(path, "wb") { |f| stream(output: f, handle: handle) } | ||
true | ||
end | ||
|
||
def stream_to_memory(encoding:, handle:) | ||
data = String.new # Mutable string has << and compatible to File | ||
stream(output: data, handle: handle) | ||
encoding == :base64 ? Base64.encode64(data) : data | ||
end | ||
|
||
def stream(output:, handle:) | ||
loop do | ||
result = command("IO.read", handle: handle, size: STREAM_CHUNK) | ||
chunk = result.fetch("data") | ||
chunk = Base64.decode64(chunk) if result["base64Encoded"] | ||
output << chunk | ||
break if result["eof"] | ||
end | ||
end | ||
end | ||
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.