Class: ActiveFedora::FileIO
- Inherits:
-
Object
- Object
- ActiveFedora::FileIO
- 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
-
#pos ⇒ Object
readonly
Returns the value of attribute pos.
Instance Method Summary collapse
- #binmode ⇒ Object
- #binmode? ⇒ Boolean
-
#close ⇒ Object
Closes the file.
-
#initialize(fedora_file) ⇒ FileIO
constructor
A new instance of FileIO.
-
#read(amount = nil, buf = nil) ⇒ String
Read bytes from the file.
-
#rewind ⇒ Object
Rewinds the io object to the beginning.
- #size ⇒ Object (also: #length)
Constructor Details
#initialize(fedora_file) ⇒ FileIO
Returns a new instance of FileIO.
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
#pos ⇒ Object (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
#binmode ⇒ Object
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
30 31 32 |
# File 'lib/active_fedora/file_io.rb', line 30 def binmode? true end |
#close ⇒ Object
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.
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 < 0 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 |
#rewind ⇒ Object
Rewinds the io object to the beginning. Read will return bytes from the start of the file again.
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 |
#size ⇒ Object Also known as: length
19 20 21 |
# File 'lib/active_fedora/file_io.rb', line 19 def size @fedora_file.size end |