File Source: ZipResourceManager.java

         /* 
    P/P   *  Method: com.dmdirc.util.resourcemanager.ZipResourceManager__static_init
          */
     1  /*
     2   * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
     3   *
     4   * Permission is hereby granted, free of charge, to any person obtaining a copy
     5   * of this software and associated documentation files (the "Software"), to deal
     6   * in the Software without restriction, including without limitation the rights
     7   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   * copies of the Software, and to permit persons to whom the Software is
     9   * furnished to do so, subject to the following conditions:
    10   *
    11   * The above copyright notice and this permission notice shall be included in
    12   * all copies or substantial portions of the Software.
    13   *
    14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   * SOFTWARE.
    21   */
    22  
    23  package com.dmdirc.util.resourcemanager;
    24  
    25  import com.dmdirc.logger.ErrorLevel;
    26  import com.dmdirc.logger.Logger;
    27  
    28  import java.io.BufferedInputStream;
    29  import java.io.IOException;
    30  import java.io.InputStream;
    31  import java.util.ArrayList;
    32  import java.util.Enumeration;
    33  import java.util.HashMap;
    34  import java.util.List;
    35  import java.util.Map;
    36  import java.util.zip.ZipEntry;
    37  import java.util.zip.ZipFile;
    38  
    39  /**
    40   * Provides an easy way to access files inside a zip or jar.
    41   */
    42  public final class ZipResourceManager extends ResourceManager {
    43      
    44      /** Zipfile instance. */
    45      private final ZipFile zipFile;
    46      
    47      /** Entries list. */
    48      private final List<String> entries;
    49      
    50      /**
    51       * Instantiates ZipResourceManager.
    52       *
    53       * @param filename Filename of the zip to load
    54       * @throws IOException Throw when the zip fails to load
    55       */
    56      protected ZipResourceManager(final String filename) throws IOException {
                 /* 
    P/P           *  Method: void com.dmdirc.util.resourcemanager.ZipResourceManager(String)
                  * 
                  *  Presumptions:
                  *    java.util.Enumeration:nextElement(...)@63 != null
                  *    java.util.zip.ZipFile:entries(...)@61 != null
                  * 
                  *  Postconditions:
                  *    this.entries == &amp;new ArrayList(ZipResourceManager#2)
                  *    this.zipFile == &amp;new ZipFile(ZipResourceManager#1)
                  *    new ArrayList(ZipResourceManager#2) num objects == 1
                  *    new ZipFile(ZipResourceManager#1) num objects == 1
                  * 
                  *  Test Vectors:
                  *    java.util.Enumeration:hasMoreElements(...)@62: {0}, {1}
                  */
    57          super();
    58          
    59          this.zipFile = new ZipFile(filename);
    60          entries = new ArrayList<String>();
    61          final Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
    62          while (zipEntries.hasMoreElements()) {
    63              entries.add(zipEntries.nextElement().getName());
    64          }
    65      }
    66      
    67      /**
    68       * Returns an instance of a ZipResourceManager for the specified file.
    69       *
    70       * @param filename Filename of the zip to load
    71       * 
    72       * @return ZipResourceManager instance
    73       * 
    74       * @throws IOException Throw when the zip fails to load
    75       */
    76      public static synchronized ZipResourceManager getInstance(final String filename) throws
    77              IOException {
                 /* 
    P/P           *  Method: ZipResourceManager getInstance(String)
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new ZipResourceManager(getInstance#1)
                  *    new ArrayList(ZipResourceManager#2) num objects == 1
                  *    new ZipFile(ZipResourceManager#1) num objects == 1
                  *    new ZipResourceManager(getInstance#1) num objects == 1
                  *    return_value.entries == &amp;new ArrayList(ZipResourceManager#2)
                  *    return_value.zipFile == &amp;new ZipFile(ZipResourceManager#1)
                  */
    78          return new ZipResourceManager(filename);
    79      }
    80      
    81      /** {@inheritDoc} */
    82      @Override
    83      public boolean resourceExists(final String resource) {
                 /* 
    P/P           *  Method: bool resourceExists(String)
                  * 
                  *  Preconditions:
                  *    this.zipFile != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
    84          final ZipEntry zipEntry = zipFile.getEntry(resource);        
    85          
    86          return zipEntry != null && !zipEntry.isDirectory();
    87      }
    88      
    89      /** {@inheritDoc} */
    90      @Override
    91      public byte[] getResourceBytes(final String resource) {
                 /* 
    P/P           *  Method: byte[] getResourceBytes(String)
                  * 
                  *  Preconditions:
                  *    this.zipFile != null
                  * 
                  *  Presumptions:
                  *    init'ed(com.dmdirc.logger.ErrorLevel.LOW)
                  *    java.util.zip.ZipEntry:getSize(...)@104 >= 0
                  * 
                  *  Postconditions:
                  *    return_value in Addr_Set{&amp;new byte[](getResourceBytes#3),&amp;new byte[](getResourceBytes#6),&amp;new byte[](getResourceBytes#7),&amp;new byte[](getResourceBytes#5),&amp;new byte[](getResourceBytes#2),&amp;new byte[](getResourceBytes#1)}
                  *    new byte[](getResourceBytes#1) num objects <= 1
                  *    new byte[](getResourceBytes#1).length == 0
                  *    new byte[](getResourceBytes#2) num objects <= 1
                  *    new byte[](getResourceBytes#2).length == 0
                  *    new byte[](getResourceBytes#3) num objects <= 1
                  *    new byte[](getResourceBytes#3).length <= 264-1
                  *    new byte[](getResourceBytes#5) num objects <= 1
                  *    new byte[](getResourceBytes#5).length == 0
                  *    new byte[](getResourceBytes#6) num objects <= 1
                  *    ...
                  * 
                  *  Test Vectors:
                  *    java.util.zip.ZipEntry:isDirectory(...)@100: {0}, {1}
                  *    java.util.zip.ZipFile:getEntry(...)@92: Inverse{null}, Addr_Set{null}
                  */
    92          final ZipEntry zipEntry = zipFile.getEntry(resource);
    93          BufferedInputStream inputStream;
    94          
    95          
    96          if (zipEntry == null) {
    97              return new byte[0];
    98          }
    99          
   100          if (zipEntry.isDirectory()) {
   101              return new byte[0];
   102          }
   103          
   104          final byte[] bytes = new byte[(int) zipEntry.getSize()];
   105          
   106          try {
   107              inputStream =
   108                      new BufferedInputStream(zipFile.getInputStream(zipEntry));
   109          } catch (IOException ex) {
   110              return new byte[0];
   111          }
   112          
   113          try {
   114              if (inputStream.read(bytes) != bytes.length) {
   115                  inputStream.close();
   116                  return new byte[0];
   117              }
   118          } catch (IOException ex) {
   119              return new byte[0];
   120          }
   121          
   122          try {
   123              inputStream.close();
   124          } catch (IOException ex) {
   125              Logger.userError(ErrorLevel.LOW, "Unable to close stream");
   126          }
   127          
   128          return bytes;
   129      }
   130      
   131      /** {@inheritDoc} */
   132      @Override
   133      public InputStream getResourceInputStream(final String resource) {
                 /* 
    P/P           *  Method: InputStream getResourceInputStream(String)
                  * 
                  *  Preconditions:
                  *    this.zipFile != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    java.util.zip.ZipFile:getEntry(...)@134: Inverse{null}, Addr_Set{null}
                  */
   134          final ZipEntry zipEntry = zipFile.getEntry(resource);
   135          
   136          if (zipEntry == null) {
   137              return null;
   138          }
   139          
   140          try {
   141              return zipFile.getInputStream(zipEntry);
   142          } catch (IOException ex) {
   143              return null;
   144          }
   145          
   146      }
   147      
   148      /** {@inheritDoc} */
   149      @Override
   150      public Map<String, byte[]> getResourcesEndingWithAsBytes(
   151              final String resourcesSuffix) {
                 /* 
    P/P           *  Method: Map getResourcesEndingWithAsBytes(String)
                  * 
                  *  Preconditions:
                  *    this.entries != null
                  *    (soft) this.zipFile != null
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@154 != null
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new HashMap(getResourcesEndingWithAsBytes#1)
                  *    new HashMap(getResourcesEndingWithAsBytes#1) num objects == 1
                  * 
                  *  Test Vectors:
                  *    java.lang.String:endsWith(...)@155: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@154: {0}, {1}
                  */
   152          final Map<String, byte[]> resources = new HashMap<String, byte[]>();
   153          
   154          for (String entry : entries) {
   155              if (entry.endsWith(resourcesSuffix)) {
   156                  resources.put(entry, getResourceBytes(entry));
   157              }
   158          }
   159          
   160          return resources;
   161      }
   162      
   163      /** {@inheritDoc} */
   164      @Override
   165      public Map<String, byte[]> getResourcesStartingWithAsBytes(
   166              final String resourcesPrefix) {
                 /* 
    P/P           *  Method: Map getResourcesStartingWithAsBytes(String)
                  * 
                  *  Preconditions:
                  *    this.entries != null
                  *    (soft) this.zipFile != null
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@169 != null
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new HashMap(getResourcesStartingWithAsBytes#1)
                  *    new HashMap(getResourcesStartingWithAsBytes#1) num objects == 1
                  * 
                  *  Test Vectors:
                  *    java.lang.String:startsWith(...)@170: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@169: {0}, {1}
                  */
   167          final Map<String, byte[]> resources = new HashMap<String, byte[]>();
   168          
   169          for (String entry : entries) {
   170              if (entry.startsWith(resourcesPrefix)) {
   171                  resources.put(entry, getResourceBytes(entry));
   172              }
   173          }
   174          
   175          return resources;
   176      }
   177      
   178      /** {@inheritDoc} */
   179      @Override
   180      public Map<String, InputStream> getResourcesStartingWithAsInputStreams(
   181              final String resourcesPrefix) {
                 /* 
    P/P           *  Method: Map getResourcesStartingWithAsInputStreams(String)
                  * 
                  *  Preconditions:
                  *    this.entries != null
                  *    (soft) this.zipFile != null
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@185 != null
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new HashMap(getResourcesStartingWithAsInputStreams#1)
                  *    new HashMap(getResourcesStartingWithAsInputStreams#1) num objects == 1
                  * 
                  *  Test Vectors:
                  *    java.lang.String:startsWith(...)@186: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@185: {0}, {1}
                  */
   182          final Map<String, InputStream> resources =
   183                  new HashMap<String, InputStream>();
   184          
   185          for (String entry : entries) {
   186              if (entry.startsWith(resourcesPrefix)) {
   187                  resources.put(entry, getResourceInputStream(entry));
   188              }
   189          }
   190          
   191          return resources;
   192      }
   193      
   194      /** {@inheritDoc} */
   195      @Override
   196      public List<String> getResourcesStartingWith(final String resourcesPrefix) {
                 /* 
    P/P           *  Method: List getResourcesStartingWith(String)
                  * 
                  *  Preconditions:
                  *    this.entries != null
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@199 != null
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new ArrayList(getResourcesStartingWith#1)
                  *    new ArrayList(getResourcesStartingWith#1) num objects == 1
                  * 
                  *  Test Vectors:
                  *    java.lang.String:startsWith(...)@200: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@199: {0}, {1}
                  */
   197          final List<String> resources = new ArrayList<String>();
   198          
   199          for (String entry : entries) {
   200              if (entry.startsWith(resourcesPrefix)) {
   201                  resources.add(entry);
   202              }
   203          }
   204          
   205          return resources;
   206      }
   207  }
   208  








SofCheck Inspector Build Version : 2.17854
ZipResourceManager.java 2009-Jun-25 01:54:24
ZipResourceManager.class 2009-Sep-02 17:04:15