Changeset 5103


Ignore:
Timestamp:
05/12/09 16:07:07 (11 years ago)
Author:
melissa
Message:

Minor cleanup in common component and improvements to URLHandle. Closes #257.

Location:
trunk/components/common/src/loci/common
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/components/common/src/loci/common/BZip2Handle.java

    r5093 r5103  
    4343public class BZip2Handle extends CompressedRandomAccess { 
    4444 
    45   // -- Fields -- 
    46  
    47   private String file; 
    48  
    4945  // -- Constructor -- 
    5046 
     
    5652    } 
    5753 
    58     BufferedInputStream bis = 
    59       new BufferedInputStream(new FileInputStream(file), MAX_OVERHEAD); 
    60     bis.skip(2); 
    61     stream = new DataInputStream(new CBZip2InputStream(bis)); 
     54    resetStream(); 
    6255 
    6356    length = 0; 
     
    6861    } 
    6962 
    70     bis = new BufferedInputStream(new FileInputStream(file), MAX_OVERHEAD); 
    71     bis.skip(2); 
    72     stream = new DataInputStream(new CBZip2InputStream(bis)); 
     63    resetStream(); 
    7364  } 
    7465 
     
    9283    else if (fp < oldFP) { 
    9384      stream.close(); 
    94  
    95       BufferedInputStream bis = 
    96         new BufferedInputStream(new FileInputStream(file), MAX_OVERHEAD); 
    97       bis.skip(2); 
    98       stream = new DataInputStream(new CBZip2InputStream(bis)); 
    99  
     85      resetStream(); 
    10086      stream.skipBytes((int) fp); 
    10187    } 
    10288  } 
    10389 
     90  // -- Helper methods -- 
     91 
     92  private void resetStream() throws IOException { 
     93    BufferedInputStream bis = new BufferedInputStream( 
     94      new FileInputStream(file), RandomAccessInputStream.MAX_OVERHEAD); 
     95    bis.skip(2); 
     96    stream = new DataInputStream(new CBZip2InputStream(bis)); 
     97  } 
     98 
    10499} 
  • trunk/components/common/src/loci/common/ByteArrayHandle.java

    r5093 r5103  
    162162  /* @see java.io.DataInput.readDouble() */ 
    163163  public double readDouble() throws IOException { 
    164     if (fp + 8 > length()) throw new EOFException(); 
    165     double d = Double.longBitsToDouble(DataTools.bytesToLong(array, fp, false)); 
    166     fp += 8; 
    167     return d; 
     164    return Double.longBitsToDouble(readLong()); 
    168165  } 
    169166 
    170167  /* @see java.io.DataInput.readFloat() */ 
    171168  public float readFloat() throws IOException { 
    172     if (fp + 4 > length()) throw new EOFException(); 
    173     float f = Float.intBitsToFloat(DataTools.bytesToInt(array, fp, false)); 
    174     fp += 4; 
    175     return f; 
     169    return Float.intBitsToFloat(readInt()); 
    176170  } 
    177171 
     
    238232  /* @see java.io.DataInput.skipBytes(int) */ 
    239233  public int skipBytes(int n) { 
    240     if (n < 0) n = 0; 
     234    if (n < 0) return 0; 
    241235    if (fp + n > length()) n = (int) (length() - fp); 
    242236    fp += n; 
     
    285279  public void writeChar(int v) throws IOException { 
    286280    if (fp + 2 > length()) setLength(fp + 2); 
    287     array[fp++] = (byte) (0xff & (v >> 8)); 
    288     array[fp++] = (byte) (0xff & v); 
     281    DataTools.unpackShort((short) v, array, fp, false); 
     282    fp += 2; 
    289283  } 
    290284 
     
    295289    char[] c = s.toCharArray(); 
    296290    for (int i=0; i<c.length; i++) { 
    297       char v = c[i]; 
    298       array[fp++] = (byte) (0xff & (v >> 8)); 
    299       array[fp++] = (byte) (0xff & v); 
     291      DataTools.unpackShort((short) c[i], array, fp, false); 
     292      fp += 2; 
    300293    } 
    301294  } 
     
    314307  public void writeInt(int v) throws IOException { 
    315308    if (fp + 4 > length()) setLength(fp + 4); 
    316     for (int i=0; i<4; i++) { 
    317       array[fp++] = (byte) (0xff & (v >> ((3 - i) * 8))); 
    318     } 
     309    DataTools.unpackBytes(v, array, fp, 4, false); 
     310    fp += 4; 
    319311  } 
    320312 
     
    322314  public void writeLong(long v) throws IOException { 
    323315    if (fp + 8 > length()) setLength(fp + 8); 
    324     for (int i=0; i<8; i++) { 
    325       array[fp++] = (byte) (0xff & (v >> ((7 - i) * 8))); 
    326     } 
     316    DataTools.unpackBytes(v, array, fp, 8, false); 
     317    fp += 8; 
    327318  } 
    328319 
     
    330321  public void writeShort(int v) throws IOException { 
    331322    if (fp + 2 > length()) setLength(fp + 2); 
    332     array[fp++] = (byte) (0xff & (v >> 8)); 
    333     array[fp++] = (byte) (0xff & v); 
     323    DataTools.unpackShort((short) v, array, fp, false); 
     324    fp += 2; 
    334325  } 
    335326 
  • trunk/components/common/src/loci/common/CompressedRandomAccess.java

    r5093 r5103  
    4141public abstract class CompressedRandomAccess implements IRandomAccess { 
    4242 
    43   // -- Constants -- 
    44  
    45   protected static final int MAX_OVERHEAD = 1048576; 
    46  
    4743  // -- Fields -- 
    4844 
     45  protected String file; 
    4946  protected DataInputStream stream; 
    5047  protected long length; 
  • trunk/components/common/src/loci/common/DataTools.java

    r5093 r5103  
    553553      float[] f = new float[b.length / 4]; 
    554554      for (int i=0; i<f.length; i++) { 
    555         f[i] = Float.intBitsToFloat(bytesToInt(b, i*4, 4, little)); 
     555        f[i] = bytesToFloat(b, i * 4, 4, little); 
    556556      } 
    557557      return f; 
     
    567567      double[] d = new double[b.length / 8]; 
    568568      for (int i=0; i<d.length; i++) { 
    569         d[i] = Double.longBitsToDouble(bytesToLong(b, i*8, 8, little)); 
     569        d[i] = bytesToDouble(b, i * 8, 8, little); 
    570570      } 
    571571      return d; 
     
    622622  /** Remove null bytes from a string. */ 
    623623  public static String stripString(String toStrip) { 
    624     char[] toRtn = new char[toStrip.length()]; 
    625     int counter = 0; 
    626     for (int i=0; i<toRtn.length; i++) { 
     624    StringBuffer s = new StringBuffer(); 
     625    for (int i=0; i<toStrip.length(); i++) { 
    627626      if (toStrip.charAt(i) != 0) { 
    628         toRtn[counter] = toStrip.charAt(i); 
    629         counter++; 
    630       } 
    631     } 
    632     toStrip = new String(toRtn); 
    633     toStrip = toStrip.trim(); 
    634     return toStrip; 
     627        s.append(toStrip.charAt(i)); 
     628      } 
     629    } 
     630    return s.toString().trim(); 
    635631  } 
    636632 
     
    645641    int slash2 = s2.lastIndexOf(File.pathSeparator); 
    646642 
    647     String sub1 = s1.substring((slash1 == -1) ? 0 : slash1 + 1, n1); 
    648     String sub2 = s2.substring((slash2 == -1) ? 0 : slash2 + 1, n2); 
     643    String sub1 = s1.substring(slash1 + 1, n1); 
     644    String sub2 = s2.substring(slash2 + 1, n2); 
    649645    return sub1.equals(sub2) || sub1.startsWith(sub2) || sub2.startsWith(sub1); 
    650646  } 
     
    675671 
    676672  /** 
    677    * Load a list of metadata tags and their corresponding descriptions. 
    678    */ 
    679   public static Hashtable getMetadataTags(String baseClass, String file) { 
    680     try { 
    681       return getMetadataTags(Class.forName(baseClass), file); 
    682     } 
    683     catch (ClassNotFoundException e) { } 
    684     return null; 
    685   } 
    686  
    687   /** 
    688    * Load a list of metadata tags and their corresponding descriptions. 
    689    */ 
    690   public static Hashtable getMetadataTags(Class c, String file) { 
    691     Hashtable<String, String> h = new Hashtable<String, String>(); 
    692     BufferedReader in = new BufferedReader(new InputStreamReader( 
    693       c.getResourceAsStream(file))); 
    694     String line = null, key = null, value = null; 
    695     while (true) { 
    696       try { 
    697         line = in.readLine(); 
    698       } 
    699       catch (IOException e) { 
    700         line = null; 
    701       } 
    702       if (line == null) break; 
    703       key = line.substring(0, line.indexOf("=>")).trim(); 
    704       value = line.substring(line.indexOf("=>") + 2).trim(); 
    705       h.put(key, value); 
    706     } 
    707     return h; 
    708   } 
    709  
    710   /** 
    711673   * Normalize the given float array so that the minimum value maps to 0.0 
    712674   * and the maximum value maps to 1.0. 
     
    719681    float min = Float.MAX_VALUE; 
    720682    float max = Float.MIN_VALUE; 
    721  
    722     for (int i=0; i<data.length; i++) { 
    723       if (data[i] < min) min = data[i]; 
    724       if (data[i] > max) { 
    725         max = data[i]; 
    726       } 
    727     } 
    728  
    729     // now normalize; min => 0.0, max => 1.0 
    730  
    731     for (int i=0; i<rtn.length; i++) { 
    732       rtn[i] = (data[i] - min) / (max - min); 
    733     } 
    734  
    735     return rtn; 
    736   } 
    737  
    738   /** 
    739    * Normalize the given double array so that the minimum value maps to 0.0 
    740    * and the maximum value maps to 1.0. 
    741    */ 
    742   public static double[] normalizeDoubles(double[] data) { 
    743     double[] rtn = new double[data.length]; 
    744  
    745     double min = Double.MAX_VALUE; 
    746     double max = Double.MIN_VALUE; 
     683    float range = max - min; 
    747684 
    748685    for (int i=0; i<data.length; i++) { 
     
    751688    } 
    752689 
     690    // now normalize; min => 0.0, max => 1.0 
     691 
    753692    for (int i=0; i<rtn.length; i++) { 
    754       rtn[i] = (data[i] - min) / (max - min); 
     693      rtn[i] = (data[i] - min) / range; 
     694    } 
     695 
     696    return rtn; 
     697  } 
     698 
     699  /** 
     700   * Normalize the given double array so that the minimum value maps to 0.0 
     701   * and the maximum value maps to 1.0. 
     702   */ 
     703  public static double[] normalizeDoubles(double[] data) { 
     704    double[] rtn = new double[data.length]; 
     705 
     706    double min = Double.MAX_VALUE; 
     707    double max = Double.MIN_VALUE; 
     708    double range = max - min; 
     709 
     710    for (int i=0; i<data.length; i++) { 
     711      if (data[i] < min) min = data[i]; 
     712      if (data[i] > max) max = data[i]; 
     713    } 
     714 
     715    for (int i=0; i<rtn.length; i++) { 
     716      rtn[i] = (data[i] - min) / range; 
    755717    } 
    756718    return rtn; 
     
    785747    throws IOException 
    786748  { 
    787     try { 
    788       SAXParser parser = SAX_FACTORY.newSAXParser(); 
    789       parser.parse(new ByteArrayInputStream(xml), handler); 
    790     } 
    791     catch (ParserConfigurationException exc) { 
    792       IOException e = new IOException(); 
    793       e.initCause(exc); 
    794       throw e; 
    795     } 
    796     catch (SAXException exc) { 
    797       IOException e = new IOException(); 
    798       e.initCause(exc); 
    799       throw e; 
    800     } 
     749    parseXML(new RandomAccessInputStream(xml), handler); 
    801750  } 
    802751 
  • trunk/components/common/src/loci/common/GZipHandle.java

    r5093 r5103  
    4444public class GZipHandle extends CompressedRandomAccess { 
    4545 
    46   // -- Fields -- 
    47  
    48   private String file; 
    49  
    5046  // -- Constructor -- 
    5147 
     
    5753    } 
    5854 
    59     BufferedInputStream bis = new BufferedInputStream( 
    60       new FileInputStream(file), MAX_OVERHEAD); 
    61     stream = new DataInputStream(new GZIPInputStream(bis)); 
     55    resetStream(); 
    6256 
    6357    length = 0; 
     
    6862    } 
    6963 
    70     bis = new BufferedInputStream(new FileInputStream(file), MAX_OVERHEAD); 
    71     stream = new DataInputStream(new GZIPInputStream(bis)); 
     64    resetStream(); 
    7265  } 
    7366 
     
    8376  /* @see IRandomAccess#seek(long) */ 
    8477  public void seek(long pos) throws IOException { 
    85     long oldFP = fp; 
     78    long diff = fp - pos; 
    8679    fp = pos; 
    87     if (fp > oldFP) { 
    88       long diff = fp - oldFP; 
     80    if (diff >= 0) { 
    8981      stream.skipBytes((int) diff); 
    9082    } 
    91     else if (fp < oldFP) { 
    92       stream.close(); 
    93       BufferedInputStream bis = new BufferedInputStream( 
    94         new FileInputStream(file), MAX_OVERHEAD); 
    95       stream = new DataInputStream(new GZIPInputStream(bis)); 
     83    else { 
     84      resetStream(); 
    9685      stream.skipBytes((int) fp); 
    9786    } 
    9887  } 
    9988 
     89  // -- Helper methods -- 
     90 
     91  private void resetStream() throws IOException { 
     92    if (stream != null) stream.close(); 
     93    BufferedInputStream bis = new BufferedInputStream( 
     94      new FileInputStream(file), RandomAccessInputStream.MAX_OVERHEAD); 
     95    stream = new DataInputStream(new GZIPInputStream(bis)); 
     96  } 
     97 
    10098} 
  • trunk/components/common/src/loci/common/Location.java

    r5093 r5103  
    161161    IRandomAccess handle = null; 
    162162    if (id.startsWith("http://")) { 
    163       handle = new URLHandle(getMappedId(id), writable ? "rw" : "r"); 
     163      handle = new URLHandle(getMappedId(id), writable ? "w" : "r"); 
    164164    } 
    165165    else if (ZipHandle.isZipFile(id)) { 
     
    329329        while (!foundEnd) { 
    330330          byte[] b = new byte[is.available()]; 
     331          is.read(b); 
    331332          String s = new String(b); 
    332333          if (s.toLowerCase().indexOf("</html>") != -1) foundEnd = true; 
  • trunk/components/common/src/loci/common/RandomAccessInputStream.java

    r5093 r5103  
    110110   */ 
    111111  public RandomAccessInputStream(String file) throws IOException { 
    112     String path = Location.getMappedId(file); 
    113     File f = new File(path).getAbsoluteFile(); 
    114     raf = Location.getHandle(file); 
    115     length = raf.length(); 
    116  
    117     if (raf == null) { 
    118       throw new IOException("File not found: " + file); 
    119     } 
    120  
    121     if (f.exists()) { 
    122       compressed = raf instanceof CompressedRandomAccess; 
    123       if (compressed) { 
    124         BufferedInputStream bis = new BufferedInputStream( 
    125           new FileInputStream(path), MAX_OVERHEAD); 
    126         dis = new DataInputStream(bis); 
    127  
    128         buf = new byte[(int) (length < MAX_OVERHEAD ? length : MAX_OVERHEAD)]; 
    129         raf.readFully(buf); 
    130         raf.seek(0); 
    131         nextMark = MAX_OVERHEAD; 
    132       } 
    133     } 
    134112    this.file = file; 
     113    reopen(); 
    135114    fp = 0; 
    136115    afp = 0; 
    137     fileCache.put(this, Boolean.TRUE); 
    138     openFiles++; 
    139     if (openFiles > MAX_FILES) cleanCache(); 
    140116  } 
    141117 
     
    197173    buf = null; 
    198174    if (Boolean.TRUE.equals(fileCache.get(this))) { 
    199       fileCache.put(this, Boolean.FALSE); 
     175      fileCache.put(this, false); 
    200176      openFiles--; 
    201177    } 
     
    489465      } 
    490466 
    491       if (fp >= nextMark) { 
    492         dis.mark(MAX_OVERHEAD); 
    493       } 
    494       nextMark = fp + MAX_OVERHEAD; 
    495       mark = fp; 
    496  
     467      resetMark(); 
    497468      return DIS; 
    498469    } 
     
    521492          } 
    522493 
    523           if (fp >= nextMark) { 
    524             dis.mark(MAX_OVERHEAD); 
    525           } 
    526           nextMark = fp + MAX_OVERHEAD; 
    527           mark = fp; 
    528  
     494          resetMark(); 
    529495          return DIS; 
    530496        } 
    531         else { 
    532           raf.seek(afp); 
    533           return RAF; 
    534         } 
    535       } 
    536       else { 
    537         // we don't want this to happen very often 
    538         raf.seek(afp); 
    539         return RAF; 
    540       } 
    541     } 
     497      } 
     498    } 
     499    // we don't want this to happen very often 
     500    raf.seek(afp); 
     501    return RAF; 
     502  } 
     503 
     504  private void resetMark() { 
     505    if (fp >= nextMark) { 
     506      dis.mark(MAX_OVERHEAD); 
     507    } 
     508    nextMark = fp + MAX_OVERHEAD; 
     509    mark = fp; 
    542510  } 
    543511 
     
    552520    length = raf.length(); 
    553521 
     522    if (raf == null) { 
     523      throw new IOException("File not found: " + file); 
     524    } 
     525 
    554526    if (f.exists()) { 
    555       BufferedInputStream bis = new BufferedInputStream( 
    556         new FileInputStream(Location.getMappedId(file)), MAX_OVERHEAD); 
    557  
    558       path = path.toLowerCase(); 
    559  
    560       if (dis != null) dis.close(); 
    561  
    562527      compressed = raf instanceof CompressedRandomAccess; 
    563  
    564       if (!compressed) { 
     528      if (compressed) { 
     529        BufferedInputStream bis = new BufferedInputStream( 
     530          new FileInputStream(path), MAX_OVERHEAD); 
     531 
     532        if (dis != null) dis.close(); 
    565533        dis = new DataInputStream(bis); 
    566534        buf = new byte[(int) (length < MAX_OVERHEAD ? length : MAX_OVERHEAD)]; 
    567535        raf.readFully(buf); 
    568536        raf.seek(0); 
    569       } 
    570     } 
    571     fileCache.put(this, Boolean.TRUE); 
     537        nextMark = MAX_OVERHEAD; 
     538      } 
     539    } 
     540    fileCache.put(this, true); 
    572541    openFiles++; 
    573542    if (openFiles > MAX_FILES) cleanCache(); 
     
    577546  private void cleanCache() { 
    578547    int toClose = MAX_FILES - 10; 
    579     RandomAccessInputStream[] files = (RandomAccessInputStream[]) 
     548    RandomAccessInputStream[] files = 
    580549      fileCache.keySet().toArray(new RandomAccessInputStream[0]); 
    581550    int closed = 0; 
     
    583552 
    584553    while (closed < toClose) { 
    585       if (!this.equals(files[ndx]) && 
    586         !fileCache.get(files[ndx]).equals(Boolean.FALSE) && 
    587         files[ndx].file != null) 
     554      if (!this.equals(files[ndx]) && files[ndx].file != null && 
     555        Boolean.TRUE.equals(fileCache.get(files[ndx]))) 
    588556      { 
    589557        try { files[ndx].close(); } 
  • trunk/components/common/src/loci/common/URLHandle.java

    r5093 r5103  
    3333/** 
    3434 * Provides random access to data over HTTP using the IRandomAccess interface. 
    35  * This is slow, but functional. 
    3635 * 
    3736 * <dl><dt><b>Source code:</b></dt> 
     
    6968  private long mark; 
    7069 
     70  private String mode; 
     71 
    7172  // -- Constructors -- 
    7273 
    7374  public URLHandle(String url, String mode) throws IOException { 
     75    this.mode = mode.toLowerCase(); 
    7476    if (!url.startsWith("http")) url = "http://" + url; 
    75     conn = (HttpURLConnection) (new URL(url)).openConnection(); 
    76     if (mode.equals("r")) { 
    77       is = new DataInputStream(new BufferedInputStream( 
    78         conn.getInputStream(), 65536)); 
    79     } 
    80     else if (mode.equals("w")) { 
    81       conn.setDoOutput(true); 
    82       os = new DataOutputStream(conn.getOutputStream()); 
    83     } 
    84     fp = 0; 
    85     length = conn.getContentLength(); 
    86     if (is != null) is.mark((int) length); 
    8777    this.url = url; 
     78    resetStream(); 
    8879  } 
    8980 
     
    136127  public void seek(long pos) throws IOException { 
    137128    if (pos >= fp) { 
    138       skipBytes((int) (pos - fp)); 
     129      skip(pos - fp); 
    139130      return; 
    140131    } 
     
    143134        is.reset(); 
    144135        fp = mark; 
    145         skipBytes((int) (pos - fp)); 
     136        skip(pos - fp); 
    146137        return; 
    147138      } 
     
    150141 
    151142    close(); 
    152     conn = (HttpURLConnection) (new URL(url)).openConnection(); 
    153     conn.setDoOutput(true); 
    154     if (is != null) { 
    155       is = new DataInputStream(new BufferedInputStream( 
    156         conn.getInputStream(), 65536)); 
    157       is.mark((int) length()); 
    158       mark = 0; 
    159     } 
    160     if (os != null) os = new DataOutputStream(conn.getOutputStream()); 
    161     fp = 0; 
    162     skipBytes((int) pos); 
     143    resetStream(); 
     144    skip(pos); 
    163145  } 
    164146 
     
    338320 
    339321  private void markManager() throws IOException { 
    340     if (fp >= mark + 65535) { 
     322    if (fp >= mark + RandomAccessInputStream.MAX_OVERHEAD - 1) { 
    341323      mark = fp; 
    342       is.mark((int) length()); 
    343     } 
    344   } 
     324      is.mark(RandomAccessInputStream.MAX_OVERHEAD); 
     325    } 
     326  } 
     327 
     328  private void resetStream() throws IOException { 
     329    conn = (HttpURLConnection) (new URL(url)).openConnection(); 
     330    if (mode.equals("w")) { 
     331      conn.setDoOutput(true); 
     332      os = new DataOutputStream(conn.getOutputStream()); 
     333    } 
     334    else { 
     335      is = new DataInputStream(new BufferedInputStream( 
     336        conn.getInputStream(), RandomAccessInputStream.MAX_OVERHEAD)); 
     337    } 
     338    fp = 0; 
     339    mark = 0; 
     340    length = conn.getContentLength(); 
     341    if (is != null) is.mark(RandomAccessInputStream.MAX_OVERHEAD); 
     342  } 
     343 
     344  private void skip(long bytes) throws IOException { 
     345    while (bytes >= Integer.MAX_VALUE) { 
     346      bytes -= skipBytes(Integer.MAX_VALUE); 
     347    } 
     348    skipBytes((int) bytes); 
     349  } 
     350 
    345351} 
  • trunk/components/common/src/loci/common/ZipHandle.java

    r5093 r5103  
    5555  public ZipHandle(String file) throws IOException { 
    5656    super(); 
     57    this.file = file; 
    5758    if (!isZipFile(file)) { 
    5859      throw new IOException(file + " is not a Zip file."); 
Note: See TracChangeset for help on using the changeset viewer.