Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-08-13
6
Medium Priority
?
542 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 200 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Accepted Solution

by:
JugglerW earned 800 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

670 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