src/core/net/sf/basedb/util/bfs/DiskInputStreamLocator.java

Code
Comments
Other
Rev Date Author Line
5224 27 Jan 10 nicklas 1 /**
5224 27 Jan 10 nicklas 2   $Id$
5224 27 Jan 10 nicklas 3
5224 27 Jan 10 nicklas 4   Copyright (C) 2009 Nicklas Nordborg
5224 27 Jan 10 nicklas 5
5224 27 Jan 10 nicklas 6   This file is part of BASE - BioArray Software Environment.
5224 27 Jan 10 nicklas 7   Available at http://base.thep.lu.se/
5224 27 Jan 10 nicklas 8
5224 27 Jan 10 nicklas 9   BASE is free software; you can redistribute it and/or
5224 27 Jan 10 nicklas 10   modify it under the terms of the GNU General Public License
5224 27 Jan 10 nicklas 11   as published by the Free Software Foundation; either version 3
5224 27 Jan 10 nicklas 12   of the License, or (at your option) any later version.
5224 27 Jan 10 nicklas 13
5224 27 Jan 10 nicklas 14   BASE is distributed in the hope that it will be useful,
5224 27 Jan 10 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
5224 27 Jan 10 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5224 27 Jan 10 nicklas 17   GNU General Public License for more details.
5224 27 Jan 10 nicklas 18
5224 27 Jan 10 nicklas 19   You should have received a copy of the GNU General Public License
5224 27 Jan 10 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
5224 27 Jan 10 nicklas 21 */
5224 27 Jan 10 nicklas 22 package net.sf.basedb.util.bfs;
5224 27 Jan 10 nicklas 23
5224 27 Jan 10 nicklas 24 import java.io.FileInputStream;
5224 27 Jan 10 nicklas 25 import java.io.FileNotFoundException;
5224 27 Jan 10 nicklas 26 import java.io.IOException;
5224 27 Jan 10 nicklas 27 import java.io.InputStream;
5224 27 Jan 10 nicklas 28
5224 27 Jan 10 nicklas 29
5224 27 Jan 10 nicklas 30 /**
5224 27 Jan 10 nicklas 31   Input stream locator implementation that works with files on the
5224 27 Jan 10 nicklas 32   local file system. The files must be located in the specified root 
5224 27 Jan 10 nicklas 33   directory. Subdirectories or parent paths are not supported.
5224 27 Jan 10 nicklas 34
5224 27 Jan 10 nicklas 35   @author Nicklas
5224 27 Jan 10 nicklas 36   @version 2.15
5224 27 Jan 10 nicklas 37   @base.modified $Date$
5224 27 Jan 10 nicklas 38 */
5224 27 Jan 10 nicklas 39 public class DiskInputStreamLocator
5224 27 Jan 10 nicklas 40   implements InputStreamLocator
5224 27 Jan 10 nicklas 41 {
5224 27 Jan 10 nicklas 42
5224 27 Jan 10 nicklas 43   private final java.io.File dir;
5224 27 Jan 10 nicklas 44
5224 27 Jan 10 nicklas 45   /**
5224 27 Jan 10 nicklas 46     Create a new input stream locator.
5224 27 Jan 10 nicklas 47     @param dir The directory in the local file system in which the
5224 27 Jan 10 nicklas 48       files should be located
5224 27 Jan 10 nicklas 49     @throws NullPointerException If the dir parameter is null
5224 27 Jan 10 nicklas 50     @throws FileNotFoundException If the given path is not an existing directory
5224 27 Jan 10 nicklas 51   */
5224 27 Jan 10 nicklas 52   public DiskInputStreamLocator(java.io.File dir)
5224 27 Jan 10 nicklas 53     throws IOException
5224 27 Jan 10 nicklas 54   {    
5224 27 Jan 10 nicklas 55     if (dir == null) throw new NullPointerException("dir");
5224 27 Jan 10 nicklas 56     if (!dir.isDirectory()) throw new FileNotFoundException(dir.toString());
5224 27 Jan 10 nicklas 57     this.dir = dir;
5224 27 Jan 10 nicklas 58   }
5224 27 Jan 10 nicklas 59   
5224 27 Jan 10 nicklas 60   /**
5224 27 Jan 10 nicklas 61     Finds and opens an input stream to the file with the given filename.
5224 27 Jan 10 nicklas 62   */
5224 27 Jan 10 nicklas 63   @Override
5224 27 Jan 10 nicklas 64   public InputStream getInputStream(String filename) 
5224 27 Jan 10 nicklas 65     throws IOException
5224 27 Jan 10 nicklas 66   {
5224 27 Jan 10 nicklas 67     java.io.File file = new java.io.File(dir, filename);
5224 27 Jan 10 nicklas 68     if (!file.exists()) throw new FileNotFoundException("File not found: " + file);
5224 27 Jan 10 nicklas 69     return new FileInputStream(file);
5224 27 Jan 10 nicklas 70   }
5224 27 Jan 10 nicklas 71
5225 29 Jan 10 nicklas 72   @Override
5225 29 Jan 10 nicklas 73   public long getSize(String filename)
5225 29 Jan 10 nicklas 74   {
5225 29 Jan 10 nicklas 75     java.io.File file = new java.io.File(dir, filename);
5225 29 Jan 10 nicklas 76     return file.length();
5225 29 Jan 10 nicklas 77   }
5225 29 Jan 10 nicklas 78
5224 27 Jan 10 nicklas 79 }