Module: IOExtras::AbstractInputStream

Includes:
Enumerable, FakeIO
Included in:
Zip::NullInputStream, Zip::ZipInputStream
Defined in:
lib/zip/ioextras.rb

Overview

Implements many of the convenience methods of IO such as gets, getc, readline and readlines depends on: input_finished?, produce_input and read

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FakeIO

#kind_of?

Methods included from Enumerable

#inject, #select_map

Instance Attribute Details

#linenoObject

Returns the value of attribute lineno.



33
34
35
# File 'lib/zip/ioextras.rb', line 33

def lineno
  @lineno
end

Instance Method Details

#each_line(aSepString = $/) ⇒ Object Also known as: each



96
97
98
99
100
101
# File 'lib/zip/ioextras.rb', line 96

def each_line(aSepString = $/)
  while true
	yield readline(aSepString)
  end
rescue EOFError
end

#flushObject



84
85
86
87
88
# File 'lib/zip/ioextras.rb', line 84

def flush
  retVal=@outputBuffer
  @outputBuffer=""
  return retVal
end

#gets(aSepString = $/) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/zip/ioextras.rb', line 67

def gets(aSepString=$/)
  @lineno = @lineno.next
  return read if aSepString == nil
  aSepString="#{$/}#{$/}" if aSepString == ""
  
  bufferIndex=0
  while ((matchIndex = @outputBuffer.index(aSepString, bufferIndex)) == nil)
	bufferIndex=@outputBuffer.length
	if input_finished?
	  return @outputBuffer.empty? ? nil : flush 
	end
	@outputBuffer << produce_input
  end
  sepIndex=matchIndex + aSepString.length
  return @outputBuffer.slice!(0...sepIndex)
end

#initializeObject



27
28
29
30
31
# File 'lib/zip/ioextras.rb', line 27

def initialize
  super
  @lineno = 0
  @outputBuffer = ""
end

#read(numberOfBytes = nil, buf = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/zip/ioextras.rb', line 35

def read(numberOfBytes = nil, buf = nil)
  buf = "" unless (buf)

  if numberOfBytes
    if numberOfBytes <= @outputBuffer.length
      buf[RANGE_ALL] = @outputBuffer.slice!(0, numberOfBytes)
    else
      if @outputBuffer.length > 0
        buf[RANGE_ALL] = @outputBuffer + sysread(numberOfBytes - @outputBuffer.length, buf)
        @outputBuffer.slice!(RANGE_ALL)
      else
        buf[RANGE_ALL] = sysread(nil, buf)
      end
    end
  else
    if @outputBuffer.length > 0
      buf[RANGE_ALL] = @outputBuffer + sysread(nil, buf)
      @outputBuffer.slice!(RANGE_ALL)
    else
      buf[RANGE_ALL] = sysread(nil, buf)
    end
  end

  buf
end

#readline(aSepString = $/) ⇒ Object

Raises:

  • (EOFError)


90
91
92
93
94
# File 'lib/zip/ioextras.rb', line 90

def readline(aSepString = $/)
  retVal = gets(aSepString)
  raise EOFError if retVal == nil
  return retVal
end

#readlines(aSepString = $/) ⇒ Object



61
62
63
64
65
# File 'lib/zip/ioextras.rb', line 61

def readlines(aSepString = $/)
  retVal = []
  each_line(aSepString) { |line| retVal << line }
  return retVal
end