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.ftp;
018:
019: import org.apache.commons.vfs.FileSystemException;
020: import org.apache.commons.vfs.provider.AbstractRandomAccessStreamContent;
021: import org.apache.commons.vfs.util.RandomAccessMode;
022:
023: import java.io.DataInputStream;
024: import java.io.FilterInputStream;
025: import java.io.IOException;
026:
027: class FtpRandomAccessContent extends AbstractRandomAccessStreamContent {
028: private final FtpFileObject fileObject;
029:
030: protected long filePointer = 0;
031: private DataInputStream dis = null;
032: private FtpFileObject.FtpInputStream mis = null;
033:
034: FtpRandomAccessContent(final FtpFileObject fileObject,
035: RandomAccessMode mode) {
036: super (mode);
037:
038: this .fileObject = fileObject;
039: // fileSystem = (FtpFileSystem) this.fileObject.getFileSystem();
040: }
041:
042: public long getFilePointer() throws IOException {
043: return filePointer;
044: }
045:
046: public void seek(long pos) throws IOException {
047: if (pos == filePointer) {
048: // no change
049: return;
050: }
051:
052: if (pos < 0) {
053: throw new FileSystemException(
054: "vfs.provider/random-access-invalid-position.error",
055: new Object[] { new Long(pos) });
056: }
057: if (dis != null) {
058: close();
059: }
060:
061: filePointer = pos;
062: }
063:
064: protected DataInputStream getDataInputStream() throws IOException {
065: if (dis != null) {
066: return dis;
067: }
068:
069: // FtpClient client = fileSystem.getClient();
070: mis = fileObject.getInputStream(filePointer);
071: dis = new DataInputStream(new FilterInputStream(mis) {
072: public int read() throws IOException {
073: int ret = super .read();
074: if (ret > -1) {
075: filePointer++;
076: }
077: return ret;
078: }
079:
080: public int read(byte b[]) throws IOException {
081: int ret = super .read(b);
082: if (ret > -1) {
083: filePointer += ret;
084: }
085: return ret;
086: }
087:
088: public int read(byte b[], int off, int len)
089: throws IOException {
090: int ret = super .read(b, off, len);
091: if (ret > -1) {
092: filePointer += ret;
093: }
094: return ret;
095: }
096:
097: public void close() throws IOException {
098: FtpRandomAccessContent.this .close();
099: }
100: });
101:
102: return dis;
103: }
104:
105: public void close() throws IOException {
106: if (dis != null) {
107: mis.abort();
108:
109: // this is to avoid recursive close
110: DataInputStream oldDis = dis;
111: dis = null;
112: oldDis.close();
113: mis = null;
114: }
115: }
116:
117: public long length() throws IOException {
118: return fileObject.getContent().getSize();
119: }
120: }
|