How it is possible to convert image to byte array and then back to image? I tried this but it will throw IllegalArgumentException

Code:
Image exampleImage = Image.createImage( "/test.jpg" );	
	
int[] rgb = new int[exampleImage.getWidth() * exampleImage.getHeight()];
exampleImage.getRGB(rgb, 0, exampleImage.getWidth(), 0, 0, exampleImage.getWidth(), exampleImage.getHeight());
int rgbLength = rgb.length;

for (int i = 0; i < rgbLength; i++) 
{
    dataOutputStream.writeInt(rgb[i]);
}

byte[] rgbByteArray = byteArrayOutputStream.toByteArray();
	
// this will fail
Image second = Image.createImage( rgbByteArray, 0, rgbByteArray.length );