Module: Zip::IOExtras::AbstractInputStream

Includes:
Enumerable, FakeIO
Included in:
Zip::InputStream, NullInputStream
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?

Instance Attribute Details

#linenoObject

Returns the value of attribute lineno.



45
46
47
# File 'lib/zip/ioextras.rb', line 45

def lineno
  @lineno
end

#posObject (readonly)

Returns the value of attribute pos.



46
47
48
# File 'lib/zip/ioextras.rb', line 46

def pos
  @pos
end

Instance Method Details

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



128
129
130
131
132
133
# File 'lib/zip/ioextras.rb', line 128

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

#flushObject



116
117
118
119
120
# File 'lib/zip/ioextras.rb', line 116

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

#gets(aSepString = $/, numberOfBytes = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/zip/ioextras.rb', line 84

def gets(aSepString = $/, numberOfBytes = nil)
  @lineno = @lineno.next

  if numberOfBytes.respond_to?(:to_int)
    numberOfBytes = numberOfBytes.to_int
    aSepString = aSepString.to_str if aSepString
  elsif aSepString.respond_to?(:to_int)
    numberOfBytes = aSepString.to_int
    aSepString    = $/
  else
    numberOfBytes = nil
    aSepString = aSepString.to_str if aSepString
  end

  return read(numberOfBytes) if aSepString.nil?
  aSepString = "#{$/}#{$/}" if aSepString.empty?

  bufferIndex = 0
  overLimit   = (numberOfBytes && @output_buffer.bytesize >= numberOfBytes)
  while ((matchIndex = @output_buffer.index(aSepString, bufferIndex)) == nil && !overLimit)
    bufferIndex = [bufferIndex, @output_buffer.bytesize - aSepString.bytesize].max
    if input_finished?
      return @output_buffer.empty? ? nil : flush
    end
    @output_buffer << produce_input
    overLimit = (numberOfBytes && @output_buffer.bytesize >= numberOfBytes)
  end
  sepIndex = [matchIndex + aSepString.bytesize, numberOfBytes || @output_buffer.bytesize].min
  @pos     += sepIndex
  return @output_buffer.slice!(0...sepIndex)
end

#initializeObject



38
39
40
41
42
43
# File 'lib/zip/ioextras.rb', line 38

def initialize
  super
  @lineno        = 0
  @pos           = 0
  @output_buffer = ""
end

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/zip/ioextras.rb', line 48

def read(numberOfBytes = nil, buf = nil)
  tbuf = nil

  if @output_buffer.bytesize > 0
    if numberOfBytes <= @output_buffer.bytesize
      tbuf = @output_buffer.slice!(0, numberOfBytes)
    else
      numberOfBytes -= @output_buffer.bytesize if (numberOfBytes)
      rbuf = sysread(numberOfBytes, buf)
      tbuf = @output_buffer
      tbuf << rbuf if (rbuf)
      @output_buffer = ""
    end
  else
    tbuf = sysread(numberOfBytes, buf)
  end

  @pos += tbuf.length

  return nil unless (tbuf)

  if buf
    buf.replace(tbuf)
  else
    buf = tbuf
  end

  buf
end

#readline(aSepString = $/) ⇒ Object

Raises:

  • (EOFError)


122
123
124
125
126
# File 'lib/zip/ioextras.rb', line 122

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

#readlines(aSepString = $/) ⇒ Object



78
79
80
81
82
# File 'lib/zip/ioextras.rb', line 78

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