Class: BoundedIO

Inherits:
Object
  • Object
show all
Defined in:
lib/excavate/extractors/cpio/cpio.rb

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, length, &eof_callback) ⇒ BoundedIO

Returns a new instance of BoundedIO.



7
8
9
10
11
12
13
14
# File 'lib/excavate/extractors/cpio/cpio.rb', line 7

def initialize(io, length, &eof_callback)
  @io = io
  @length = length
  @remaining = length

  @eof_callback = eof_callback
  @eof = false
end

Instance Attribute Details

#lengthObject (readonly)

Returns the value of attribute length.



4
5
6
# File 'lib/excavate/extractors/cpio/cpio.rb', line 4

def length
  @length
end

#remainingObject (readonly)

Returns the value of attribute remaining.



5
6
7
# File 'lib/excavate/extractors/cpio/cpio.rb', line 5

def remaining
  @remaining
end

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
# File 'lib/excavate/extractors/cpio/cpio.rb', line 30

def eof?
  return false if @remaining > 0
  return @eof if @eof

  @eof_callback.call
  @eof = true
end

#read(size = nil) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/excavate/extractors/cpio/cpio.rb', line 16

def read(size=nil)
  return nil if eof?
  size = @remaining if size.nil?
  data = @io.read(size)
  @remaining -= data.bytesize
  eof?
  data
end

#sysread(size) ⇒ Object

Raises:

  • (EOFError)


25
26
27
28
# File 'lib/excavate/extractors/cpio/cpio.rb', line 25

def sysread(size)
  raise EOFError, "end of file reached" if eof?
  read(size)
end