Java – Compressing array of integers in java


I have some extremely large array of integers which i would like to compress.
However the way to do it in java is to use something like this –

int[] myIntArray;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new DeflaterOutputStream(byteArrayOutputStream));

Note that the int array first needs to be converted to bytes by java.
Now I know that is fast but it still needs to create a whole new byte array and scan through the entire original int array converting it to bytes and copying the value to the new byte array.

Is there any way to skip the byte conversion and make it compress the integers right away?

Best Solution

Skip the ObjectOutputStream and just store the ints directly as four bytes each. DataOutputStream.writeInt for instance is an easy way to do it.