Project

General

Profile

1
package eu.dnetlib.dhp.common.java.io;
2

    
3
import java.io.File;
4
import java.io.IOException;
5

    
6
import org.apache.hadoop.conf.Configuration;
7
import org.apache.hadoop.fs.FSDataInputStream;
8
import org.apache.hadoop.fs.FileSystem;
9
import org.apache.hadoop.fs.Path;
10

    
11
/**
12
 * Path to a directory or a file along with information about the 
13
 * file system in which the path is defined.
14
 * 
15
 * @author Mateusz Kobos
16
 *
17
 */
18
public class FileSystemPath {
19
	private final FileSystem fs;
20
	private final Path path;
21

    
22
	/**
23
	 * Path in the local file system
24
	 */
25
	public FileSystemPath(File file) throws IOException {
26
		this(new Path(file.toURI()));
27
	}
28
	
29
	/**
30
	 * Path in the local file system
31
	 */
32
	public FileSystemPath(Path path) throws IOException{
33
		this(FileSystem.get(new Configuration(false)), path);
34
	}
35
	
36
	/**
37
	 * Path in the given file system
38
	 */
39
	public FileSystemPath(FileSystem fs, Path path){
40
		this.fs = fs;
41
		this.path = path;
42
	}
43
	
44
	/** Create a path with a child element */
45
	public FileSystemPath(FileSystemPath parent, String child){
46
		this.fs = parent.getFileSystem();
47
		this.path = new Path(parent.getPath(), child);
48
	}
49

    
50
	public FileSystem getFileSystem() {
51
		return fs;
52
	}
53

    
54
	public Path getPath() {
55
		return path;
56
	}
57
	
58
	public FSDataInputStream getInputStream() throws IOException{
59
		return getFileSystem().open(getPath());
60
	}
61
}
(5-5/7)