Class: Ccrypto::Java::ManagedMemoryBuffer

Inherits:
Object
  • Object
show all
Includes:
DataConversion
Defined in:
lib/ccrypto/java/utils/memory_buffer.rb

Instance Method Summary collapse

Methods included from DataConversion

#from_b64, #from_b64_mime, #from_hex, included, #logger, #to_b64, #to_b64_mime, #to_bin, #to_hex, #to_java_bytes, #to_str

Constructor Details

#initialize(*args, &block) ⇒ ManagedMemoryBuffer

Returns a new instance of ManagedMemoryBuffer.



9
10
11
# File 'lib/ccrypto/java/utils/memory_buffer.rb', line 9

def initialize(*args, &block)
  @raf = java.io.RandomAccessFile.new(java.nio.file.Files.createTempFile(nil,".ccl").toFile, "rw")
end

Instance Method Details

#bytesObject



13
14
15
16
17
18
# File 'lib/ccrypto/java/utils/memory_buffer.rb', line 13

def bytes
  buf = ::Java::byte[@raf.length].new
  @raf.seek(0)
  @raf.read_fully(buf)
  buf
end

#dispose(wcnt = 32) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ccrypto/java/utils/memory_buffer.rb', line 33

def dispose(wcnt = 32)
  len = @raf.length
  @raf.seek(0)
 
  cnt = 0
  loop do
    @raf.write(SecureRandomEngine.random_bytes(len))
    @raf.seek(0)

    cnt += 1
    break if cnt >= wcnt
  end

  @raf = nil
  GC.start
end

#equals?(val) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
73
# File 'lib/ccrypto/java/utils/memory_buffer.rb', line 64

def equals?(val)
  case val
  when ::Java::byte[]
    bytes == val
  when String
    bytes == to_java_bytes(val)
  else
    raise MemoryBufferException, "Unknown how to compare with #{val}"
  end
end

#lengthObject



25
26
27
# File 'lib/ccrypto/java/utils/memory_buffer.rb', line 25

def length
  @raf.length
end

#posObject

Return current cursor position



21
22
23
# File 'lib/ccrypto/java/utils/memory_buffer.rb', line 21

def pos
  @raf.getFilePointer
end

#read(len) ⇒ Object



54
55
56
57
58
# File 'lib/ccrypto/java/utils/memory_buffer.rb', line 54

def read(len)
  buf = ::Java::byte[len].new
  @raf.read(buf,0,len)
  buf
end

#respond_to_missing?(mtd, *args, &block) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/ccrypto/java/utils/memory_buffer.rb', line 60

def respond_to_missing?(mtd, *args, &block)
  @raf.respond_to?(mtd, *args, &block)
end

#rewindObject



29
30
31
# File 'lib/ccrypto/java/utils/memory_buffer.rb', line 29

def rewind
  @raf.seek(0)
end

#write(val) ⇒ Object



50
51
52
# File 'lib/ccrypto/java/utils/memory_buffer.rb', line 50

def write(val)
  @raf.write(to_java_bytes(val))
end