Class: ActiveFedora::FileIO

Inherits:
Object
  • Object
show all
Defined in:
lib/active_fedora/file_io.rb

Overview

IO like object for reading Fedora files. Use ActiveFedora::FileIO.new(fedora_file) to create one. You can then call read on it or use it with IO.copy_stream and the like.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fedora_file) ⇒ FileIO

Returns a new instance of FileIO.

Parameters:



13
14
15
16
17
# File 'lib/active_fedora/file_io.rb', line 13

def initialize(fedora_file)
  @fedora_file = fedora_file
  @closed = false
  rewind # this initialises various variables
end

Instance Attribute Details

#posObject (readonly)

Returns the value of attribute pos.



9
10
11
# File 'lib/active_fedora/file_io.rb', line 9

def pos
  @pos
end

Instance Method Details

#binmodeObject



25
26
27
28
# File 'lib/active_fedora/file_io.rb', line 25

def binmode
  # Do nothing, just return self. The stream is essentially always in binmode.
  self
end

#binmode?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/active_fedora/file_io.rb', line 30

def binmode?
  true
end

#closeObject

Closes the file. No further action can be taken on the file.



83
84
85
86
87
# File 'lib/active_fedora/file_io.rb', line 83

def close
  @closed = true
  @stream_fiber = nil
  nil
end

#read(amount = nil, buf = nil) ⇒ String

Read bytes from the file. See IO.read for more information.

Parameters:

  • the (Integer)

    number of bytes to read. If nil or omitted, the entire contents will be read.

  • a (String)

    string in which the contents are read. Can be omitted.

Returns:

  • (String)

    the read bytes. If number of bytes to read was not specified then always returns a String, possibly an empty one. If number of bytes was specified then the returned string will always be at least one byte long. If no bytes are left in the file returns nil instead.

Raises:

  • (IOError)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/active_fedora/file_io.rb', line 44

def read(amount = nil, buf = nil)
  raise(IOError, "closed stream") if @closed

  buf ||= ''.force_encoding("ASCII-8BIT")
  buf.clear

  if amount.nil?
    read_to_buf(nil, buf) # read the entire file, returns buf
  elsif amount.negative?
    raise(ArgumentError, "negative length #{amount} given")
  elsif amount.zero?
    ''
  else
    read_to_buf(amount, buf)
    # if amount was specified but we reached eof before reading anything
    # then we must return nil
    buf.empty? ? nil : buf
  end
end

#rewindObject

Rewinds the io object to the beginning. Read will return bytes from the start of the file again.

Raises:

  • (IOError)


67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/active_fedora/file_io.rb', line 67

def rewind
  raise(IOError, "closed stream") if @closed
  @pos = 0
  @buffer = nil
  @stream_fiber = Fiber.new do
    @fedora_file.stream.each do |chunk|
      Fiber.yield chunk
    end
    @stream_fiber = nil
    # last value from Fiber is the return value of the block which should be nil
  end
  0
end

#sizeObject Also known as: length



19
20
21
# File 'lib/active_fedora/file_io.rb', line 19

def size
  @fedora_file.size
end