Solved

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

Posted on 2004-08-13
6
494 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
session migration servlets 2 36
servlet example issue 6 38
Error in @AspectJ Based AOP with Spring 2 13
ejb on wildfly 5 19
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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

785 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