Q:

How to convert specified byte array to Point Array for displaying image in Java

I have a Base64 encoded String: iQA8AJ4ANwCjADYAqQA1ALAANAC7ADMAyAAyANUAMQACATEAEwEyACMBNABDATkAUwE8AGIBQABwAUQAfgFIAIsBTQCYAVEApQFVALIBWQC+AVwAygFgANcBYgDjAWQA8AFmAPwBZwAIAmcAFQJmACECZAAsAmMAOAJgAEMCXQBNAlkAVgJWAF8CUgBnAk4AbgJKAHUCRgB7AkIAgAI+AIQCOwCIAjgAjAI1AI8CMwCSAjIA
My system is required to Base64 decode this value. The decoded data is a series of 2 byte x 2 byte y coordinates (low order byte 1st). i.e. (-1,-1) or (0xFFFF, 0xFFFF) denotes a pen up.
How can I convert Based64 encoded to Point Array in Java?

A:

byte[] bytes = Base64.getDecoder().decode("8AG...");
ShortBuffer buf = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();
int npoints = bytes.length / 4;
for (int i = 0; i < npoints; ++i) {
    short x = buf.get();
    short y = buf.get();
}

To wit: wrap the bytes in a ByteBuffer. Set the byte order on little-endian, and fetch shorts.
Older answers exist for Base64. This usest the latest Base64 class to replace the older ones.

