Created
February 8, 2010 16:19
-
-
Save pingles/298305 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.commons.lang.NotImplementedException; | |
import org.apache.hadoop.conf.Configuration; | |
import org.apache.hadoop.fs.*; | |
import org.apache.hadoop.fs.permission.FsPermission; | |
import org.apache.hadoop.util.Progressable; | |
import java.io.BufferedInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.URI; | |
import java.net.URL; | |
import java.net.URLConnection; | |
public class ReadOnlyHttpFileSystem extends FileSystem { | |
private URI uri; | |
private Path path; | |
private static final long DEFAULT_BLOCK_SIZE = 4 * 1024; | |
@Override | |
public void initialize(URI uri, Configuration conf) throws IOException { | |
this.uri = uri; | |
} | |
@Override | |
public URI getUri() { | |
return uri; | |
} | |
@Override | |
public FSDataInputStream open(Path path, int i) throws IOException { | |
this.path = path; | |
URL url = new URL(uri.toString()); | |
InputStream inputStream = url.openStream(); | |
BufferedInputStream bufferedInput = new BufferedInputStream(inputStream); | |
return new FSDataInputStream(bufferedInput); | |
} | |
@Override | |
public FSDataOutputStream create(Path path, FsPermission fsPermission, boolean b, int i, short s, long l, Progressable progressable) throws IOException { | |
throw new NotImplementedException("Cannot create path"); | |
} | |
@Override | |
public FSDataOutputStream append(Path path, int i, Progressable progressable) throws IOException { | |
throw new NotImplementedException("Cannot append to path"); | |
} | |
@Override | |
public boolean rename(Path path, Path path1) throws IOException { | |
throw new NotImplementedException("Cannot rename path"); | |
} | |
@Override | |
public boolean delete(Path path) throws IOException { | |
throw new NotImplementedException("Cannot delete path"); | |
} | |
@Override | |
public boolean delete(Path path, boolean b) throws IOException { | |
throw new NotImplementedException("Cannot delete path"); | |
} | |
@Override | |
public FileStatus[] listStatus(Path path) throws IOException { | |
return new FileStatus[]{ createFileStatus(path) }; | |
} | |
@Override | |
public void setWorkingDirectory(Path path) { | |
} | |
@Override | |
public Path getWorkingDirectory() { | |
return this.path; | |
} | |
@Override | |
public boolean mkdirs(Path path, FsPermission fsPermission) throws IOException { | |
throw new NotImplementedException("Cannot make directory"); | |
} | |
@Override | |
public FileStatus getFileStatus(Path path) throws IOException { | |
return createFileStatus(path); | |
} | |
private FileStatus createFileStatus(Path path) throws IOException { | |
URL url = new URL(uri.toString()); | |
URLConnection urlConnection = url.openConnection(); | |
return new FileStatus(urlConnection.getContentLength(), false, 1, DEFAULT_BLOCK_SIZE, urlConnection.getLastModified(), path); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment