Class: Bio::FlatFile::BufferedInputStream
- Defined in:
- lib/bio/io/flatfile/buffer.rb
Overview
Wrapper for a IO (or IO-like) object. It can input with a buffer.
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Pathname, filename or URI to open the object.
Class Method Summary collapse
-
.for_io(io) ⇒ Object
Creates a new input stream wrapper from the given IO object.
-
.open_file(filename, *arg) ⇒ Object
Creates a new input stream wrapper to open file filename by using File.open.
-
.open_uri(uri, *arg) ⇒ Object
Creates a new input stream wrapper from URI specified as uri.
Instance Method Summary collapse
-
#close ⇒ Object
Closes the IO object if possible.
-
#eof? ⇒ Boolean
Returns true if end-of-file.
-
#getc ⇒ Object
Same as IO#getc.
-
#gets(io_rs = $/) ⇒ Object
Same as IO#gets.
-
#initialize(io, path) ⇒ BufferedInputStream
constructor
Creates a new input stream wrapper.
-
#pos ⇒ Object
Returns current file position.
-
#pos=(p) ⇒ Object
Sets current file position if possible Internal buffer in this wrapper is cleared.
-
#prefetch_buffer ⇒ Object
Gets current prefetch buffer.
-
#prefetch_gets(*arg) ⇒ Object
It does @io.gets, and addes returned string to the internal buffer, and returns the string.
-
#prefetch_readpartial(*arg) ⇒ Object
It does @io.readpartial, and addes returned string to the internal buffer, and returns the string.
-
#rewind ⇒ Object
Rewinds the IO object if possible Internal buffer in this wrapper is cleared.
-
#skip_spaces ⇒ Object
Skips space characters in the stream.
-
#to_io ⇒ Object
Converts to IO object if possible.
-
#ungetc(c) ⇒ Object
Pushes back one character into the internal buffer.
-
#ungets(str) ⇒ Object
Pushes back given str to the internal buffer.
Constructor Details
#initialize(io, path) ⇒ BufferedInputStream
Creates a new input stream wrapper
24 25 26 27 28 29 |
# File 'lib/bio/io/flatfile/buffer.rb', line 24 def initialize(io, path) @io = io @path = path # initialize prefetch buffer @buffer = '' end |
Instance Attribute Details
#path ⇒ Object (readonly)
Pathname, filename or URI to open the object. Like File#path, returned value isn’t normalized.
87 88 89 |
# File 'lib/bio/io/flatfile/buffer.rb', line 87 def path @path end |
Class Method Details
.for_io(io) ⇒ Object
Creates a new input stream wrapper from the given IO object.
32 33 34 35 36 37 38 39 |
# File 'lib/bio/io/flatfile/buffer.rb', line 32 def self.for_io(io) begin path = io.path rescue NameError path = nil end self.new(io, path) end |
.open_file(filename, *arg) ⇒ Object
Creates a new input stream wrapper to open file filename by using File.open. *arg is passed to File.open.
Like File.open, a block can be accepted.
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/bio/io/flatfile/buffer.rb', line 46 def self.open_file(filename, *arg) if block_given? then File.open(filename, *arg) do |fobj| yield self.new(fobj, filename) end else fobj = File.open(filename, *arg) self.new(fobj, filename) end end |
.open_uri(uri, *arg) ⇒ Object
Creates a new input stream wrapper from URI specified as uri. by using OpenURI.open_uri or URI#open. uri must be a String or URI object. *arg is passed to OpenURI.open_uri or URI#open.
Like OpenURI.open_uri, it can accept a block.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/bio/io/flatfile/buffer.rb', line 63 def self.open_uri(uri, *arg) if uri.kind_of?(URI) if block_given? uri.open(*arg) do |fobj| yield self.new(fobj, uri.to_s) end else fobj = uri.open(*arg) self.new(fobj, uri.to_s) end else if block_given? OpenURI.open_uri(uri, *arg) do |fobj| yield self.new(fobj, uri) end else fobj = OpenURI.open_uri(uri, *arg) self.new(fobj, uri) end end end |
Instance Method Details
#close ⇒ Object
Closes the IO object if possible
95 96 97 |
# File 'lib/bio/io/flatfile/buffer.rb', line 95 def close @io.close end |
#eof? ⇒ Boolean
Returns true if end-of-file. Otherwise, returns false.
Note that it returns false if internal buffer is this wrapper is not empty,
124 125 126 127 128 129 130 |
# File 'lib/bio/io/flatfile/buffer.rb', line 124 def eof? if @buffer.size > 0 false else @io.eof? end end |
#getc ⇒ Object
Same as IO#getc.
184 185 186 187 188 189 190 191 192 |
# File 'lib/bio/io/flatfile/buffer.rb', line 184 def getc if @buffer.size > 0 then r = @buffer[0] @buffer = @buffer[1..-1] else r = @io.getc end r end |
#gets(io_rs = $/) ⇒ Object
Same as IO#gets.
Compatibility note: the bahavior of paragraph mode (io_rs = ”) may differ from that of IO#gets(”).
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/bio/io/flatfile/buffer.rb', line 136 def gets(io_rs = $/) if @buffer.size > 0 if io_rs == nil then r = @buffer + @io.gets(nil).to_s @buffer = '' else if io_rs == '' then # io_rs.empty? sp_rs = /((?:\r?\n){2,})/n else sp_rs = io_rs end a = @buffer.split(sp_rs, 2) if a.size > 1 then r = a.shift r += (io_rs.empty? ? a.shift : io_rs) @buffer = a.shift.to_s else @buffer << @io.gets(io_rs).to_s a = @buffer.split(sp_rs, 2) if a.size > 1 then r = a.shift r += (io_rs.empty? ? a.shift : io_rs) @buffer = a.shift.to_s else r = @buffer @buffer = '' end end end r else @io.gets(io_rs) end end |
#pos ⇒ Object
Returns current file position
108 109 110 |
# File 'lib/bio/io/flatfile/buffer.rb', line 108 def pos @io.pos - @buffer.size end |
#pos=(p) ⇒ Object
Sets current file position if possible Internal buffer in this wrapper is cleared.
114 115 116 117 118 |
# File 'lib/bio/io/flatfile/buffer.rb', line 114 def pos=(p) r = (@io.pos = p) @buffer = '' r end |
#prefetch_buffer ⇒ Object
Gets current prefetch buffer
202 203 204 |
# File 'lib/bio/io/flatfile/buffer.rb', line 202 def prefetch_buffer @buffer end |
#prefetch_gets(*arg) ⇒ Object
It does @io.gets, and addes returned string to the internal buffer, and returns the string.
208 209 210 211 212 |
# File 'lib/bio/io/flatfile/buffer.rb', line 208 def prefetch_gets(*arg) r = @io.gets(*arg) @buffer << r if r r end |
#prefetch_readpartial(*arg) ⇒ Object
It does @io.readpartial, and addes returned string to the internal buffer, and returns the string.
216 217 218 219 220 |
# File 'lib/bio/io/flatfile/buffer.rb', line 216 def prefetch_readpartial(*arg) r = @io.readpartial(*arg) @buffer << r if r r end |
#rewind ⇒ Object
Rewinds the IO object if possible Internal buffer in this wrapper is cleared.
101 102 103 104 105 |
# File 'lib/bio/io/flatfile/buffer.rb', line 101 def rewind r = @io.rewind @buffer = '' r end |
#skip_spaces ⇒ Object
Skips space characters in the stream. returns nil.
224 225 226 227 228 229 230 231 232 233 |
# File 'lib/bio/io/flatfile/buffer.rb', line 224 def skip_spaces ws = { ?\s => true, ?\n => true, ?\r => true, ?\t => true } while r = self.getc unless ws[r] then self.ungetc(r) break end end nil end |
#to_io ⇒ Object
Converts to IO object if possible
90 91 92 |
# File 'lib/bio/io/flatfile/buffer.rb', line 90 def to_io @io.to_io end |
#ungetc(c) ⇒ Object
Pushes back one character into the internal buffer. Unlike IO#getc, it can be called more than one time.
196 197 198 199 |
# File 'lib/bio/io/flatfile/buffer.rb', line 196 def ungetc(c) @buffer = sprintf("%c", c) + @buffer nil end |
#ungets(str) ⇒ Object
Pushes back given str to the internal buffer. Returns nil. str must be read previously with the wrapper object.
Note that in current implementation, the str can be everything, but please don’t depend on it.
178 179 180 181 |
# File 'lib/bio/io/flatfile/buffer.rb', line 178 def ungets(str) @buffer = str + @buffer nil end |