Module: Ccrypto::Java::DataConversion

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

Add the methods to class level



93
94
95
96
97
# File 'lib/ccrypto/java/data_conversion.rb', line 93

def self.included(klass)
  klass.class_eval <<-END
    extend Ccrypto::Java::DataConversion
  END
end

Instance Method Details

#from_b64(str) ⇒ Object



32
33
34
# File 'lib/ccrypto/java/data_conversion.rb', line 32

def from_b64(str)
  java.util.Base64.decoder.decode(str)
end

#from_b64_mime(str) ⇒ Object



28
29
30
# File 'lib/ccrypto/java/data_conversion.rb', line 28

def from_b64_mime(str)
  java.util.Base64.mime_decoder.decode(str)
end

#from_hex(str) ⇒ Object

end to_hex



13
14
15
# File 'lib/ccrypto/java/data_conversion.rb', line 13

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

#loggerObject



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

def logger
  Ccrypto::Java.logger(:data_conv)
end

#to_b64(bin) ⇒ Object

end from_hex



18
19
20
# File 'lib/ccrypto/java/data_conversion.rb', line 18

def to_b64(bin)
  String.from_java_bytes(java.util.Base64.encoder.encode(to_java_bytes(bin)))
end

#to_b64_mime(bin) ⇒ Object

end to_b64



24
25
26
# File 'lib/ccrypto/java/data_conversion.rb', line 24

def to_b64_mime(bin)
  String.from_java_bytes(java.util.Base64.mime_encoder.encode(to_java_bytes(bin)))
end

#to_bin(str) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/ccrypto/java/data_conversion.rb', line 45

def to_bin(str)
  if str.nil?
    ::Java::byte[0].new
  else
    str.to_java.getBytes
  end
end

#to_hex(bin) ⇒ Object



8
9
10
# File 'lib/ccrypto/java/data_conversion.rb', line 8

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

#to_java_bytes(val, encoding = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
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/data_conversion.rb', line 53

def to_java_bytes(val, encoding = nil)
  #logger.debug "Given #{val} to convert to java bytes"
  case val
  when String
    val.to_java_bytes
  when Array
    val.join.to_java_bytes
  when java.lang.String
    if not_empty?(encoding)
      val.getBytes(encoding)
    else
      val.getBytes
    end
  when Ccrypto::Java::ManagedMemoryBuffer
    val.bytes
  when ::Tempfile
    val.rewind
    baos = java.io.ByteArrayOutputStream.new
    while not val.eof?
      baos.write(val.read(Java::READ_BUF_SIZE).to_java_bytes)
    end
    res = baos.toByteArray
    logger.debug "Tempfile has #{res.length} bytes"
    res
  else
    if val.respond_to?(:encoded)
      val.encoded
    else
      val
    end
  end
end

#to_str(bin) ⇒ Object

end from_b64



37
38
39
40
41
42
43
# File 'lib/ccrypto/java/data_conversion.rb', line 37

def to_str(bin)
  if bin.is_a?(::Java::byte[])
    String.from_java_bytes(bin)
  else
    bin
  end
end