Class: MiniTarball::LimitedSizeStream

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_tarball/streams/limited_size_stream.rb

Direct Known Subclasses

PlaceholderStream

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, start_position:, max_file_size:) ⇒ LimitedSizeStream

Returns a new instance of LimitedSizeStream.



12
13
14
15
16
# File 'lib/mini_tarball/streams/limited_size_stream.rb', line 12

def initialize(io, start_position:, max_file_size:)
  @io = io
  @start_position = start_position
  @end_position = start_position + max_file_size
end

Instance Attribute Details

#end_positionObject (readonly)

Returns the value of attribute end_position.



8
9
10
# File 'lib/mini_tarball/streams/limited_size_stream.rb', line 8

def end_position
  @end_position
end

#start_positionObject (readonly)

Returns the value of attribute start_position.



8
9
10
# File 'lib/mini_tarball/streams/limited_size_stream.rb', line 8

def start_position
  @start_position
end

Instance Method Details

#write(data) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/mini_tarball/streams/limited_size_stream.rb', line 18

def write(data)
  current_position = io.pos

  if current_position < start_position || current_position + data.bytesize > end_position
    raise WriteOutOfRangeError.new("Writing outside of limits not allowed")
  end

  io.write(data)
end