3 ms·
Can you give an example how to do that? I've only seen IO and parsing libraries that copy around byte arrays, but you can't cast byte arrays to arbitrary object
by tsss 4y ago
Can you give an example how to do that? I've only seen IO and parsing libraries that copy around byte arrays, but you can't cast byte arrays to arbitrary objects, which means you usually have to manipulate the bytes directly (unless you're lucky to work with only strings). At that point I imagine it would be far easier to just use C++ or Rust.
- marginalia_nu 4y agoYeah this sort of work isn't Java's strong suite. A lot of it's sort of like programming old-school C with oven mitts. It'll get a bit better with the Foreign Memory API which is in the JEP pipe. But a very bare bones example might look something like class MdrLayout { static final int FOO_OFFSET = 0; static final int BAR_OFFSET = 4; static final int BAZ_OFFSET = 12; static final int ENTRY_SIZE = 13; } class MyDataRecord { int idx = Integer.MIN_VALUE; ByteBuffer buffer = null; MyDataRecord() { } void movePointer(ByteBuffer buffer, int idx) { this.buffer = buffer; this.idx = idx; } int foo() { return buffer.getInt(ENTRY_SIZE * idx + FOO_OFFSET); } long bar() { return buffer.getLong(ENTRY_SIZE * idx + BAR_OFFSET); } byte baz() { return buffer.get(ENTRY_SIZE * idx + BAZ_OFFSET); } // may have operators like reset(), next() or skip() as well } or class MyDataRecord2 { final ByteBuffer buffer; MyDataRecord2(ByteBuffer buffer) { this.buffer = buffer; } int foo(int idx) { return buffer.getInt(ENTRY_SIZE * idx + FOO_OFFSET); } long bar(int idx) { return buffer.getLong(ENTRY_SIZE * idx + BAR_OFFSET); } byte baz(int idx) { return buffer.get(ENTRY_SIZE * idx + BAZ_OFFSET); } }