Converting a byte array to an object increases the size ..?

my code looks like this.

private Object populateObj(InputStream inputStream) {
        int i=0;
        Object resObject = null;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int next = inputStream.read();
            while (next > -1) {
                i++;
                bos.write(next);
                next = inputStream.read();
            }
            bos.flush();
            byte[] result = bos.toByteArray();
            System.out.println("Size of byte array 1: "+result.length);
            System.out.println("Result is []");
            for(int j=0;j<result.length;j++){
                System.out.println(result[j]);
            }
            resObject = result;
            byte[]  result2=toByteArray(resObject);
            System.out.println("Size of byte array 2 : "+result2.length);
            System.out.println("Result2 is []");
            for(int j=0;j<result.length;j++){
                System.out.println(result2[j]);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("size of inputStream in bytes : "+i);
        return resObject;
    }

      

to convert object to byte array: -

public static byte[] toByteArray(Object obj) throws IOException {
        byte[] bytes = null;
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        try {
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(obj);
            oos.flush();
            bytes = bos.toByteArray();
        } finally {
            if (oos != null) {
                oos.close();
            }
            if (bos != null) {
                bos.close();
            }
        }
        return bytes;
    }

      

when i compare the length of both byte arrays they are different. An extra byte is added, but at what point in the conversion ..? (when assigning a byte array to an object, or when) The size of the input stream is the same size as the first byte array.

I want to convert the http request input stream to a resObject object which can then be passed to outputstream.Using ObjectInputStream.readObject () provides a StreamCorruptedException.

+3


source to share


2 answers


You are serializing a byte array using Java Object Serialization. This has an overhead over a massive byte array as the serialized stream contains the serialization header, the class name of the object being serialized, and maybe some extra bytes to know where the object starts and ends in the stream.



Remember that when deserializing, the receiver knows nothing about the contents of the ObjectInputStream. Therefore, he should be able to know that the serialized array is a single byte array (not String, Integer, or any other serializable object). Therefore this information has to be stored in the stream in some way and therefore needs additional bytes.

+8


source


    byte[] array = new byte[] { 1, 2, 3, 4 };
    System.out.println(array.length);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(array);
    oos.close();

    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ObjectInputStream ois = new ObjectInputStream(bais);
    Object obj = ois.readObject();
    ois.close();

    System.out.println(((byte[]) obj).length);

      



0


source







All Articles