jaulib v1.3.0
Jau Support Library (C++, Java, ..)
MemUtil.java
Go to the documentation of this file.
1package org.jau.io;
2
3import java.nio.ByteBuffer;
4
5public class MemUtil {
6 /**
7 * Zeros all bytes of given direct NIO byte buffer.
8 */
9 public static native void zeroByteBuffer(final ByteBuffer buf);
10
11 /**
12 * Zeros all underlying bytes of given Java string.
13 *
14 * Implementation either uses the JNI GetStringCritical() with is_copy == false or the String backing array `value`.
15 *
16 * @return true if successful, i.e. the underlying bytes could have been zeroed, otherwise returns false w/o zero'ed content.
17 public static native boolean zeroString(final String str);
18 */
19
20 /**
21 * Converts the Java string to a native direct ByteBuffer as UTF-8
22 * allowing better control over its memory region to {@link #zeroByteBuffer(ByteBuffer) zero} it after usage.
23 *
24 * @param str the string to convert
25 * @param zerostr pass true to zero the bytes of the given Java string afterwards (recommended!)
26 * @return direct ByteBuffer instance suitable for Cipherpack.
27 public static ByteBuffer to_ByteBuffer(final String str, final boolean zerostr) {
28 final ByteBuffer res = toByteBufferImpl(str);
29 res.limit(res.capacity());
30 res.position(0);
31 if( zerostr ) {
32 zeroString(str);
33 }
34 return res;
35 }
36 private static native ByteBuffer toByteBufferImpl(final String str);
37
38 public static String to_String(final ByteBuffer bb, final Charset cs) {
39 final int p0 = bb.position();
40 bb.position(0);
41 final byte[] bb_bytes = new byte[bb.limit()];
42 bb.get(bb_bytes);
43 bb.position(p0);
44 return new String(bb_bytes, cs);
45 }
46 */
47
48}
static native void zeroByteBuffer(final ByteBuffer buf)
Zeros all bytes of given direct NIO byte buffer.