Solved

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

Posted on 2004-08-13
6
471 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

759 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

18 Experts available now in Live!

Get 1:1 Help Now