Solved

How to monitor a writeObject() method to send its progress to a JProgressBar

Posted on 2004-08-13
6
483 Views
Last Modified: 2008-03-03
The thing is I have to write an object to a stream, but the size of the object can go up to 5 mb so I would like to give some feedback to the user.

I have a JProgressBar but don't know how to figure out the progress made by the writeObject() method.

I don't know if this is possible... Maybe using Threads?
0
Comment
Question by:b_loco
  • 2
  • 2
  • 2
6 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
ID: 11792661
It might be better to writeObject into a ByteArrayOutputStream and then write that. You can monitor the latter
0
 
LVL 7

Expert Comment

by:JugglerW
ID: 11792732
You may wrap your output stream in a Stream dervied from FilterOutputStream (see JDK doc) .
So you can count how many bytes are alread sent over stream.
But for the progression bar you must know beforehand how large your object is.

0
 
LVL 2

Author Comment

by:b_loco
ID: 11793261
I'm currently away from my work computer. Anyway, the code I have looks something like this:

try{
FileOutputStream fos = new FileOutputStream("t.tmp");  
ObjectOutputStream oos = new ObjectOutputStream(fos);  

oos.writeObject(1stObj);
oos.writeObject(2ndObj);  
 
oos.close();
} catch(IOException e) {
System.out.println(e.toString());
}

I'm sorry but up to now I have always avoided writing to streams byte by byte and so I don't really know how to do it.

Also, how can I get the Object's size?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Accepted Solution

by:
JugglerW earned 200 total points
ID: 11793489
Try this. I implemented CEHJs idea to use an ByteArrayBuffer because its easier to demonstrate. But note the ByteArrayBuffer needs as much memory on your system as object you write.

===== Save as ObjectWrite.java ===========

import java.io.*;
import java.io.ByteArrayOutputStream;
import java.io.Serializable;

public class ObjectWrite implements Serializable
{
    String s1;
    String s2;
    int    i1;

    public ObjectWrite(  )
    {
        s1     = "Hello";
        s2     = "World";
        i1     = 4711;
    }

    public String toString(  )
    {
        return "s1=" + s1 + " s2=" + s2 + " i1=" + i1;
    }

    public static void main( String[] args )
    {
        try
        {
            ObjectWrite           ow = new ObjectWrite(  );

            ByteArrayOutputStream baos = new ByteArrayOutputStream(  );
            ObjectOutputStream    oos  = new ObjectOutputStream( baos );
            oos.writeObject( ow ); // Write object to ByteArrayBuffer
            oos.close(  );

            System.out.println( "Object is " + baos.size(  ) + " bytes long" );

            // Write to file
            OutputStream out = new BufferedOutputStream( new FileOutputStream( "test.dat" ) );
            out.write( baos.toByteArray(  ) ); // You could write in smaller parts to update progress bar
            out.close(  );

            // Read from file
            ObjectInputStream in = new ObjectInputStream( new FileInputStream( "test.dat" ) );
            ObjectWrite       readed = (ObjectWrite)in.readObject(  );
            System.out.println( "read: " + readed );
        }
        catch ( IOException e )
        {
            e.printStackTrace(  );
        }
        catch ( ClassNotFoundException e )
        {
            e.printStackTrace(  );
        }
    }
}

===== End as ObjectWrite.java ===========
0
 
LVL 2

Author Comment

by:b_loco
ID: 11796018
Thank you both
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11799785
8-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 39
ArrayIndexOutOfBoundException 9 68
print map entry 34 55
fibonacci ten numbers 4 28
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now