Class: Ccrypto::Java::Compression

Inherits:
Object
  • Object
show all
Includes:
DataConversion, TR::CondUtils, TeLogger::TeLogHelper
Defined in:
lib/ccrypto/java/engines/compression_engine.rb

Instance Method Summary collapse

Methods included from DataConversion

#from_b64, #from_hex, included, #to_b64, #to_b64_mime, #to_bin, #to_hex, #to_java_bytes, #to_str

Constructor Details

#initialize(*args, &block) ⇒ Compression

Returns a new instance of Compression.

Raises:

  • (CompressionError)


14
15
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
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ccrypto/java/engines/compression_engine.rb', line 14

def initialize(*args, &block)

  @config = args.first
  raise CompressionError, "Compress Config is expected. Given #{@config}" if not @config.is_a?(Ccrypto::CompressionConfig)
  
  #if block

  #  outPath = block.call(:out_path)
  #  if is_empty?(outPath)
  #    outFile = block.call(:out_file) 
  #    raise CompressionError, "OutputStream required" if not outFile.is_a?(java.io.OutputStream)
  #    @out = outFile
  #  else
  #    @out = java.io.RandomAccessFile.new(java.io.File.new(outPath), "w")
  #  end

  #  @intBufSize = block.call(:int_buf_size) || 102400

  #else
  #  @intBufSize = 102400

  #end

  #@in = java.io.RandomAccessFile.new(java.nio.file.Files.createTempFile(nil,".zl").toFile, "rw")
  #@inPtr = 0

  case @config.level
  when :best_compression
    teLogger.debug "Compression with best compression"
    @eng = java.util.zip.Deflater.new(java.util.zip.Deflater::BEST_COMPRESSION)
  when :best_speed
    teLogger.debug "Compression with best speed"
    @eng = java.util.zip.Deflater.new(java.util.zip.Deflater::BEST_SPEED)
  when :no_compression
    teLogger.debug "No compression"
    @eng = java.util.zip.Deflater.new(java.util.zip.Deflater::NO_COMPRESSION)
  else
    teLogger.debug "Default compression"
    @eng = java.util.zip.Deflater.new(java.util.zip.Deflater::DEFAULT_COMPRESSION)
  end

  teLogger.debug "Default strategy"
  @eng.setStrategy(java.util.zip.Deflater::DEFAULT_STRATEGY)

  @os = java.io.ByteArrayOutputStream.new
  
end

Instance Method Details

#finalObject



86
87
88
# File 'lib/ccrypto/java/engines/compression_engine.rb', line 86

def final
     
end

#update(val) ⇒ Object

returns compressed output length



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ccrypto/java/engines/compression_engine.rb', line 63

def update(val)
  if val.length > 0
    teLogger.debug "Given #{val.length} bytes for compression"
    #teLogger.debug "Write ready-to-compress data : #{val.length}"
    #@in.write(to_java_bytes(val))

    @eng.setInput(to_java_bytes(val))

    @eng.finish

    baos = java.io.ByteArrayOutputStream.new
    buf = ::Java::byte[102400].new
    while not @eng.finished
      done = @eng.deflate(buf)
      @os.write(buf,0,done)
    end

    @os.toByteArray
  else
    ::Java::byte[0].new
  end
end