Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 100   Methods: 8
NCLOC: 57   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
FileCache.java 100% 72.7% 37.5% 67.6%
coverage coverage
 1    /*
 2    * Licensed to the Apache Software Foundation (ASF) under one or more
 3    * contributor license agreements. See the NOTICE file distributed with
 4    * this work for additional information regarding copyright ownership.
 5    * The ASF licenses this file to You under the Apache License, Version 2.0
 6    * (the "License"); you may not use this file except in compliance with
 7    * the License. You may obtain a copy of the License at
 8    *
 9    * http://www.apache.org/licenses/LICENSE-2.0
 10    *
 11    * Unless required by applicable law or agreed to in writing, software
 12    * distributed under the License is distributed on an "AS IS" BASIS,
 13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14    * See the License for the specific language governing permissions and
 15    * limitations under the License.
 16    *
 17    * $Id: FileCache.java 541508 2007-05-25 01:54:12Z vgritsenko $
 18    */
 19   
 20    package org.apache.xindice.util;
 21   
 22    import java.io.File;
 23    import java.io.FileInputStream;
 24    import java.io.IOException;
 25    import java.util.Map;
 26    import java.util.WeakHashMap;
 27   
 28    /**
 29    * FileCache caches the content of files in memory.
 30    *
 31    * @version $Revision: 541508 $, $Date: 2007-05-24 18:54:12 -0700 (Thu, 24 May 2007) $
 32    */
 33    public class FileCache {
 34   
 35    /**
 36    * Caches FileCacheInfo objects. Keys are File objects.
 37    */
 38    private final Map cache = new WeakHashMap();
 39   
 40  19 public FileCache() {
 41    }
 42   
 43  0 public final boolean isInCache(File file) {
 44  0 return (cache.get(file) != null);
 45    }
 46   
 47  0 public final boolean isInCache(String name) {
 48  0 return (cache.get(new File(name)) != null);
 49    }
 50   
 51  0 public final boolean isModified(String name) {
 52  0 return isModified(new File(name));
 53    }
 54   
 55  0 public final boolean isModified(File file) {
 56  0 FileCacheInfo finfo = (FileCacheInfo) cache.get(file);
 57  0 return !file.exists()
 58    || finfo == null
 59    || (file.lastModified() != finfo.lastModified);
 60    }
 61   
 62  0 public final byte[] getFile(String name) throws IOException {
 63  0 return getFile(new File(name));
 64    }
 65   
 66  1961 public final byte[] getFile(File file) throws IOException {
 67  1961 if (!file.exists()) {
 68  3 return null;
 69    }
 70   
 71  1958 FileCacheInfo finfo = (FileCacheInfo) cache.get(file);
 72  1958 long lastmod = file.lastModified();
 73  1958 if (finfo == null || finfo.lastModified != lastmod) {
 74  1957 FileInputStream fis = new FileInputStream(file);
 75  1957 byte[] content = new byte[fis.available()];
 76  1957 fis.read(content);
 77  1957 fis.close();
 78  1957 finfo = new FileCacheInfo(file, lastmod, content);
 79  1957 cache.put(file, finfo);
 80  1957 return content;
 81    } else {
 82  1 return finfo.content;
 83    }
 84    }
 85   
 86    /**
 87    * FileCacheInfo
 88    */
 89    private class FileCacheInfo {
 90    public File file;
 91    public long lastModified = 0;
 92    public byte[] content;
 93   
 94  1957 public FileCacheInfo(File file, long lastModified, byte[] content) {
 95  1957 this.file = file;
 96  1957 this.lastModified = lastModified;
 97  1957 this.content = content;
 98    }
 99    }
 100    }