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.

Methods

close   eof?   for_io   getc   gets   new   open_file   open_uri   pos   pos=   prefetch_buffer   prefetch_gets   prefetch_readpartial   rewind   skip_spaces   to_io   ungetc   ungets  

Attributes

path  [R]  Pathname, filename or URI to open the object. Like File#path, returned value isn‘t normalized.

Public Class methods

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

[Source]

    # File lib/bio/io/flatfile/buffer.rb, line 32
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

[Source]

    # File lib/bio/io/flatfile/buffer.rb, line 24
24:       def initialize(io, path)
25:         @io = io
26:         @path = path
27:         # initialize prefetch buffer
28:         @buffer = ''
29:       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.

[Source]

    # File lib/bio/io/flatfile/buffer.rb, line 46
46:       def self.open_file(filename, *arg)
47:         if block_given? then
48:           File.open(filename, *arg) do |fobj|
49:             yield self.new(fobj, filename)
50:           end
51:         else
52:           fobj = File.open(filename, *arg)
53:           self.new(fobj, filename)
54:         end
55:       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.

[Source]

    # File lib/bio/io/flatfile/buffer.rb, line 63
63:       def self.open_uri(uri, *arg)
64:         if uri.kind_of?(URI)
65:           if block_given?
66:             uri.open(*arg) do |fobj|
67:               yield self.new(fobj, uri.to_s)
68:             end
69:           else
70:             fobj = uri.open(*arg)
71:             self.new(fobj, uri.to_s)
72:           end
73:         else
74:           if block_given?
75:             OpenURI.open_uri(uri, *arg) do |fobj|
76:               yield self.new(fobj, uri)
77:             end
78:           else
79:             fobj = OpenURI.open_uri(uri, *arg)
80:             self.new(fobj, uri)
81:           end
82:         end
83:       end

Public Instance methods

Closes the IO object if possible

[Source]

    # File lib/bio/io/flatfile/buffer.rb, line 95
95:       def close
96:         @io.close
97:       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, line 124
124:       def eof?
125:         if @buffer.size > 0
126:           false
127:         else
128:           @io.eof?
129:         end
130:       end

Same as IO#getc.

[Source]

     # File lib/bio/io/flatfile/buffer.rb, line 184
184:       def getc
185:         if @buffer.size > 0 then
186:           r = @buffer[0]
187:           @buffer = @buffer[1..-1]
188:         else
189:           r = @io.getc
190:         end
191:         r
192:       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, line 136
136:       def gets(io_rs = $/)
137:         if @buffer.size > 0
138:           if io_rs == nil then
139:             r = @buffer + @io.gets(nil).to_s
140:             @buffer = ''
141:           else
142:             if io_rs == '' then # io_rs.empty?
143:               sp_rs = /((?:\r?\n){2,})/n
144:             else
145:               sp_rs = io_rs
146:             end
147:             a = @buffer.split(sp_rs, 2)
148:             if a.size > 1 then
149:               r = a.shift
150:               r += (io_rs.empty? ? a.shift : io_rs)
151:               @buffer = a.shift.to_s
152:             else
153:               @buffer << @io.gets(io_rs).to_s
154:               a = @buffer.split(sp_rs, 2)
155:               if a.size > 1 then
156:                 r = a.shift
157:                 r += (io_rs.empty? ? a.shift : io_rs)
158:                 @buffer = a.shift.to_s
159:               else
160:                 r = @buffer
161:                 @buffer = ''
162:               end
163:             end
164:           end
165:           r
166:         else
167:           @io.gets(io_rs)
168:         end
169:       end

Returns current file position

[Source]

     # File lib/bio/io/flatfile/buffer.rb, line 108
108:       def pos
109:         @io.pos - @buffer.size
110:       end

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

[Source]

     # File lib/bio/io/flatfile/buffer.rb, line 114
114:       def pos=(p)
115:         r = (@io.pos = p)
116:         @buffer = ''
117:         r
118:       end

Gets current prefetch buffer

[Source]

     # File lib/bio/io/flatfile/buffer.rb, line 202
202:       def prefetch_buffer
203:         @buffer
204:       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, line 208
208:       def prefetch_gets(*arg)
209:         r = @io.gets(*arg)
210:         @buffer << r if r
211:         r
212:       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, line 216
216:       def prefetch_readpartial(*arg)
217:         r = @io.readpartial(*arg)
218:         @buffer << r if r
219:         r
220:       end

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

[Source]

     # File lib/bio/io/flatfile/buffer.rb, line 101
101:       def rewind
102:         r = @io.rewind
103:         @buffer = ''
104:         r
105:       end

Skips space characters in the stream. returns nil.

[Source]

     # File lib/bio/io/flatfile/buffer.rb, line 224
224:       def skip_spaces
225:         ws = { ?\s => true, ?\n => true, ?\r => true, ?\t => true }
226:         while r = self.getc
227:           unless ws[r] then
228:             self.ungetc(r)
229:             break
230:           end
231:         end
232:         nil
233:       end

Converts to IO object if possible

[Source]

    # File lib/bio/io/flatfile/buffer.rb, line 90
90:       def to_io
91:         @io.to_io
92:       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, line 196
196:       def ungetc(c)
197:         @buffer = sprintf("%c", c) + @buffer
198:         nil
199:       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.

[Source]

     # File lib/bio/io/flatfile/buffer.rb, line 178
178:       def ungets(str)
179:         @buffer = str + @buffer
180:         nil
181:       end

[Validate]