Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
Public Member Functions | Static Public Member Functions | List of all members
jau.test.io.TestMemBuffers00 Class Reference
Inheritance diagram for jau.test.io.TestMemBuffers00:
Collaboration diagram for jau.test.io.TestMemBuffers00:

Public Member Functions

final void test01_ByteBuffer ()
 
- Public Member Functions inherited from jau.test.junit.util.JunitTracer
final String getFullTestName (final String separator)
 
final String getSimpleTestName (final String separator)
 
final String getTestMethodName ()
 
final void setUpBase ()
 
final void tearDownBase ()
 

Static Public Member Functions

static void main (final String args[])
 
- Static Public Member Functions inherited from jau.test.junit.util.JunitTracer
static final boolean isTestSupported ()
 
static final void oneTimeSetUpBase ()
 
static final void oneTimeTearDownBase ()
 
static final void setTestSupported (final boolean v)
 
static void waitForKey (final String preMessage)
 

Additional Inherited Members

- Public Attributes inherited from jau.test.junit.util.JunitTracer
final TestName _unitTestName = new TestName()
 

Detailed Description

Definition at line 41 of file TestMemBuffers00.java.

Member Function Documentation

◆ test01_ByteBuffer()

final void jau.test.io.TestMemBuffers00.test01_ByteBuffer ( )

Definition at line 45 of file TestMemBuffers00.java.

◆ main()

static void jau.test.io.TestMemBuffers00.main ( final String  args[])
static
@Test(timeout = 10000)
public final void test02_String_ByteBuffer() {
    PlatformRuntime.checkInitialized();
    final ByteBuffer b0;
    {
        final String s1 = "0123456789"; // len = 10
        b0 = CPUtils.to_ByteBuffer(s1, false);
        final String s2 = new String(s1.getBytes()); // true copy before deletion
        final boolean r2 = CPUtils.zeroString(s2);

Assert.assertTrue( r1 ); CPUtils.fprintf_td(System.err, "test01_enc_dec_file_ok.1a: s1: '%s', len %d\n", s1, s1.length()); CPUtils.fprintf_td(System.err, "test01_enc_dec_file_ok.1a: s2: '%s', len %d, zeroString result %b\n", s2, s2.length(), r2);

final boolean r1 = CPUtils.zeroString(s1); CPUtils.fprintf_td(System.err, "test01_enc_dec_file_ok.1b: s1: '%s', len %d, zeroString result %b\n", s1, s1.length(), r1); } { Ooops, the internalized string at compile time got zeroed out final String s1 = "0123456789"; // len = 10 CPUtils.fprintf_td(System.err, "test01_enc_dec_file_ok.1c: s1: '%s', len %d\n", s1, s1.length()); } { That works, using dynamic storage final String s1 = CPUtils.to_String(b0, StandardCharsets.UTF_8); CPUtils.fprintf_td(System.err, "test01_enc_dec_file_ok.1d: s1: '%s', len %d\n", s1, s1.length()); } { final String s1 = CPUtils.to_String(b0, StandardCharsets.UTF_8); CPUtils.fprintf_td(System.err, "test01_enc_dec_file_ok.2a: s1: '%s', len %d\n", s1, s1.length());

final ByteBuffer b1 = CPUtils.newDirectByteBuffer(s1.length() * 2); Assert.assertEquals(s1.length() * 2, b1.capacity()); Assert.assertEquals(s1.length() * 2, b1.limit()); Assert.assertEquals(0, b1.position()); Assert.assertEquals(s1.length() * 2, b1.remaining());

b1.put(s1.getBytes(StandardCharsets.UTF_8)); Assert.assertEquals(s1.length(), b1.position()); b1.flip(); Assert.assertEquals(s1.length(), b1.limit()); Assert.assertEquals(0, b1.position()); Assert.assertEquals(s1.length(), b1.remaining()); { final String b1_str = CPUtils.to_String(b1, StandardCharsets.UTF_8); CPUtils.fprintf_td(System.err, "test01_enc_dec_file_ok.2b: b1: '%s', len %d/%d\n", b1_str, b1_str.length(), b1.limit()); } Assert.assertEquals(s1.length(), b1.limit()); Assert.assertEquals(0, b1.position()); Assert.assertEquals(s1.length(), b1.remaining());

final String s2 = new String(s1.getBytes()); // true copy final ByteBuffer b2 = CPUtils.to_ByteBuffer(s2, true); Assert.assertEquals(s1.length(), b2.capacity()); Assert.assertEquals(s1.length(), b2.limit()); Assert.assertEquals(0, b2.position()); Assert.assertEquals(s1.length(), b2.remaining()); { final byte[] b2_bytes = new byte[b2.remaining()]; b2.get(b2_bytes); b2.rewind(); // yada yada yada (relative get, absolute @ JDK13 final String b2_str = new String(b2_bytes, StandardCharsets.UTF_8); CPUtils.fprintf_td(System.err, "test01_enc_dec_file_ok.2c: b2: '%s', len %d/%d\n", b2_str, b2_str.length(), b2.limit()); } } }

Definition at line 139 of file TestMemBuffers00.java.


The documentation for this class was generated from the following file: