-1

I've created a stream from client to server. How to create ProgressMonitorInputStream or something else in this kind, for this piece of code?

        FileInputStream fileStream = new FileInputStream(file);
        int ch;
        do
        {
            ch = fileStream.read();
            exitStream.writeUTF(String.valueOf(ch));
        }
        while(ch != -1);
        fileStream.close();

Updated code - the window appear, but it's empty. Only a frame. How to fix it?

         String fileName = "aaa.jpg";
         File fileToBeSend = new File(fileName);

         InputStream input = new ProgressMonitorInputStream(
         null, 
         "Reading: " + fileName, 
         new FileInputStream(fileToBeSend));

         int ch;
         do 
         {
             ch = input.read();
            exitStream.writeUTF(String.valueOf(ch)); 
         } while(ch != -1);

         input.close();
Ariel Grabijas
  • 1,472
  • 5
  • 25
  • 45

2 Answers2

1

For ProgressMonitorInputStream to work , You need to read very big file. It is specified in its document that:

This creates a progress monitor to monitor the progress of reading the input stream. If it's taking a while, a ProgressDialog will be popped up to inform the user. If the user hits the Cancel button an InterruptedIOException will be thrown on the next read. All the right cleanup is done when the stream is closed.

Here is the example. Make sure that you input a file(bigFile.txt) contains lot of stuffs to read.

enter image description here

import java.awt.Color;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import javax.swing.JLabel;
import javax.swing.ProgressMonitorInputStream;

public class ProgressMonitorInputStreamDemo {

  public static void main(String args[]) throws Exception {
  String file = "bigFile.txt";
  FileInputStream fis = new FileInputStream(file);
  JLabel filenameLabel = new JLabel(file, JLabel.RIGHT);
  filenameLabel.setForeground(Color.black);
  Object message[] = { "Reading:", filenameLabel };
  ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(null, message, fis);
  InputStreamReader isr = new InputStreamReader(pmis);
  BufferedReader br = new BufferedReader(isr);
  String line;
  while ((line = br.readLine()) != null) {
    System.out.println(line);
  }
  br.close();
  }
}

Note: If you want to see ProgressBar , no matter how small file you are reading , You can use SwingWorker . Have a look at this Post.

Community
  • 1
  • 1
Vishal K
  • 12,976
  • 2
  • 27
  • 38
  • I can find this kind of tutorial by myself. I am asking for solution for this particular code that I've quoted in main post. Because for this moment it is a problem for me to do it by myself. – Ariel Grabijas Apr 09 '13 at 19:09
  • Look at the Post that I have written in the **Note** section.. And yes of course you can find tutorials..But I expect that after going through those tutorials and few Tips you would easily solve your problem... – Vishal K Apr 09 '13 at 19:50
  • Yes I understand that Note section. But i don't really need it for small files. So I took file big enough for this, and after that window appears. But this window is empty. I've tried 2 codes, yours with additional streams, and the one from @hyde. – Ariel Grabijas Apr 09 '13 at 19:57
  • Then post your updated code .. – Vishal K Apr 09 '13 at 19:59
  • I've posted the one from @hyde - because for now it's simpler and more intuitive for me. – Ariel Grabijas Apr 09 '13 at 20:07
  • I don't see any problem with your code as it is working fine at my end ..Except for `exitStream` I am using `SOP`. What is the size of file that you are reading? – Vishal K Apr 09 '13 at 20:21
  • File got about 400MB. Second file had 2GB. exitStream is: `socket = new Socket(ip, port); exitStream = new DataOutputStream(socket.getOutputStream());` – Ariel Grabijas Apr 09 '13 at 20:34
1

Do you simply mean, how to use nested streams, something like this?

    ProgressMonitorInputStream input = new ProgressMonitorInputStream(
         null, 
         "Reading: " + file, 
         new FileInputStream(file));

    ProgressMonitor monitor = input.getProgressMonitor();
    // do some configuration for monitor here

    int ch;
    do {
        ch = input.read();
        // note: writing also the last -1 value
        exitStream.writeUTF(String.valueOf(ch)); 
    } while(ch != -1);

    input.close();
hyde
  • 60,639
  • 21
  • 115
  • 176
  • @Noran I have no idea what `exitStream` is, I just copy-pasted and modified the code in your question... – hyde Apr 09 '13 at 19:41
  • The "Progress" window appears, but its empty. There is no progress bar inside of it.PS: Yea sorry. I am tired and `exitStream` is a translated name from my lenguage. – Ariel Grabijas Apr 09 '13 at 19:44
  • The same happens when I use the code from @Vishal K . Window appear but its empty. It's just a frame. – Ariel Grabijas Apr 09 '13 at 19:48
  • @Noran Note sure what the problem could be, but I'd add some other GUI code just to see that works (like google java gui hello world). Then I would try configuring the progress monitor, edited the code in my answer to have template for that. – hyde Apr 09 '13 at 19:58
  • Check out my update, maybe there is some small thing that I can't see. After tons of hours with code its quite possible. – Ariel Grabijas Apr 09 '13 at 20:04