Module: GcryptoBcCms::IoUtils

Defined in:
lib/gcrypto_bc_cms/io_utils.rb

Class Method Summary collapse

Class Method Details

.ensure_java_bytes(bin) ⇒ Object

end file_to_memory_byte_array



67
68
69
70
71
72
73
# File 'lib/gcrypto_bc_cms/io_utils.rb', line 67

def IoUtils.ensure_java_bytes(bin)
  if not bin.java_kind_of?(Java::byte[])
    bin.to_java_bytes
  else
    bin
  end
end

.file_to_memory_byte_array(path) ⇒ Object

end read_chunk



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gcrypto_bc_cms/io_utils.rb', line 51

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

.load_input(opts = { }) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/gcrypto_bc_cms/io_utils.rb', line 5

def IoUtils.load_input(opts = { })
  if opts.nil? or opts.empty?
    raise GcryptoBcCms::Error, "No input given to load"
  else
    file = opts[:file]
    bin = opts[:bin]
    
    if not (file.nil? or file.empty?)
      fis = java.io.FileInputStream.new(file)
      fis
    elsif not bin.nil?
      begin
        bais = java.io.ByteArrayInputStream.new(bin.to_java_bytes)
      rescue NoMethodError
        bais = java.io.ByteArrayInputStream.new(bin)
      end
      bais
    else
      raise GcryptoBcCms::Error, "Neither file nor bin given to load input"
    end
  end
end

.read_chunk(is, opts = { }, &block) ⇒ Object

end load_input()



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gcrypto_bc_cms/io_utils.rb', line 31

def IoUtils.read_chunk(is, opts = { },&block)

  raise GcryptoBcCms::Error, "Cannot read chunk from nil input stream" if is.nil?
  raise GcryptoBcCms::Error, "Block required for read_chunk" if not block

  if not is.java_kind_of?(java.io.InputStream)
    raise GcryptoBcCms::Error, "Cannot read from '#{is.class}' object"
  end

  bufLen = opts[:buffer_length] || 10240
  
  b = Java::byte[bufLen].new
  while((read = is.read(b,0,b.length)) != -1)
    block.call(b,0,read)
  end
end