Module: TcryptoJava::IoUtils

Defined in:
lib/tcrypto_java/io_utils.rb

Class Method Summary collapse

Class Method Details

.attempt_zeroise(var) ⇒ Object

end ensure_java_bytes



125
126
127
# File 'lib/tcrypto_java/io_utils.rb', line 125

def IoUtils.attempt_zeroise(var)
  java.util.Arrays.fill(var, 0)
end

.ensure_java_bytes(bin) ⇒ Object

end file_to_memory_byte_array



115
116
117
118
119
120
121
# File 'lib/tcrypto_java/io_utils.rb', line 115

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



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/tcrypto_java/io_utils.rb', line 99

def IoUtils.file_to_memory_byte_array(path)
  if path.nil? or path.empty?
    raise TcryptoJava::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



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/tcrypto_java/io_utils.rb', line 12

def IoUtils.load_input(opts = { })
  if opts.nil? or opts.empty?
    raise TcryptoJava::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 => e
        bais = java.io.ByteArrayInputStream.new(bin)
      end
      bais
    else
      if not (opts[:input_optional].nil? and opts[:input_optional])
        raise TcryptoJava::Error, "Neither file nor bin given to load input"
      end
    end
  end
end

.prep_output(opts = { }) ⇒ Object

end load_input()



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/tcrypto_java/io_utils.rb', line 40

def IoUtils.prep_output(opts = { })
  
  if opts.nil? or opts.empty?
    raise TcryptoJava::Error, "No opts given to prep output"
  else
    file = opts[:outFile]
    
    if not (file.nil? or file.empty?)
      fos = java.io.FileOutputStream.new(file)
      fos
    else
      baos = java.io.ByteArrayOutputStream.new
      baos
    end
  end
  
end

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

Raises:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/tcrypto_java/io_utils.rb', line 78

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

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

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

  bufLen = opts[:buffer_size] || 1024*1024
  TcryptoJava::GConf.instance.glog.debug "read_chunk buffer size is #{NumberHelper.number_to_human_size(bufLen)}. Modifiable by caller app via key :in_buf -> :buffer_size"
  
  b = Java::byte[bufLen].new
  while((read = is.read(b,0,b.length)) != -1)
    block.call(b,0,read)
  end
end

.return_if_buffer(os) ⇒ Object



72
73
74
75
76
# File 'lib/tcrypto_java/io_utils.rb', line 72

def IoUtils.return_if_buffer(os)
  if os.java_kind_of?(java.io.ByteArrayOutputStream)
    os.toByteArray
  end
end

.to_file(path) ⇒ Object

Raises:

  • (Tcrypto::Error)


58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/tcrypto_java/io_utils.rb', line 58

def IoUtils.to_file(path)
  
  raise Tcrypto::Error, "Given path to convert to file object is nil or empty" if path.nil? or path.empty?
  
  file = java.io.File.new(path)
  raise Tcrypto::Error, "Given input file not available [#{file.absolute_path}]" if not file.exists
  raise Tcrypto::Error, "Given file path is a directory [#{file.absolute_path}]" if file.is_directory?

  src = java.io.FileInputStream.new(file)

  res = { file: file, file_is: src }
  res
end