[kepler-dev] Re: kepler/src/util URLToLocalFile.java

Christopher Brooks cxh at eecs.berkeley.edu
Thu Oct 28 08:22:43 PDT 2004


Cool actor!

I'm not sure if you are aware of a similar method binaryCopyURLToFile() in
ptII/ptolemy/util/FileUtilities.java

    /** Copy sourceURL to destinationFile without doing any byte conversion.
     *  @param sourceURL The source URL
     *  @param destinationFile The destination File.
     *  @return true if the file was copied, false if the file was not
     *  copied because the sourceURL and the destinationFile refer to the   
     *  same file
     */
    public static boolean binaryCopyURLToFile(URL sourceURL,
            File destinationFile)
            throws IOException {

        if (sourceURL.sameFile(destinationFile.getCanonicalFile().toURL())) {
            return false;
        }
        BufferedInputStream input = null;
        BufferedOutputStream output = null;
        try {
            input = new BufferedInputStream(sourceURL.openStream());

            output = new BufferedOutputStream(
                    new FileOutputStream(destinationFile));

            int c;
            while (( c = input.read()) != -1) {
                output.write(c);
            }
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (Throwable throwable) {
                    System.out.println("Ignoring failure to close stream "
                            + "on " + sourceURL);
                    throwable.printStackTrace();
                }
            }
            if (output != null) {
                try {
                    output.close();
                } catch (Throwable throwable) {
                    System.out.println("Ignoring failure to close stream "
                            + "on " + destinationFile);
                    throwable.printStackTrace();
                }
            }
        }
        return true;
    }



The crazy finally code was prompted by a tool written by Westly Weimer
that detects problems in Run-Time Error Handling, see
http://chess.eecs.berkeley.edu/publications/talks/04/weimer4-6.pdf

I wrote this method to handle opening pdf files located in jar files
and caching them locally.

_Christopher

--------

    higgins     04/10/27 15:16:19
    
      Added:       src/util URLToLocalFile.java
      Log:
      Actor which streams URL data to the local file system. Useful for reducin
   g installation size by avoiding need to include large data files.
      
      Revision  Changes    Path
      1.1                  kepler/src/util/URLToLocalFile.java
      
      Index: URLToLocalFile.java
      ===================================================================
      /* An actor that reads binary files.
      
      @Copyright (c) 2002-2003 The Regents of the University of California.
      All rights reserved.
      
      Permission is hereby granted, without written agreement and without
      license or royalty fees, to use, copy, modify, and distribute this
      software and its documentation for any purpose, provided that the
      above copyright notice and the following two paragraphs appear in all
      copies of this software.
      
      IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
      FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
      ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
      THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
      SUCH DAMAGE.
      
      THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
      INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
      MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
      PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
      CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
      ENHANCEMENTS, OR MODIFICATIONS.
      
                                                      PT_COPYRIGHT_VERSION 2
                                                      COPYRIGHTENDKEY
      */
      
      package org.ecoinformatics.util;
      
      import ptolemy.actor.TypedIOPort;
      import ptolemy.actor.lib.Source;
      import ptolemy.data.ArrayToken;
      import ptolemy.data.BooleanToken;
      import ptolemy.data.IntToken;
      import ptolemy.data.StringToken;
      import ptolemy.data.UnsignedByteToken;
      import ptolemy.data.Token;
      import ptolemy.data.expr.FileParameter;
      import ptolemy.data.expr.Parameter;
      import ptolemy.data.type.ArrayType;
      import ptolemy.data.type.BaseType;
      import ptolemy.kernel.CompositeEntity;
      import ptolemy.kernel.util.*;
      
      import java.io.BufferedReader;
      import java.io.FileInputStream;
      import java.io.FileOutputStream;
      import java.io.File;
      import java.io.BufferedInputStream;
      import java.io.BufferedOutputStream;
      import java.io.IOException;
      import java.io.InputStream;
      import java.io.InputStreamReader;
      import java.net.URL;
      
      /////////////////////////////////////////////////////////////////////////
   /
      //// URLToLocalFile
      /**
      This actor is a modification of the BinaryFileReader.
      It is designed to read a URL and copy it to the local
      file system. (It can also be used to read a local file and
      then write it to another location.)
      
      @author  Dan Higgins
      @version $Id: URLToLocalFile.java,v 1.1 2004/10/27 22:16:19 higgins Exp $
      @since Ptolemy II 3.0.2
      */
      public class URLToLocalFile extends Source {
      
          /** Construct an actor with the given container and name.
           *  @param container The container.
           *  @param name The name of this actor.
           *  @exception IllegalActionException If the actor cannot be containe
   d
           *   by the proposed container.
           *  @exception NameDuplicationException If the container already has 
   an
           *   actor with this name.
           */
          public URLToLocalFile(CompositeEntity container, String name)
                  throws IllegalActionException, NameDuplicationException {
              super(container, name);
      
              output.setTypeEquals(BaseType.BOOLEAN);
      
      //        endOfFile = new TypedIOPort(this, "endOfFile", false, true);
      //        endOfFile.setTypeEquals(BaseType.BOOLEAN);
      
              fileOrURL = new FileParameter(this, "fileOrURL");
              outputFile = new FileParameter(this, "outputFile");
              
              fileOrURLPort = new TypedIOPort(this, "fileOrURLPort", true, fals
   e);
              fileOrURLPort.setTypeEquals(BaseType.STRING);
      
              outputFilePort = new TypedIOPort(this, "outputFilePort", true, fa
   lse);
              outputFilePort.setTypeEquals(BaseType.STRING);
      
              _attachText("_iconDescription", "<svg>\n"
                      + "<rect x=\"-25\" y=\"-20\" "
                      + "width=\"50\" height=\"40\" "
                      + "style=\"fill:white\"/>\n"
                      + "<polygon points=\"-15,-10 -12,-10 -8,-14 -1,-14 3,-10"
                      + " 15,-10 15,10, -15,10\" "
                      + "style=\"fill:red\"/>\n"
                      + "</svg>\n");
          }
      
          ///////////////////////////////////////////////////////////////////
          ////                     ports and parameters                  ////
      
          /** An output port that produces <i>false</i> until the end of file
           *  is reached, at which point it produces <i>true</i>. The type
           *  is boolean.
           */
          public TypedIOPort endOfFile;
      
          /** The file name or URL from which to read.  This is a string with
           *  any form accepted by FileParameter.
           *  @see FileParameter
           */
          public FileParameter fileOrURL;
      
          /** An input for optionally providing an input file name.
           *  @see FileParameter
           */
          public TypedIOPort fileOrURLPort;
      
          /** The file name to which to write.  This is a string with
           *  any form accepted by FileParameter.
           *  @see FileParameter
           */
          public FileParameter outputFile;
          
          /** An output for optionally providing an output file name.
           *  @see FileParameter
           */
          public TypedIOPort outputFilePort;
      
          ///////////////////////////////////////////////////////////////////
          ////                         public methods                    ////
      
          /** Clone the actor into the specified workspace.
           *  @return A new actor.
           *  @exception CloneNotSupportedException If a derived class contains
           *   an attribute that cannot be cloned.
           */
          public Object clone(Workspace workspace)
                  throws CloneNotSupportedException {
              URLToLocalFile newObject = (URLToLocalFile)super.clone(workspace)
   ;
              newObject.nBytesRead = 0;
              newObject.bytesRead = null;
              newObject._reachedEOF = false;
              newObject._reader = null;
              return newObject;
          }
      
          /** Output the data read in the preinitialize() or in the previous
           *  invocation of postfire(), if there is any.
           *  @exception IllegalActionException If there's no director.
           */
          public void fire() throws IllegalActionException {
              super.fire();
       /*
             if (nBytesRead > 0) {
                  Token _bytes[] = new Token[nBytesRead];
                  for (int i = 0; i < nBytesRead; i++) {
                      _bytes[i] = new UnsignedByteToken(bytesRead[i]);
                  }
                  output.send(0, new ArrayToken(_bytes));
              }
       */ 
            if (_reachedEOF == false) {
              _openAndReadBytes();
            }
            output.send(0, new BooleanToken(true));
          }
      
          /** If this is called after prefire() has been called but before
           *  wrapup() has been called, then close any
           *  open file re-open it, and read the first chunk of bytes
           *  be produced in the next invocation of prefire(). This occurs if
           *  this actor is re-initialized during a run of the model.
           *  @exception IllegalActionException If the file or URL cannot be
           *   opened or read.
           */
          public void initialize() throws IllegalActionException {
              super.initialize();
                  _reader = null;
                  _writer = null;
      //            _openAndReadFirstBytes();
          }
      
          /** Read the next bytes from the file. If there reached EOF,
           *  return false.  Otherwise, return whatever the superclass returns.
           *  @exception IllegalActionException If there is a problem reading
           *   the file.
           */
          public boolean postfire() throws IllegalActionException {
            return super.postfire();
          }
      
          /** Return false if there is no more data available in the file.
           *  Otherwise, return whatever the superclass returns.
           *  @exception IllegalActionException If the superclass throws it.
           */
          public boolean prefire() throws IllegalActionException {
              if (_reachedEOF) return false;
              else return super.prefire();
          }
      
          /** Open the file or URL, and read the first bytes to
           *  be sent out in the fire() method.
           *  This is done in preinitialize() so
           *  that derived classes can extract information from the file
           *  that affects information used in type resolution or scheduling.
           *  @exception IllegalActionException If the file or URL cannot be
           *   opened or read.
           */
          public void preinitialize() throws IllegalActionException {
              _reachedEOF = false;
              super.preinitialize();
          }
      
          /** Close the reader if there is one.
           *  @exception IllegalActionException If an IO error occurs.
           */
          public void wrapup() throws IllegalActionException {
          	fileOrURL.close();
            outputFile.close();
              _reader = null;
              _writer = null;
          }
      
          ///////////////////////////////////////////////////////////////////
          ////                         protected members                 ////
      
          /** number of bytes read. */
          protected int nBytesRead;
      
          /** The current bytes read. */
          protected byte[] bytesRead = new byte[20000];
      
          /** The current reader for the input file. */
          protected InputStream _reader;
      
          /** The current writer for the output file. */
          protected FileOutputStream _writer;
      
          ///////////////////////////////////////////////////////////////////
          ////                         private methods                   ////
      
          /** Open the file and read the first bytes.
           */
          private void _openAndReadBytes() throws IllegalActionException {
          	if (fileOrURLPort.getWidth() > 0) {
          		if (fileOrURLPort.hasToken(0)) {
          			String name = ((StringToken) fileOrURLPort.get(
   0)).stringValue();
          			fileOrURL.setExpression(name);
              	}
          	}
          	if (outputFilePort.getWidth() > 0) {
          		if (outputFilePort.hasToken(0)) {
          			String name = ((StringToken) outputFilePort.get
   (0)).stringValue();
          			outputFile.setExpression(name);
              	}
          	}
          	String name = fileOrURL.stringValue();
            String outname = outputFile.stringValue();
          	// file or url.
          	URL url = fileOrURL.asURL();
            File file = outputFile.asFile();
          	if (url == null) {
          		throw new IllegalActionException(this,
          		"No input url/file name has been specified.");
          	}
          	try {
          		_reader = url.openStream();
              _writer = new FileOutputStream(file);
          	} catch (IOException ex) {
          		throw new IllegalActionException(this, ex,
          		"Cannot open file or URL");
              }
              BufferedInputStream _breader = new BufferedInputStream(_reader);
              BufferedOutputStream _bwriter = new BufferedOutputStream(_writer)
   ;
              _reachedEOF = false;
              try {
                int c;
                while ((c = _breader.read())!=-1) {
                  _bwriter.write(c);
                }
                _bwriter.flush();
                _writer.close();
                _reader.close();
                _reachedEOF = true;
      //            nBytesRead = _reader.read(bytesRead);
              } catch (IOException ex) {
                  throw new IllegalActionException(this, ex,
                          "Preinitialize failed.");
              }
          }
      
          ///////////////////////////////////////////////////////////////////
          ////                         private members                   ////
      
          /** Previous value of fileOrURL parameter. */
          private String _previousFileOrURL;
      
          /** Indicator that we have reached the end of file. */
          private boolean _reachedEOF = false;
          
      }
      
      
      
    _______________________________________________
    kepler-cvs mailing list
    kepler-cvs at ecoinformatics.org
    http://www.ecoinformatics.org/mailman/listinfo/kepler-cvs
--------



More information about the Kepler-dev mailing list