require_relative '../data_conversion' module Ccrypto module Java class Compression include DataConversion include TR::CondUtils def initialize(*args, &block) @config = args.first raise CompressionError, "Compress Config is expected. Given #{@config}" if not @config.is_a?(Ccrypto::CompressionConfig) 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 # returns compressed output length def update(val) if val.length > 0 teLogger.debug "Given #{val.length} bytes for compression" @eng.setInput(to_java_bytes(val)) @eng.finish baos = java.io.ByteArrayOutputStream.new buf = ::Java::byte[READ_BUF_SIZE].new while not @eng.finished done = @eng.deflate(buf) @os.write(buf,0,done) end @os.toByteArray else ::Java::byte[0].new end end def final end private def teLogger Java.logger(:comp_eng) end end end end