-3

I have written code to Serialize/De-serialize java Object with

  1. FileOutputStream/ObjectOutputStream & FileInputStream/ObjectInputStream

    Vs

  2. FileOutputStream/ByteArrayOutputStream & FileInputStream/ByteArrayInputStream

    import java.io.*;  
    
    class ObjectData implements Serializable{
        private int id;
        private String name;
        private String city;
        private int dimensions[]; 
    
        public ObjectData(int size){
            this.id = 1;
            this.name="ravindra";
            this.city = "Newjersey";
            this.dimensions = new int[size];
            for (int i=0;i<size;i++){
                this.dimensions[i] = i;
            }
        }
        public String toString(){
            StringBuffer sb = new StringBuffer(dimensions.length);
            sb.append("ID:NAME:CITY:DIM:").append(id).append(":").append(name).append(":");
            sb.append(city).append(":").append(dimensions.length);
            return sb.toString();
        }
    }
    
    public class ObjectSize{
        public static void main(String args[]){
            if (args.length < 1){
                System.out.println("Usage: java ObjectSize intSize");
                return;
            }
            int size = Integer.parseInt(args[0]);
            ObjectData data = new ObjectData(size);
    
            // Writing object with ObjectOutputStream as Object
            try{
                long t1 = System.currentTimeMillis();
                FileOutputStream fos1 = new FileOutputStream("data1.txt");  
                ObjectOutputStream out1 = new ObjectOutputStream(fos1);
                out1.writeObject(data);
                out1.close();
                fos1.close();
                long t2 = System.currentTimeMillis();
                System.out.println("Write Object Data1 with OOS in Millis:"+(t2-t1));
    
                t1 = System.currentTimeMillis();
                FileInputStream fis1 = new FileInputStream("data1.txt");
                ObjectInputStream ois1 = new ObjectInputStream(fis1);   
                ObjectData data1 = (ObjectData)ois1.readObject();
                System.out.println("Read Object Data1 from OIS:"+data1);
                ois1.close();
                fis1.close();
                t2 = System.currentTimeMillis();
                System.out.println("Read Object Data1 with OIS in Millis:"+(t2-t1));
    
            }catch(Exception err1){
                err1.printStackTrace();
            }
    
            try{
                long t1 = System.currentTimeMillis();
                ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
                ObjectOutputStream oos2 = new ObjectOutputStream(bos2);
                oos2.writeObject(data);
                FileOutputStream fos2 = new FileOutputStream("data2.txt");
                fos2.write(bos2.toByteArray());
    
                /* Close resources */
                oos2.close();
                fos2.close();
                long t2 = System.currentTimeMillis();
                System.out.println("Write Object Data2 with ByteStreams in Millis:"+(t2-t1));
    
                t1 = System.currentTimeMillis();
                File file2 = new File("data2.txt");
                FileInputStream fis2 = new FileInputStream(file2);
                byte fileContent[] = new byte[(int)file2.length()];
                fis2.read(fileContent);
                ByteArrayInputStream bis2 = new ByteArrayInputStream(fileContent);
                /* Below two lines to be corrected */
                ObjectInputStream ois2 = new ObjectInputStream(bis2);
                ObjectData data2 = (ObjectData) ois2.readObject();
    
                /* Close resources */
                ois2.close();
                fis2.close();
                System.out.println("Read Object Data2 from BIS:"+data2);
                t2 = System.currentTimeMillis();
                System.out.println("Read Object Data2 with ByteStreams in Millis:"+(t2-t1));
            }catch(Exception err){
                err.printStackTrace();
            }
    
        }
    }
    

I have done this exercise since some users are facing issue with Serialization of large Objects with ObjectOutputStream. The problem has been solved for them by moving from ObjectOutputStream to ByteArrayOutputStream.

Reference articles:

http://www.javamex.com/tutorials/io/StreamCorruptedException.shtml Java StreamCorruptedException when sending object containing byte data over a certain size

Output from my program:

D:\Study\Java>java ObjectSize 100000000
Write Object Data1 with OOS in Millis:1497
Read Object Data1 from OIS:ID:NAME:CITY:DIM:1:ravindra:Newjersey:100000000
Read Object Data1 with OIS in Millis:1202
Write Object Data2 with ByteStreams in Millis:2451
Read Object Data2 from BIS:ID:NAME:CITY:DIM:1:ravindra:Newjersey:100000000
Read Object Data2 with ByteStreams in Millis:780

D:\Study\Java>java ObjectSize 100000000
Write Object Data1 with OOS in Millis:1545
Read Object Data1 from OIS:ID:NAME:CITY:DIM:1:ravindra:Newjersey:100000000
Read Object Data1 with OIS in Millis:1185
Write Object Data2 with ByteStreams in Millis:2611
Read Object Data2 from BIS:ID:NAME:CITY:DIM:1:ravindra:Newjersey:100000000
Read Object Data2 with ByteStreams in Millis:780

D:\Study\Java>java ObjectSize 100000000
Write Object Data1 with OOS in Millis:1529
Read Object Data1 from OIS:ID:NAME:CITY:DIM:1:ravindra:Newjersey:100000000
Read Object Data1 with OIS in Millis:1203
Write Object Data2 with ByteStreams in Millis:2387
Read Object Data2 from BIS:ID:NAME:CITY:DIM:1:ravindra:Newjersey:100000000
Read Object Data2 with ByteStreams in Millis:780

D:\Study\Java>dir data*.txt
 Volume in drive D is New Volume
 Volume Serial Number is 6268-2397

 Directory of D:\Study\Java

10/21/2015  10:32 PM       400,000,144 data1.txt
10/21/2015  10:32 PM       400,000,144 data2.txt
               2 File(s)    800,000,288 bytes
               0 Dir(s)  248,054,890,496 bytes free

After this exercise, I have found that:

  1. ObjectStreams are working faster than ByteStreams for write operation

  2. ByteStreams are better to send large Objects ( from above two articles)

  3. Write is faster with ObjectStreams & Read is faster with ByteStreams.

But why ByteStreams are taking more time compared to ObjectStreams for write operation unlike read operation? Is there a better way to improve performance ?

Note: data1.txt is written with ObjectStreams & data2.txt is written with ByteStreams.

Community
  • 1
  • 1
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211

1 Answers1

2

There is no 'object stream versus byte array stream' about any of this.

ObjectStreams are working faster than ByteStreams for write operation

No they aren't. Writing directly to the file via FileOutputStream is faster than writing to a ByteArrayOutputStream and then writing that to the file, and that is only to be expected. Why you would do the latter is a mystery. It can only waste time and space.

2) ByteStreams are better to send large Objects ( from above two articles)

Rubbish. If that's really what the articles say, they are rubbish too, but I'm not going to waste my time reading them to find out.

3) Write is faster with ObjectStreams & Read is faster with ByteStreams

Rubbish again. You can't write objects to byte streams. You can only write them to object streams. You aren't comparing these against each other, you're comparing writing directly versus indirectly to the file, and of course direct wins. Why wouldn't it?

But why ByteStreams are taking more time compared to ObjectStreams for write operation

Because they add latency.

unlike read operation?

Another fallacy. What you're measuring here is the effect of buffering, and you would get a similar or probably bigger improvement by interposing a BufferedInputStream between the FileInputStream and the ObjectInputStream. And waste less space. Similarly, a BufferedInputStream between the FileInputStream and the ObjectInputStream will improve reading.

Note: data1.txt is written with ObjectStreams & data2.txt is written with ByteStreams.

Again, this is completely untrue. Both are written with object streams.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • EJP: It would be great if you suggest optimized approach in writing & reading of large objects to and from file. What are the best APIs tor faster writes & reads of objects? – Ravindra babu Oct 21 '15 at 18:16
  • @Am_ I_Helpful My last point is correct. The object is written to an ObjectOutputStream; the ObjectOutputStream is written to a ByteArrayOutputStream; and the ByteArrayOutputStream is written to a FileOutputStream. You're just repeating the OP's original mistake here. – user207421 Oct 21 '15 at 18:19
  • @ravindra If that's your real question, ask it. Don't bury it in comments. But in general you don't need anything more than BufferedInput/OutoutStreams. – user207421 Oct 21 '15 at 18:23
  • Got it. Compared to some of trivial questions with positive score without any research and effort, this question does not need -ve score of -2 – Ravindra babu Oct 21 '15 at 18:23
  • @EJP - Sorry, I really mingled in them. Well, what finally you mentioned should appear better in your answer to the last point. Anyways, +1 as usual. – Am_I_Helpful Oct 21 '15 at 18:25
  • Downvoters : I am happy if you edit the question in useful way instead of -ve score. – Ravindra babu Oct 21 '15 at 18:25
  • To be frank, When I posted this question, I am expecting reply from EJP :) because I follow him. Luckly I got quick response. – Ravindra babu Oct 21 '15 at 18:27
  • @Am_I_Helpful 'What finally [I] mentioned' is not an answer to any point, let alone the final point. It is a reply to a comment. – user207421 Oct 21 '15 at 18:31
  • @ravindra Whinging about downvotes under this answer won't do you any good anywhere, but certainly not here. Your question is fundamentally invalid. No amount of editing could fix it. – user207421 Oct 22 '15 at 02:33