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
31 def self.for_io(io)
32   begin
33     path = io.path
34   rescue NameError
35     path = nil
36   end
37   self.new(io, path)
38 end
new(io, path) click to toggle source

Creates a new input stream wrapper

   # File lib/bio/io/flatfile/buffer.rb
23 def initialize(io, path)
24   @io = io
25   @path = path
26   # initialize prefetch buffer
27   @buffer = ''
28 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
48 def self.open_file(filename, *arg)
49   params = _parse_file_open_arg(*arg)
50   if params[:textmode] or /t/ =~ params[:fmode_string].to_s then
51     textmode = true
52   else
53     textmode = false
54   end
55   if block_given? then
56     File.open(filename, *arg) do |fobj|
57       fobj.binmode unless textmode
58       yield self.new(fobj, filename)
59     end
60   else
61     fobj = File.open(filename, *arg)
62     fobj.binmode unless textmode
63     self.new(fobj, filename)
64   end
65 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
146 def self.open_uri(uri, *arg)
147   if uri.kind_of?(URI)
148     if block_given?
149       uri.open(*arg) do |fobj|
150         yield self.new(fobj, uri.to_s)
151       end
152     else
153       fobj0 = uri.open(*arg)
154       self.new(fobj0, uri.to_s)
155     end
156   else
157     if block_given?
158       OpenURI.open_uri(uri, *arg) do |fobj|
159         yield self.new(fobj, uri)
160       end
161     else
162       fobj0 = OpenURI.open_uri(uri, *arg)
163       self.new(fobj0, uri)
164     end
165   end
166 end

Public Instance Methods

close() click to toggle source

Closes the IO object if possible

    # File lib/bio/io/flatfile/buffer.rb
178 def close
179   @io.close
180 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
207 def eof?
208   if @buffer.size > 0
209     false
210   else
211     @io.eof?
212   end
213 end
getc() click to toggle source

Same as IO#getc.

    # File lib/bio/io/flatfile/buffer.rb
267 def getc
268   if @buffer.size > 0 then
269     r = @buffer[0]
270     @buffer = @buffer[1..-1]
271   else
272     r = @io.getc
273   end
274   r
275 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
219 def gets(io_rs = $/)
220   if @buffer.size > 0
221     if io_rs == nil then
222       r = @buffer + @io.gets(nil).to_s
223       @buffer = ''
224     else
225       if io_rs == '' then # io_rs.empty?
226         sp_rs = /((?:\r?\n){2,})/n
227       else
228         sp_rs = io_rs
229       end
230       a = @buffer.split(sp_rs, 2)
231       if a.size > 1 then
232         r = a.shift
233         r += (io_rs.empty? ? a.shift : io_rs)
234         @buffer = a.shift.to_s
235       else
236         @buffer << @io.gets(io_rs).to_s
237         a = @buffer.split(sp_rs, 2)
238         if a.size > 1 then
239           r = a.shift
240           r += (io_rs.empty? ? a.shift : io_rs)
241           @buffer = a.shift.to_s
242         else
243           r = @buffer
244           @buffer = ''
245         end
246       end
247     end
248     r
249   else
250     @io.gets(io_rs)
251   end
252 end
pos() click to toggle source

Returns current file position

    # File lib/bio/io/flatfile/buffer.rb
191 def pos
192   @io.pos - @buffer.size
193 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
197 def pos=(p)
198   r = (@io.pos = p)
199   @buffer = ''
200   r
201 end
prefetch_buffer() click to toggle source

Gets current prefetch buffer

    # File lib/bio/io/flatfile/buffer.rb
285 def prefetch_buffer
286   @buffer
287 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
291 def prefetch_gets(*arg)
292   r = @io.gets(*arg)
293   @buffer << r if r
294   r
295 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
299 def prefetch_readpartial(*arg)
300   r = @io.readpartial(*arg)
301   @buffer << r if r
302   r
303 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
184 def rewind
185   r = @io.rewind
186   @buffer = ''
187   r
188 end
skip_spaces() click to toggle source

Skips space characters in the stream. returns nil.

    # File lib/bio/io/flatfile/buffer.rb
307 def skip_spaces
308   ws = { ?\s => true, ?\n => true, ?\r => true, ?\t => true }
309   while r = self.getc
310     unless ws[r] then
311       self.ungetc(r)
312       break
313     end
314   end
315   nil
316 end
to_io() click to toggle source

Converts to IO object if possible

    # File lib/bio/io/flatfile/buffer.rb
173 def to_io
174   @io.to_io
175 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
279 def ungetc(c)
280   @buffer = sprintf("%c", c) + @buffer
281   nil
282 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
261 def ungets(str)
262   @buffer = str + @buffer
263   nil
264 end