Solved

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

Posted on 2004-08-13
6
533 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

617 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