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