private ProgressMonitorInputStream getMonitorableStream(InputStream stream, String message) {

    final ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(this, message, stream);

    ProgressMonitor progress = pmis.getProgressMonitor();
    progress.setMillisToDecideToPopup(1);
    progress.setMillisToPopup(1);

    return pmis;
  }
Example #2
0
  /**
   * Read word list from file with name WORDLISTFILENAME, and pass a Set containing those words to
   * the computer player to intialize its lexicon.
   */
  private void initLexicon(InputStream stream) {

    ProgressMonitorInputStream pmis;
    ProgressMonitor progress = null;
    pmis = new ProgressMonitorInputStream(this, "reading words", stream);
    progress = pmis.getProgressMonitor();
    progress.setMillisToDecideToPopup(10);
    Scanner s = new Scanner(pmis);
    myLexicon.load(s);
    try {
      pmis.close();
    } catch (IOException e) {
      JOptionPane.showMessageDialog(
          null, "Error Closing Stream", "Error", JOptionPane.ERROR_MESSAGE);
    }
  }
  protected File doRead() {

    int retval = ourChooser.showOpenDialog(null);
    if (retval != JFileChooser.APPROVE_OPTION) {
      return null;
    }
    showMessage("reading/initializing");

    myFile = ourChooser.getSelectedFile();
    ProgressMonitorInputStream temp = null;
    if (myFast) {
      temp = getMonitorableStream(getFastByteReader(myFile), "counting/reading bits ...");
    } else {
      temp = getMonitorableStream(myFile, "counting/reading bits ...");
    }
    final ProgressMonitorInputStream pmis = temp;
    final ProgressMonitor progress = pmis.getProgressMonitor();
    try {

      myFirstFileThread =
          new Thread() {
            public void run() {
              try {
                myFirstReadingDone = false;
                int saved = myModel.preprocessCompress(pmis);
                HuffViewer.this.showMessage("saved: " + saved + " bits");
                myFirstReadingDone = true;
              } catch (IOException e) {
                HuffViewer.this.showError("reading exception\n " + e);
                // e.printStackTrace();
              }
              if (progress.isCanceled()) {
                HuffViewer.this.showError("reading cancelled");
              }
            }
          };
      myFirstFileThread.start();
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    File ret = myFile;
    myFile = null;
    return ret;
  }
  private ProgressMonitorInputStream getMonitorableStream(File file, String message) {
    try {
      FileInputStream stream = new FileInputStream(file);
      if (stream == null) {
        System.out.println("null on " + file.getCanonicalPath());
      }
      final ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(this, message, stream);

      ProgressMonitor progress = pmis.getProgressMonitor();
      progress.setMillisToDecideToPopup(1);
      progress.setMillisToPopup(1);

      return pmis;
    } catch (IOException e) {
      showError("could not open " + file.getName());
      e.printStackTrace();
      return null;
    }
  }
  private void doSave() {
    myFile = doRead();
    if (myFile == null) {
      return;
    }

    String name = myFile.getName();
    showMessage("compressing " + name);
    String newName =
        JOptionPane.showInputDialog(this, "Name of compressed file", name + HUFF_SUFFIX);
    if (newName == null) {
      return;
    }
    String path = null;
    try {
      path = myFile.getCanonicalPath();
    } catch (IOException e) {
      showError("trouble with file canonicalizing");
      return;
    }
    int pos = path.lastIndexOf(name);
    newName = path.substring(0, pos) + newName;
    final File file = new File(newName);
    try {
      final FileOutputStream out = new FileOutputStream(file);
      ProgressMonitorInputStream temp = null;
      if (myFast) {
        temp = getMonitorableStream(getFastByteReader(myFile), "compressing bits...");
      } else {
        temp = getMonitorableStream(myFile, "compressing bits ...");
      }
      final ProgressMonitorInputStream pmis = temp;
      final ProgressMonitor progress = pmis.getProgressMonitor();
      Thread fileWriterThread =
          new Thread() {
            public void run() {
              try {
                while (!myFirstReadingDone) {
                  try {
                    sleep(100);
                  } catch (InterruptedException e) {
                    // what to do?
                    HuffViewer.this.showError("Trouble in Thread " + e);
                  }
                }
                myModel.compress(pmis, out, myForce);
              } catch (IOException e) {
                HuffViewer.this.showError("compression exception\n " + e);
                cleanUp(file);
                // e.printStackTrace();
              }
              if (progress.isCanceled()) {
                HuffViewer.this.showError("compression cancelled");
                cleanUp(file);
              }
            }
          };
      fileWriterThread.start();
    } catch (FileNotFoundException e) {
      showError("could not open " + file.getName());
      e.printStackTrace();
    }
    myFile = null;
  }
  private void doDecode() {
    File file = null;
    showMessage("uncompressing");
    try {
      int retval = ourChooser.showOpenDialog(null);
      if (retval != JFileChooser.APPROVE_OPTION) {
        return;
      }
      file = ourChooser.getSelectedFile();
      String name = file.getName();
      String uname = name;
      if (name.endsWith(HUFF_SUFFIX)) {
        uname = name.substring(0, name.length() - HUFF_SUFFIX.length()) + UNHUFF_SUFFIX;
      } else {
        uname = name + UNHUFF_SUFFIX;
      }
      String newName = JOptionPane.showInputDialog(this, "Name of uncompressed file", uname);
      if (newName == null) {
        return;
      }
      String path = file.getCanonicalPath();

      int pos = path.lastIndexOf(name);
      newName = path.substring(0, pos) + newName;
      final File newFile = new File(newName);
      ProgressMonitorInputStream temp = null;
      if (myFast) {
        temp = getMonitorableStream(getFastByteReader(file), "uncompressing bits ...");
      } else {
        temp = getMonitorableStream(file, "uncompressing bits...");
      }
      final ProgressMonitorInputStream stream = temp;

      final ProgressMonitor progress = stream.getProgressMonitor();
      final OutputStream out = new FileOutputStream(newFile);
      Thread fileReaderThread =
          new Thread() {
            public void run() {
              try {
                myModel.uncompress(stream, out);
              } catch (IOException e) {

                cleanUp(newFile);
                HuffViewer.this.showError("could not uncompress\n " + e);
                // e.printStackTrace();
              }
              if (progress.isCanceled()) {
                cleanUp(newFile);
                HuffViewer.this.showError("reading cancelled");
              }
            }
          };
      fileReaderThread.start();
    } catch (FileNotFoundException e) {
      showError("could not open " + file.getName());
      e.printStackTrace();
    } catch (IOException e) {
      showError("IOException, uncompression halted from viewer");
      e.printStackTrace();
    }
  }