Skip to content

Instantly share code, notes, and snippets.

@pingles
Created February 8, 2010 16:19
Show Gist options
  • Save pingles/298305 to your computer and use it in GitHub Desktop.
Save pingles/298305 to your computer and use it in GitHub Desktop.
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