Class | Bio::FlatFile::BufferedInputStream |
In: |
lib/bio/io/flatfile/buffer.rb
|
Parent: | Object |
Wrapper for a IO (or IO-like) object. It can input with a buffer.
path | [R] | Pathname, filename or URI to open the object. Like File#path, returned value isn‘t normalized. |
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.
Unlike File.open, the default is binary mode, unless text mode is explicity specified in mode.
# File lib/bio/io/flatfile/buffer.rb, line 49 49: def self.open_file(filename, *arg) 50: params = _parse_file_open_arg(*arg) 51: if params[:textmode] or /t/ =~ params[:fmode_string].to_s then 52: textmode = true 53: else 54: textmode = false 55: end 56: if block_given? then 57: File.open(filename, *arg) do |fobj| 58: fobj.binmode unless textmode 59: yield self.new(fobj, filename) 60: end 61: else 62: fobj = File.open(filename, *arg) 63: fobj.binmode unless textmode 64: self.new(fobj, filename) 65: end 66: end
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.
# File lib/bio/io/flatfile/buffer.rb, line 147 147: def self.open_uri(uri, *arg) 148: if uri.kind_of?(URI) 149: if block_given? 150: uri.open(*arg) do |fobj| 151: yield self.new(fobj, uri.to_s) 152: end 153: else 154: fobj = uri.open(*arg) 155: self.new(fobj, uri.to_s) 156: end 157: else 158: if block_given? 159: OpenURI.open_uri(uri, *arg) do |fobj| 160: yield self.new(fobj, uri) 161: end 162: else 163: fobj = OpenURI.open_uri(uri, *arg) 164: self.new(fobj, uri) 165: end 166: end 167: end
Closes the IO object if possible
# File lib/bio/io/flatfile/buffer.rb, line 179 179: def close 180: @io.close 181: end
Returns true if end-of-file. Otherwise, returns false.
Note that it returns false if internal buffer is this wrapper is not empty,
# File lib/bio/io/flatfile/buffer.rb, line 208 208: def eof? 209: if @buffer.size > 0 210: false 211: else 212: @io.eof? 213: end 214: end
Same as IO#getc.
# File lib/bio/io/flatfile/buffer.rb, line 268 268: def getc 269: if @buffer.size > 0 then 270: r = @buffer[0] 271: @buffer = @buffer[1..-1] 272: else 273: r = @io.getc 274: end 275: r 276: end
Same as IO#gets.
Compatibility note: the bahavior of paragraph mode (io_rs = ’’) may differ from that of IO#gets(’’).
# File lib/bio/io/flatfile/buffer.rb, line 220 220: def gets(io_rs = $/) 221: if @buffer.size > 0 222: if io_rs == nil then 223: r = @buffer + @io.gets(nil).to_s 224: @buffer = '' 225: else 226: if io_rs == '' then # io_rs.empty? 227: sp_rs = /((?:\r?\n){2,})/n 228: else 229: sp_rs = io_rs 230: end 231: a = @buffer.split(sp_rs, 2) 232: if a.size > 1 then 233: r = a.shift 234: r += (io_rs.empty? ? a.shift : io_rs) 235: @buffer = a.shift.to_s 236: else 237: @buffer << @io.gets(io_rs).to_s 238: a = @buffer.split(sp_rs, 2) 239: if a.size > 1 then 240: r = a.shift 241: r += (io_rs.empty? ? a.shift : io_rs) 242: @buffer = a.shift.to_s 243: else 244: r = @buffer 245: @buffer = '' 246: end 247: end 248: end 249: r 250: else 251: @io.gets(io_rs) 252: end 253: end
Returns current file position
# File lib/bio/io/flatfile/buffer.rb, line 192 192: def pos 193: @io.pos - @buffer.size 194: end
Sets current file position if possible Internal buffer in this wrapper is cleared.
# File lib/bio/io/flatfile/buffer.rb, line 198 198: def pos=(p) 199: r = (@io.pos = p) 200: @buffer = '' 201: r 202: end
Gets current prefetch buffer
# File lib/bio/io/flatfile/buffer.rb, line 286 286: def prefetch_buffer 287: @buffer 288: end
It does @io.gets, and addes returned string to the internal buffer, and returns the string.
# File lib/bio/io/flatfile/buffer.rb, line 292 292: def prefetch_gets(*arg) 293: r = @io.gets(*arg) 294: @buffer << r if r 295: r 296: end
It does @io.readpartial, and addes returned string to the internal buffer, and returns the string.
# File lib/bio/io/flatfile/buffer.rb, line 300 300: def prefetch_readpartial(*arg) 301: r = @io.readpartial(*arg) 302: @buffer << r if r 303: r 304: end
Rewinds the IO object if possible Internal buffer in this wrapper is cleared.
# File lib/bio/io/flatfile/buffer.rb, line 185 185: def rewind 186: r = @io.rewind 187: @buffer = '' 188: r 189: end
Skips space characters in the stream. returns nil.
# File lib/bio/io/flatfile/buffer.rb, line 308 308: def skip_spaces 309: ws = { ?\s => true, ?\n => true, ?\r => true, ?\t => true } 310: while r = self.getc 311: unless ws[r] then 312: self.ungetc(r) 313: break 314: end 315: end 316: nil 317: end
Converts to IO object if possible
# File lib/bio/io/flatfile/buffer.rb, line 174 174: def to_io 175: @io.to_io 176: end
Pushes back one character into the internal buffer. Unlike IO#getc, it can be called more than one time.
# File lib/bio/io/flatfile/buffer.rb, line 280 280: def ungetc(c) 281: @buffer = sprintf("%c", c) + @buffer 282: nil 283: end
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.
# File lib/bio/io/flatfile/buffer.rb, line 262 262: def ungets(str) 263: @buffer = str + @buffer 264: nil 265: end