Performing Random Access on Read-Once Input Streams


You have an IO object, probably a socket, that doesn't support random-access methods like seek, pos=, and rewind. You want to treat this object like a file on disk, where you can jump around and reread parts of the file.


The simplest solution is to read the entire contents of the socket (or as much as you're going to need) and put it into a StringIO object. You can then treat the StringIO object exactly like a file:

	require 'socket'
	require 'stringio'
	sock ="", 80)
	sock.write("GET /
	file = # => "

" " this web page "


A socket is supposed to work just like a file, but sometimes the illusion breaks down. Since the data is coming from another computer over which you have no control, you can't just go back and reread data you've already read. That data has already been sent over the pipe, and the server doesn't care if you lost it or need to process it again.

If you have enough memory to read the entire contents of a socket, it's easy to put the results into a form that more closely simulates a file on disk. But you might not want to read the entire socket, or the socket may be one that keeps sending data until you close it. In that case you'll need to buffer the data as you read it. Instead of using memory for the entire contents of the socket (which may be infinite), you'll only use memory for the data you've actually read.

This code defines a BufferedIO class that adds data to an internal StringIO as it's read from its source:

	 def initialize(io)
	 @buff =
	 @source = io
	 @pos = 0

	 def read(x=nil)
	 to_read = x ? to_read = x+@buff.pos-@buff.size : nil
	 _append( if !to_read or to_read > 0

	 def pos=(x)
	 read(x-@buff.pos) if x > @buff.size
	 @buff.pos = x
	 def seek(x, whence=IO::SEEK_SET)
	 case whence
	 when IO::SEEK_SET then self.pos=(x)
	 when IO::SEEK_CUR then self.pos=(@buff.pos+x)
	 when IO::SEEK_END then read; self.pos=(@buff.size-x)
	 # Note: SEEK END reads all the socket data.

	 # Some methods can simply be delegated to the buffer.
	 ["pos", "rewind", "tell"].each do |m|
	 module_eval "def #{m}


	 def _append(s)
	 @buff << s
	 @buff.pos -= s.size

Now you can seek, rewind, and generally move around in an input socket as if it were a disk file. You only have to read as much data as you need:

	sock ="", 80)
	sock.write("GET /
	file = # => "

0 # => " 90 # => " this web page ", IO::SEEK_CUR) # => 95 # => " web page "

BufferedIO doesn't implement all the methods of IO, only the ones not implemented by socket-type IO objects. If you need the other methods, you should be able to implement the ones you need using the existing methods as guidelines. For instance, you could implement readline like this:

	class BufferedIO
	 def readline
	 oldpos = @buff.pos
	 line = @buff.readline unless @buff.eof?
	 if !line or line[-1] != ?

	 _append(@source.readline) # Finish the line
	 @buff.pos = oldpos # Go back to where we were
	 line = @buff.readline # Read the line again

	file.readline # => "by typing "",


See Also

  • Recipe 6.17, " Processing a Binary File," for more information on IO#seek



Date and Time



Files and Directories

Code Blocks and Iteration

Objects and Classes8

Modules and Namespaces

Reflection and Metaprogramming


Graphics and Other File Formats

Databases and Persistence

Internet Services

Web Development Ruby on Rails

Web Services and Distributed Programming

Testing, Debugging, Optimizing, and Documenting

Packaging and Distributing Software

Automating Tasks with Rake

Multitasking and Multithreading

User Interface

Extending Ruby with Other Languages

System Administration

Ruby Cookbook
Ruby Cookbook (Cookbooks (OReilly))
ISBN: 0596523696
EAN: 2147483647
Year: N/A
Pages: 399

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: