Class: RBzip2::Java::Decompressor

Inherits:
Object
  • Object
show all
Defined in:
lib/facter/util/rbzip2-0.3.0/lib/rbzip2/java/decompressor.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-2017, Sebastian Staudt

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Decompressor

Returns a new instance of Decompressor.



8
9
10
11
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/java/decompressor.rb', line 8

def initialize(io)
  @io = io
  @is = RBzip2::Java::BZip2CompressorInputStream.new io.to_inputstream
end

Instance Method Details

#closeObject



13
14
15
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/java/decompressor.rb', line 13

def close
  @is.close
end

#getcObject



17
18
19
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/java/decompressor.rb', line 17

def getc
  read(1)[0].chr
end

#getsObject



21
22
23
24
25
26
27
28
29
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/java/decompressor.rb', line 21

def gets
  line = ''
  loop do
    char = getc
    line += char
    break if char == "\n"
  end
  line
end

#inspectObject



61
62
63
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/java/decompressor.rb', line 61

def inspect
  "#<#{self.class}: @io=#{@io.inspect} size=#{size} uncompressed=#{uncompressed}>"
end

#read(length = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/java/decompressor.rb', line 31

def read(length = nil)
  if length.nil?
    bytes = Java::byte[0].new
    chunk = Java::byte[1024].new
    begin
      bytes_read = @is.read chunk
      chunk = chunk[0..(bytes_read - 1)] if bytes_read < 1024
      bytes += chunk
    end while bytes_read == 1024
  else
    bytes = Java::byte[length].new
    @is.read bytes
  end

  String.from_java_bytes bytes
end

#sizeObject



48
49
50
51
52
53
54
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/java/decompressor.rb', line 48

def size
  if @io.is_a? StringIO
    @io.size
  elsif @io.is_a? File
    @io.stat.size
  end
end

#uncompressedObject



56
57
58
59
# File 'lib/facter/util/rbzip2-0.3.0/lib/rbzip2/java/decompressor.rb', line 56

def uncompressed
  @data = read
  @data.size
end