How to create byte[] with size using reflections?

Introduction:
So, I'm using an API and I need to overwrite 2 100% identical functions, one returning byte[] and one returning short[]. To make sure I don't have to edit everything twice, I want to create a function returning the required one. But to achieve this I would need to create a byte[4096] or a short[4096] which cannot be casted to anything, so I thought about using reflections and check whether a byte[] or a short[] is required.
Problem:
I don't know how to create a fixed-sized byte[] using reflections, so I'm asking this right here.


Solution:

Array.newInstance(byte.class, size); // new byte[size];
Array.newInstance(byte.class, size, size); // new byte[size][];
Array.newInstance(byte.class, size1, size2); // new byte[size1][size2];
Jon Skeet
people
quotationmark

Just use java.lang.reflect.Array.newInstance:

Object byteArray = Array.newInstance(byte.class, 4096)
Object shortArray = Array.newInstance(short.class, 4096);

You can use the other methods in Array to manipulate the array.

people

See more on this question at Stackoverflow