Class: RBzip2::FFI::Compressor
- Inherits:
-
Object
- Object
- RBzip2::FFI::Compressor
- Extended by:
- FFI::Library
- Defined in:
- lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ffi/compressor.rb
Overview
This code is free software; you can redistribute it and/or modify it under the terms of the new BSD License.
Copyright © 2013, Brian Lopez Copyright © 2013-2017, Sebastian Staudt
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
- #flush ⇒ Object
-
#initialize(io) ⇒ Compressor
constructor
A new instance of Compressor.
- #putc(int) ⇒ Object
- #puts(line) ⇒ Object
- #write(bytes) ⇒ Object
Constructor Details
#initialize(io) ⇒ Compressor
Returns a new instance of Compressor.
51 52 53 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ffi/compressor.rb', line 51 def initialize(io) @io = io end |
Class Method Details
.compress(data, blksize = RBzip2::FFI::DEFAULT_BLK_SIZE, verbosity = 0, work_factor = 30) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ffi/compressor.rb', line 16 def self.compress(data, blksize = RBzip2::FFI::DEFAULT_BLK_SIZE, verbosity = 0, work_factor = 30) blksize = 1 if blksize < 1 blksize = 9 if blksize > 9 verbosity = 0 if verbosity < 0 verbosity = 4 if verbosity > 4 work_factor = 0 if work_factor < 0 work_factor = 250 if work_factor > 250 out_len = data.bytesize + (data.bytesize * 0.01) + 600 dst_buf = ::FFI::MemoryPointer.new :char, out_len dst_len = ::FFI::MemoryPointer.new :uint32 dst_len.write_uint out_len src_buf = ::FFI::MemoryPointer.new :char, data.bytesize src_buf.put_bytes 0, data ret = BZ2_bzBuffToBuffCompress dst_buf, dst_len, src_buf, data.bytesize, blksize, verbosity, work_factor case ret when RBzip2::FFI::BZ_OK dst_buf.read_bytes dst_len.read_uint when RBzip2::FFI::BZ_PARAM_ERROR raise ArgumentError, 'One of blksize, verbosity or work_factor is out of range' when RBzip2::FFI::BZ_MEM_ERROR raise NoMemoryError, 'Out of memory' when RBzip2::FFI::BZ_OUTBUFF_FULL raise RBzip2::FFI::BufferError, "Output buffer isn't large enough" when RBzip2::FFI::BZ_CONFIG_ERROR raise RBzip2::FFI::ConfigError, 'libbz2 has been mis-compiled' else raise RBzip2::FFI::Error, "Unhandled error code: #{ret}" end end |
Instance Method Details
#close ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ffi/compressor.rb', line 59 def close flush unless @io.nil? @io.close @io = nil end end |
#flush ⇒ Object
55 56 57 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ffi/compressor.rb', line 55 def flush @io.flush unless @io.nil? end |
#putc(int) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ffi/compressor.rb', line 67 def putc(int) if int.is_a? Numeric write int & 0xff else write int.to_s[0].chr end end |
#puts(line) ⇒ Object
75 76 77 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ffi/compressor.rb', line 75 def puts(line) write line + $/ end |
#write(bytes) ⇒ Object
79 80 81 82 83 |
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/ffi/compressor.rb', line 79 def write(bytes) raise 'stream closed' if @io.nil? @io.write self.class.compress(bytes, 9) end |