Java: Offsetting a byte[] without performing a copy operation

arrays, java, memory-management, performance

Solution

You can pass `ByteBuffer` around instead. It can be advanced, duplicated, sliced without copying.

ByteBuffer is really ugly and counter-intuitive. However it's being used extensively in new JDK APIs, so one can probably accept that it's a basic type.

Problem

I was wondering if it is possible, access a `byte[]` with an offset without having to copy data around? I've looked at `Arrays.*`, `ByteArrayInputStream` and `System.arraycopy`, but they all require to allocate a new `byte[]` to copy to. What I want is an equivalent to this in `C++`: ``` char* buffer = new char[256]; char* buf_offset = buffer + 128; // <- no copy ```

Original source