package org.apache.commons.vfs2.provider.http;
import java.io.DataInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.provider.AbstractRandomAccessStreamContent;
import org.apache.commons.vfs2.util.MonitorInputStream;
import org.apache.commons.vfs2.util.RandomAccessMode;
class HttpRandomAccessContent<FS extends HttpFileSystem> extends AbstractRandomAccessStreamContent {
protected long filePointer = 0;
private final HttpFileObject<FS> fileObject;
private final HttpFileSystem fileSystem;
private DataInputStream dis = null;
private MonitorInputStream mis = null;
HttpRandomAccessContent(final HttpFileObject<FS> fileObject, final RandomAccessMode mode) {
super(mode);
this.fileObject = fileObject;
fileSystem = (HttpFileSystem) this.fileObject.getFileSystem();
}
@Override
public long getFilePointer() throws IOException {
return filePointer;
}
@Override
public void seek(final long pos) throws IOException {
if (pos == filePointer) {
return;
}
if (pos < 0) {
throw new FileSystemException("vfs.provider/random-access-invalid-position.error", Long.valueOf(pos));
}
if (dis != null) {
close();
}
filePointer = pos;
}
@Override
protected DataInputStream getDataInputStream() throws IOException {
if (dis != null) {
return dis;
}
final GetMethod getMethod = new GetMethod();
fileObject.setupMethod(getMethod);
getMethod.setRequestHeader("Range", "bytes=" + filePointer + "-");
final int status = fileSystem.getClient().executeMethod(getMethod);
if (status != HttpURLConnection.HTTP_PARTIAL && status != HttpURLConnection.HTTP_OK) {
throw new FileSystemException("vfs.provider.http/get-range.error", fileObject.getName(),
Long.valueOf(filePointer), Integer.valueOf(status));
}
mis = new HttpFileObject.HttpInputStream(getMethod);
if (status == HttpURLConnection.HTTP_OK) {
final long skipped = mis.skip(filePointer);
if (skipped != filePointer) {
throw new FileSystemException("vfs.provider.http/get-range.error", fileObject.getName(),
Long.valueOf(filePointer), Integer.valueOf(status));
}
}
dis = new DataInputStream(new FilterInputStream(mis) {
@Override
public int read() throws IOException {
final int ret = super.read();
if (ret > -1) {
filePointer++;
}
return ret;
}
@Override
public int read(final byte[] b) throws IOException {
final int ret = super.read(b);
if (ret > -1) {
filePointer += ret;
}
return ret;
}
@Override
public int read(final byte[] b, final int off, final int len) throws IOException {
final int ret = super.read(b, off, len);
if (ret > -1) {
filePointer += ret;
}
return ret;
}
});
return dis;
}
@Override
public void close() throws IOException {
if (dis != null) {
dis.close();
dis = null;
mis = null;
}
}
@Override
public long length() throws IOException {
return fileObject.getContent().getSize();
}
}