class Bio::FlatFile::BufferedInputStream

Wrapper for a IO (or IO-like) object. It can input with a buffer.

Attributes

path[R]

Pathname, filename or URI to open the object. Like File#path, returned value isn't normalized.

Public Class Methods

for_io(io) click to toggle source

Creates a new input stream wrapper from the given IO object.

# File lib/bio/io/flatfile/buffer.rb, line 31
def self.for_io(io)
  begin
    path = io.path
  rescue NameError
    path = nil
  end
  self.new(io, path)
end
new(io, path) click to toggle source

Creates a new input stream wrapper

# File lib/bio/io/flatfile/buffer.rb, line 23
def initialize(io, path)
  @io = io
  @path = path
  # initialize prefetch buffer
  @buffer = ''
end
open_file(filename, *arg) { |self| ... } click to toggle source

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 48
def self.open_file(filename, *arg)
  params = _parse_file_open_arg(*arg)
  if params[:textmode] or /t/ =~ params[:fmode_string].to_s then
    textmode = true
  else
    textmode = false
  end
  if block_given? then
    File.open(filename, *arg) do |fobj|
      fobj.binmode unless textmode
      yield self.new(fobj, filename)
    end
  else
    fobj = File.open(filename, *arg)
    fobj.binmode unless textmode
    self.new(fobj, filename)
  end
end
open_uri(uri, *arg) { |self| ... } click to toggle source

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 146
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
      fobj0 = uri.open(*arg)
      self.new(fobj0, uri.to_s)
    end
  else
    if block_given?
      OpenURI.open_uri(uri, *arg) do |fobj|
        yield self.new(fobj, uri)
      end
    else
      fobj0 = OpenURI.open_uri(uri, *arg)
      self.new(fobj0, uri)
    end
  end
end

Public Instance Methods

close() click to toggle source

Closes the IO object if possible

# File lib/bio/io/flatfile/buffer.rb, line 178
def close
  @io.close
end
eof?() click to toggle source

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 207
def eof?
  if @buffer.size > 0
    false
  else
    @io.eof?
  end
end
getc() click to toggle source

Same as IO#getc.

# File lib/bio/io/flatfile/buffer.rb, line 267
def getc
  if @buffer.size > 0 then
    r = @buffer[0]
    @buffer = @buffer[1..-1]
  else
    r = @io.getc
  end
  r
end
gets(io_rs = $/) click to toggle source

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 219
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() click to toggle source

Returns current file position

# File lib/bio/io/flatfile/buffer.rb, line 191
def pos
  @io.pos - @buffer.size
end
pos=(p) click to toggle source

Sets current file position if possible Internal buffer in this wrapper is cleared.

# File lib/bio/io/flatfile/buffer.rb, line 197
def pos=(p)
  r = (@io.pos = p)
  @buffer = ''
  r
end
prefetch_buffer() click to toggle source

Gets current prefetch buffer

# File lib/bio/io/flatfile/buffer.rb, line 285
def prefetch_buffer
  @buffer
end
prefetch_gets(*arg) click to toggle source

It does @io.gets, and addes returned string to the internal buffer, and returns the string.

# File lib/bio/io/flatfile/buffer.rb, line 291
def prefetch_gets(*arg)
  r = @io.gets(*arg)
  @buffer << r if r
  r
end
prefetch_readpartial(*arg) click to toggle source

It does @io.readpartial, and addes returned string to the internal buffer, and returns the string.

# File lib/bio/io/flatfile/buffer.rb, line 299
def prefetch_readpartial(*arg)
  r = @io.readpartial(*arg)
  @buffer << r if r
  r
end
rewind() click to toggle source

Rewinds the IO object if possible Internal buffer in this wrapper is cleared.

# File lib/bio/io/flatfile/buffer.rb, line 184
def rewind
  r = @io.rewind
  @buffer = ''
  r
end
skip_spaces() click to toggle source

Skips space characters in the stream. returns nil.

# File lib/bio/io/flatfile/buffer.rb, line 307
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() click to toggle source

Converts to IO object if possible

# File lib/bio/io/flatfile/buffer.rb, line 173
def to_io
  @io.to_io
end
ungetc(c) click to toggle source

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 279
def ungetc(c)
  @buffer = sprintf("%c", c) + @buffer
  nil
end
ungets(str) click to toggle source

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 261
def ungets(str)
  @buffer = str + @buffer
  nil
end