?? binaryfile.java
字號:
// **********************************************************************// // <copyright>// // BBN Technologies// 10 Moulton Street// Cambridge, MA 02138// (617) 873-8000// // Copyright (C) BBNT Solutions LLC. All rights reserved.// // </copyright>// **********************************************************************// // $Source: /cvs/distapps/openmap/src/openmap/com/bbn/openmap/io/BinaryFile.java,v $// $RCSfile: BinaryFile.java,v $// $Revision: 1.4.2.4 $// $Date: 2005/09/13 14:28:48 $// $Author: dietrick $// // **********************************************************************package com.bbn.openmap.io;import java.io.*;import java.net.*;import java.util.Vector;import java.lang.ref.WeakReference;import com.bbn.openmap.Environment;import com.bbn.openmap.MoreMath;import com.bbn.openmap.util.Debug;/** * The BinaryFile is the standard object used to access data files. It * acts like a RandomAccessFile, but will work on jar file contents * and URLs, too. The source of the data is isolated through the * InputReader interface. */public class BinaryFile { private static int openCount = 0; private static int classCount = 0; private InputReader inputReader = null; /** * The byte order of the underlying file. (<code>true</code>== * MSB-First == big-endian) */ protected boolean MSBFirst = false; /** * Constructs a new BinaryFile with the specified file as the * input. The default byte-order is LSB first. Reads start at the * first byte of the file. * * @param f the file to be opened for reading * @exception IOException pass-through errors from opening a * RandomAccessFile with f * @see java.io.RandomAccessFile */ public BinaryFile(File f) throws IOException { inputReader = new FileInputReader(f); classCount++; openCount++; } /** * Constructs a new BinaryFile with the specified file as the * input. The byte-order is undefined. Reads start at the first * byte of the file. This constructor looks for the file with the * string given, and will call the correct constructor as * appropriate. If the string represents a file available locally, * then the BinaryFile will be accessed with a FileInputReader * using a RandomAccessFile. If it's only available as a resource, * then a StreamInputReader will be used. The name should be a * path to a file, or the name of a resource that can be found in * the classpath, or a URL. * * @param name the name of the file to be opened for reading * @exception IOException pass-through errors from opening the * file. */ public BinaryFile(String name) throws IOException { boolean showDebug = false; if (Debug.debugging("binaryfile")) { showDebug = true; } if (showDebug) { Debug.output("BinaryFile: trying to figure out how to handle " + name); } try { File file = null; URL url = null; if (!Environment.isApplet()) { file = new File(name); } if (file != null && file.exists()) { // If the string represents a file, then we want to // use the RandomAccessFile aspect of the BinaryFile. setInputReader(new FileInputReader(file)); } else { // url = ClassLoader.getSystemResource(name); url = Thread.currentThread() .getContextClassLoader() .getResource(name); // OK, now we want to look around for the file, in the // classpaths, and as a resource. It may be a file in // a classpath, available for direct access. if (url != null) { String newname = url.getFile(); if (showDebug) { Debug.output("BinaryFile: looking for " + newname); } if (!Environment.isApplet()) { file = new File(newname); } if (file != null && file.exists()) { // It's still a file, available directly. // Access it with the RandomAccessFile setInputReader(new FileInputReader(file)); } else { // Need to get it as a resource. Needs // special handling if it's coming in a jar // file. Jar file references have a "!" in // them if (!setJarInputReader(newname)) { if (showDebug) { Debug.output(" trying as url: " + url); } setInputReader(new URLInputReader(url)); } } } else if (Environment.isApplet()) { if (showDebug) { Debug.output(" As applet, checking codebase..."); } // Look in the codebase for applets... URL[] cba = new URL[1]; cba[0] = Environment.getApplet().getCodeBase(); URLClassLoader ucl = URLClassLoader.newInstance(cba); url = ucl.getResource(name); if (url != null) { setInputReader(new URLInputReader(url)); } } // It's not in the classpath, so try it as a URL. if (inputReader == null) { if (showDebug) { Debug.output(" lastly, trying as URL: " + name); } try { setInputReader(new URLInputReader(new URL(name))); } catch (java.security.AccessControlException ace) { Debug.output("BinaryFile: " + name + " couldn't be accessed."); throw new IOException("AccessControlException trying to fetch " + name + " as a URL"); } } } if (inputReader == null) { throw new FileNotFoundException("BinaryFile can't find: " + name); } classCount++; openCount++; } catch (IOException ioe) { throw ioe; } } /** * Takes a name of a file, and checks to see if it reflects an * entry in a jar file. (Check the filename and see if it looks * like "jarfile!jarfileentry".) If it is, it separates the path, * and set the inputReader to a JarInputReader and returns true. * If not, it returns false. */ protected boolean setJarInputReader(String name) throws IOException { try { int index = name.indexOf("!"); if (index != -1) { // Used to be this, modified by Erik Sanders to work // with jdk 1.4 plugin // String jarFileName = // name.substring(name.indexOf(":") + 1, index); // changed to this... String jarFileName; if (name.startsWith("file:")) { // java-plugin 1.3 returns local file: strip file: // from string jarFileName = name.substring(name.indexOf(":") + 1, index); } else { // java-plugin 1.4 returns reference to server, so // leave http:// part // Used to start the substring from 1, but changed // to 0 thanks to DGK jarFileName = name.substring(0, index); } // skip !/ " String jarEntryName = name.substring(index + 2); if (Debug.debugging("binaryfile")) { Debug.output(" got: \n" + jarFileName + "\n" + jarEntryName); } // If the jar doesn't exist, should return something // that indicates this. Should check the performance // impllications of this call, though, at some point. // DGK added File f = new File(jarFileName); if (f.exists() == false) { return false; } setInputReader(new JarInputReader(jarFileName, jarEntryName)); return true; } } catch (java.security.AccessControlException ace) { if (Debug.debugging("binaryfile")) { Debug.output("BinaryFile.setJarInputFile: AccessControlException for " + name); } } return false; } /** * A simple test method to determine if a file or directory, * represented by a string, can be found by the current Java * environment. Uses the same tests as BinaryFile constructor for * tracking down a file. * * @param name A path to a file, a URL, or a path to a jar file * entry. */ public static boolean exists(String name) { boolean exists = false; try { File file = null; URL url = null; if (!Environment.isApplet()) { file = new File(name); } if (file != null && file.exists()) { exists = true; } else { // url = ClassLoader.getSystemResource(name); url = Thread.currentThread() .getContextClassLoader() .getResource(name); // OK, now we want to look around for the file, in the // classpaths, and as a resource. It may be a file in // a classpath, available for direct access. if (url != null) { exists = true; } else if (Environment.isApplet()) { if (Debug.debugging("binaryfile")) { Debug.output(" As applet, checking codebase..."); } // Look in the codebase for applets... URL[] cba = new URL[1]; cba[0] = Environment.getApplet().getCodeBase(); URLClassLoader ucl = URLClassLoader.newInstance(cba); if (ucl.getResource(name) != null) { exists = true; // This has been commented out because the // AppletDataNugget has been deprecated, and // is not needed. // } else { // url = AppletDataNugget.findResource(name); // if (url != null) { // exists = true; // } } } // It's not in the classpath, so try it as a URL to a // webserver. if (!exists && name.indexOf("http:") != -1) { try { InputStream stream = new URL(name).openStream(); stream.close(); exists = true; } catch (java.security.AccessControlException ace) { exists = false; } } } } catch (IOException ioe) { Debug.message("binaryfile", "BinaryFile.exists() caught IOException"); exists = false; } if (Debug.debugging("binaryfile")) { Debug.output("BinaryFile.exists(" + name + ") = " + exists); } return exists; } /** * Get the source name from the input reader. */ public String getName() { if (inputReader != null) { return inputReader.getName(); } return null; } /** * Get the inputReader used for accessing the file, for quering * purposes. Don't use it to get data, or the file pointers may * get messed up. */ public InputReader getInputReader() { return inputReader; } /** * Set the input reader used by the BinaryFile. Make sure it's * intialized properly. */ public void setInputReader(InputReader reader) { if (Debug.debugging("binaryfile")) { Debug.output("Setting inputReader"); } inputReader = reader; } /**
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -