Module: PkernelJce::IoUtils

Included in:
Pkernel::IoUtils
Defined in:
lib/pkernel_jce/io_utils.rb

Class Method Summary collapse

Class Method Details

.ensure_java_bytes(bin) ⇒ Object

end file_to_memory_byte_array



21
22
23
24
25
26
27
28
29
# File 'lib/pkernel_jce/io_utils.rb', line 21

def IoUtils.ensure_java_bytes(bin)
  if not bin.java_kind_of?(Java::byte[])
    bin.to_java_bytes
  elsif bin.is_a?(String)
    bin = bin.to_java.getBytes
  else
    bin
  end
end

.file_to_memory_byte_array(path) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/pkernel_jce/io_utils.rb', line 5

def IoUtils.file_to_memory_byte_array(path)
  if path.nil? or path.empty?
    raise PkernelJce::Error, "Given path '#{path}' to load to memory is nil or empty"
  else
    f = java.io.File.new(path)
    b = Java::byte[f.length].new
    dis = java.io.DataInputStream.new(java.io.FileInputStream.new(f))
    dis.readFully(b)
    dis.close

    b
  end
end

.from_hex(str) ⇒ Object



37
38
39
# File 'lib/pkernel_jce/io_utils.rb', line 37

def IoUtils.from_hex(str)
  org.bouncycastle.util.encoders.Hex.decode(str)
end

.to_hex(bin) ⇒ Object

end ensure_java_bytes



33
34
35
# File 'lib/pkernel_jce/io_utils.rb', line 33

def IoUtils.to_hex(bin)
  String.from_java_bytes(org.bouncycastle.util.encoders.Hex.encode(bin))
end