001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.vfs.provider.http;
018:
019: import org.apache.commons.httpclient.methods.GetMethod;
020: import org.apache.commons.vfs.FileSystemException;
021: import org.apache.commons.vfs.provider.AbstractRandomAccessStreamContent;
022: import org.apache.commons.vfs.util.MonitorInputStream;
023: import org.apache.commons.vfs.util.RandomAccessMode;
024:
025: import java.io.DataInputStream;
026: import java.io.FilterInputStream;
027: import java.io.IOException;
028: import java.net.HttpURLConnection;
029:
030: class HttpRandomAccesContent extends AbstractRandomAccessStreamContent {
031: private final HttpFileObject fileObject;
032: private final HttpFileSystem fileSystem;
033:
034: protected long filePointer = 0;
035: private DataInputStream dis = null;
036: private MonitorInputStream mis = null;
037:
038: HttpRandomAccesContent(final HttpFileObject fileObject,
039: RandomAccessMode mode) {
040: super (mode);
041:
042: this .fileObject = fileObject;
043: fileSystem = (HttpFileSystem) this .fileObject.getFileSystem();
044: }
045:
046: public long getFilePointer() throws IOException {
047: return filePointer;
048: }
049:
050: public void seek(long pos) throws IOException {
051: if (pos == filePointer) {
052: // no change
053: return;
054: }
055:
056: if (pos < 0) {
057: throw new FileSystemException(
058: "vfs.provider/random-access-invalid-position.error",
059: new Object[] { new Long(pos) });
060: }
061: if (dis != null) {
062: close();
063: }
064:
065: filePointer = pos;
066: }
067:
068: protected DataInputStream getDataInputStream() throws IOException {
069: if (dis != null) {
070: return dis;
071: }
072:
073: final GetMethod getMethod = new GetMethod();
074: fileObject.setupMethod(getMethod);
075: getMethod.setRequestHeader("Range", "bytes=" + filePointer
076: + "-");
077: final int status = fileSystem.getClient().executeMethod(
078: getMethod);
079: if (status != HttpURLConnection.HTTP_PARTIAL) {
080: throw new FileSystemException(
081: "vfs.provider.http/get-range.error",
082: new Object[] { fileObject.getName(),
083: new Long(filePointer) });
084: }
085:
086: mis = new HttpFileObject.HttpInputStream(getMethod);
087: dis = new DataInputStream(new FilterInputStream(mis) {
088: public int read() throws IOException {
089: int ret = super .read();
090: if (ret > -1) {
091: filePointer++;
092: }
093: return ret;
094: }
095:
096: public int read(byte b[]) throws IOException {
097: int ret = super .read(b);
098: if (ret > -1) {
099: filePointer += ret;
100: }
101: return ret;
102: }
103:
104: public int read(byte b[], int off, int len)
105: throws IOException {
106: int ret = super .read(b, off, len);
107: if (ret > -1) {
108: filePointer += ret;
109: }
110: return ret;
111: }
112: });
113:
114: return dis;
115: }
116:
117: public void close() throws IOException {
118: if (dis != null) {
119: dis.close();
120: dis = null;
121: mis = null;
122: }
123: }
124:
125: public long length() throws IOException {
126: return fileObject.getContent().getSize();
127: }
128: }
|