5224 |
27 Jan 10 |
nicklas |
1 |
/** |
5224 |
27 Jan 10 |
nicklas |
$Id$ |
5224 |
27 Jan 10 |
nicklas |
3 |
|
5224 |
27 Jan 10 |
nicklas |
Copyright (C) 2009 Nicklas Nordborg |
5224 |
27 Jan 10 |
nicklas |
5 |
|
5224 |
27 Jan 10 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
5224 |
27 Jan 10 |
nicklas |
Available at http://base.thep.lu.se/ |
5224 |
27 Jan 10 |
nicklas |
8 |
|
5224 |
27 Jan 10 |
nicklas |
BASE is free software; you can redistribute it and/or |
5224 |
27 Jan 10 |
nicklas |
modify it under the terms of the GNU General Public License |
5224 |
27 Jan 10 |
nicklas |
as published by the Free Software Foundation; either version 3 |
5224 |
27 Jan 10 |
nicklas |
of the License, or (at your option) any later version. |
5224 |
27 Jan 10 |
nicklas |
13 |
|
5224 |
27 Jan 10 |
nicklas |
BASE is distributed in the hope that it will be useful, |
5224 |
27 Jan 10 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
5224 |
27 Jan 10 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5224 |
27 Jan 10 |
nicklas |
GNU General Public License for more details. |
5224 |
27 Jan 10 |
nicklas |
18 |
|
5224 |
27 Jan 10 |
nicklas |
You should have received a copy of the GNU General Public License |
5224 |
27 Jan 10 |
nicklas |
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.IOException; |
5224 |
27 Jan 10 |
nicklas |
25 |
import java.io.InputStream; |
5225 |
29 Jan 10 |
nicklas |
26 |
import java.util.HashMap; |
5225 |
29 Jan 10 |
nicklas |
27 |
import java.util.Map; |
5224 |
27 Jan 10 |
nicklas |
28 |
|
5224 |
27 Jan 10 |
nicklas |
29 |
import net.sf.basedb.core.DbControl; |
5224 |
27 Jan 10 |
nicklas |
30 |
import net.sf.basedb.core.Directory; |
5224 |
27 Jan 10 |
nicklas |
31 |
import net.sf.basedb.core.File; |
5224 |
27 Jan 10 |
nicklas |
32 |
|
5224 |
27 Jan 10 |
nicklas |
33 |
/** |
5224 |
27 Jan 10 |
nicklas |
Input stream locator implementation that works with files within a given |
5224 |
27 Jan 10 |
nicklas |
directory in the BASE file system. The files must be located in the |
5224 |
27 Jan 10 |
nicklas |
specified root directory. Subdirectories or parent paths are not |
5224 |
27 Jan 10 |
nicklas |
supported. |
5224 |
27 Jan 10 |
nicklas |
38 |
|
5224 |
27 Jan 10 |
nicklas |
@author Nicklas |
5224 |
27 Jan 10 |
nicklas |
@version 2.15 |
5224 |
27 Jan 10 |
nicklas |
@base.modified $Date$ |
5224 |
27 Jan 10 |
nicklas |
42 |
*/ |
5224 |
27 Jan 10 |
nicklas |
43 |
public class BaseInputStreamLocator |
5224 |
27 Jan 10 |
nicklas |
44 |
implements InputStreamLocator |
5224 |
27 Jan 10 |
nicklas |
45 |
{ |
5224 |
27 Jan 10 |
nicklas |
46 |
|
5224 |
27 Jan 10 |
nicklas |
47 |
private final DbControl dc; |
5224 |
27 Jan 10 |
nicklas |
48 |
private final Directory dir; |
5225 |
29 Jan 10 |
nicklas |
49 |
private final Map<String, File> cache; |
5224 |
27 Jan 10 |
nicklas |
50 |
|
5224 |
27 Jan 10 |
nicklas |
51 |
/** |
5224 |
27 Jan 10 |
nicklas |
Create a new input stream locator. |
5224 |
27 Jan 10 |
nicklas |
@param dc A DbControl to use for database access |
5224 |
27 Jan 10 |
nicklas |
@param dir The directory in the BASE file system in which the |
5224 |
27 Jan 10 |
nicklas |
files should be located |
5224 |
27 Jan 10 |
nicklas |
56 |
*/ |
5224 |
27 Jan 10 |
nicklas |
57 |
public BaseInputStreamLocator(DbControl dc, Directory dir) |
5224 |
27 Jan 10 |
nicklas |
58 |
{ |
5224 |
27 Jan 10 |
nicklas |
59 |
if (dir == null) throw new NullPointerException("dir"); |
5224 |
27 Jan 10 |
nicklas |
60 |
this.dc = dc; |
5224 |
27 Jan 10 |
nicklas |
61 |
this.dir = dir; |
5225 |
29 Jan 10 |
nicklas |
62 |
this.cache = new HashMap<String, File>(); |
5224 |
27 Jan 10 |
nicklas |
63 |
} |
5224 |
27 Jan 10 |
nicklas |
64 |
|
5224 |
27 Jan 10 |
nicklas |
65 |
/** |
5224 |
27 Jan 10 |
nicklas |
Finds and opens an input stream to the file with the given filename. |
5224 |
27 Jan 10 |
nicklas |
67 |
*/ |
5224 |
27 Jan 10 |
nicklas |
68 |
@Override |
5224 |
27 Jan 10 |
nicklas |
69 |
public InputStream getInputStream(String filename) |
5224 |
27 Jan 10 |
nicklas |
70 |
throws IOException |
5224 |
27 Jan 10 |
nicklas |
71 |
{ |
5225 |
29 Jan 10 |
nicklas |
72 |
return getFile(filename).getDownloadStream(0); |
5224 |
27 Jan 10 |
nicklas |
73 |
} |
5224 |
27 Jan 10 |
nicklas |
74 |
|
5225 |
29 Jan 10 |
nicklas |
75 |
@Override |
5225 |
29 Jan 10 |
nicklas |
76 |
public long getSize(String filename) |
5225 |
29 Jan 10 |
nicklas |
77 |
{ |
5225 |
29 Jan 10 |
nicklas |
78 |
return getFile(filename).getSize(); |
5225 |
29 Jan 10 |
nicklas |
79 |
} |
5225 |
29 Jan 10 |
nicklas |
80 |
|
5225 |
29 Jan 10 |
nicklas |
81 |
private File getFile(String filename) |
5225 |
29 Jan 10 |
nicklas |
82 |
{ |
5225 |
29 Jan 10 |
nicklas |
83 |
File f = cache.get(filename); |
5225 |
29 Jan 10 |
nicklas |
84 |
if (f == null) |
5225 |
29 Jan 10 |
nicklas |
85 |
{ |
5225 |
29 Jan 10 |
nicklas |
86 |
f = File.getFile(dc, dir, filename, false); |
5225 |
29 Jan 10 |
nicklas |
87 |
cache.put(filename, f); |
5225 |
29 Jan 10 |
nicklas |
88 |
} |
5225 |
29 Jan 10 |
nicklas |
89 |
return f; |
5225 |
29 Jan 10 |
nicklas |
90 |
} |
5225 |
29 Jan 10 |
nicklas |
91 |
|
5224 |
27 Jan 10 |
nicklas |
92 |
} |