Class: Gem::Package::TarWriter::BoundedStream

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygems/package/tar_writer.rb

Overview

IO wrapper that allows writing a limited amount of data

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, limit) ⇒ BoundedStream

Wraps io and allows up to limit bytes to be written



34
35
36
37
38
# File 'lib/rubygems/package/tar_writer.rb', line 34

def initialize(io, limit)
  @io = io
  @limit = limit
  @written = 0
end

Instance Attribute Details

#limitObject (readonly)

Maximum number of bytes that can be written



24
25
26
# File 'lib/rubygems/package/tar_writer.rb', line 24

def limit
  @limit
end

#writtenObject (readonly)

Number of bytes written



29
30
31
# File 'lib/rubygems/package/tar_writer.rb', line 29

def written
  @written
end

Instance Method Details

#write(data) ⇒ Object

Writes data onto the IO, raising a FileOverflow exception if the number of bytes will be more than #limit



44
45
46
47
48
49
50
51
# File 'lib/rubygems/package/tar_writer.rb', line 44

def write(data)
  if data.bytesize + @written > @limit
    raise FileOverflow, "You tried to feed more data than fits in the file."
  end
  @io.write data
  @written += data.bytesize
  data.bytesize
end