class Bio::FlatFile::BufferedInputStream
Wrapper for a IO (or IO-like) object. It can input with a buffer.
Attributes
Pathname, filename or URI to open the object. Like File#path, returned value isn’t normalized.
Public Class Methods
Source
# File lib/bio/io/flatfile/buffer.rb 32 def self.for_io(io) 33 begin 34 path = io.path 35 rescue NameError 36 path = nil 37 end 38 self.new(io, path) 39 end
Creates a new input stream wrapper from the given IO object.
Source
# File lib/bio/io/flatfile/buffer.rb 24 def initialize(io, path) 25 @io = io 26 @path = path 27 # initialize prefetch buffer 28 @buffer = String.new 29 end
Creates a new input stream wrapper
Source
# File lib/bio/io/flatfile/buffer.rb 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 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.
Source
# File lib/bio/io/flatfile/buffer.rb 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 fobj0 = uri.open(*arg) 155 self.new(fobj0, 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 fobj0 = OpenURI.open_uri(uri, *arg) 164 self.new(fobj0, uri) 165 end 166 end 167 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.
Public Instance Methods
Source
# File lib/bio/io/flatfile/buffer.rb 179 def close 180 @io.close 181 end
Closes the IO object if possible
Source
# File lib/bio/io/flatfile/buffer.rb 208 def eof? 209 if @buffer.size > 0 210 false 211 else 212 @io.eof? 213 end 214 end
Returns true if end-of-file. Otherwise, returns false.
Note that it returns false if internal buffer is this wrapper is not empty,
Source
# File lib/bio/io/flatfile/buffer.rb 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#getc.
Source
# File lib/bio/io/flatfile/buffer.rb 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
Same as IO#gets.
Compatibility note: the bahavior of paragraph mode (io_rs = ”) may differ from that of IO#gets(”).
Source
# File lib/bio/io/flatfile/buffer.rb 192 def pos 193 @io.pos - @buffer.size 194 end
Returns current file position
Source
# File lib/bio/io/flatfile/buffer.rb 198 def pos=(p) 199 r = (@io.pos = p) 200 @buffer = '' 201 r 202 end
Sets current file position if possible Internal buffer in this wrapper is cleared.
Source
# File lib/bio/io/flatfile/buffer.rb 286 def prefetch_buffer 287 @buffer 288 end
Gets current prefetch buffer
Source
# File lib/bio/io/flatfile/buffer.rb 292 def prefetch_gets(*arg) 293 r = @io.gets(*arg) 294 @buffer << r if r 295 r 296 end
It does @io.gets, and addes returned string to the internal buffer, and returns the string.
Source
# File lib/bio/io/flatfile/buffer.rb 300 def prefetch_readpartial(*arg) 301 r = @io.readpartial(*arg) 302 @buffer << r if r 303 r 304 end
It does @io.readpartial, and addes returned string to the internal buffer, and returns the string.
Source
# File lib/bio/io/flatfile/buffer.rb 185 def rewind 186 r = @io.rewind 187 @buffer = '' 188 r 189 end
Rewinds the IO object if possible Internal buffer in this wrapper is cleared.
Source
# File lib/bio/io/flatfile/buffer.rb 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
Skips space characters in the stream. returns nil.
Source
# File lib/bio/io/flatfile/buffer.rb 174 def to_io 175 @io.to_io 176 end
Converts to IO object if possible
Source
# File lib/bio/io/flatfile/buffer.rb 280 def ungetc(c) 281 @buffer = sprintf("%c", c) + @buffer 282 nil 283 end
Pushes back one character into the internal buffer. Unlike IO#getc, it can be called more than one time.
Source
# File lib/bio/io/flatfile/buffer.rb 262 def ungets(str) 263 @buffer = str + @buffer 264 nil 265 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.