View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2.provider.ftp;
18  
19  import java.io.DataInputStream;
20  import java.io.FilterInputStream;
21  import java.io.IOException;
22  
23  import org.apache.commons.vfs2.FileSystemException;
24  import org.apache.commons.vfs2.provider.AbstractRandomAccessStreamContent;
25  import org.apache.commons.vfs2.util.RandomAccessMode;
26  
27  /**
28   * Implements FTP stream-based random access.
29   */
30  class FtpRandomAccessContent extends AbstractRandomAccessStreamContent {
31  
32      protected long filePointer;
33  
34      private final FtpFileObject fileObject;
35      private DataInputStream dis;
36      private FtpFileObject.FtpInputStream mis;
37  
38      FtpRandomAccessContent(final FtpFileObject fileObject, final RandomAccessMode mode) {
39          super(mode);
40  
41          this.fileObject = fileObject;
42          // fileSystem = (FtpFileSystem) this.fileObject.getFileSystem();
43      }
44  
45      @Override
46      public void close() throws IOException {
47          if (dis != null) {
48              mis.abort();
49  
50              // this is to avoid recursive close
51              final DataInputStream oldDis = dis;
52              dis = null;
53              oldDis.close();
54              mis = null;
55          }
56      }
57  
58      @Override
59      protected DataInputStream getDataInputStream() throws IOException {
60          if (dis != null) {
61              return dis;
62          }
63  
64          // FtpClient client = fileSystem.getClient();
65          mis = fileObject.getInputStream(filePointer);
66          dis = new DataInputStream(new FilterInputStream(mis) {
67              @Override
68              public void close() throws IOException {
69                  FtpRandomAccessContent.this.close();
70              }
71  
72              @Override
73              public int read() throws IOException {
74                  final int ret = super.read();
75                  if (ret > -1) {
76                      filePointer++;
77                  }
78                  return ret;
79              }
80  
81              @Override
82              public int read(final byte[] b) throws IOException {
83                  final int ret = super.read(b);
84                  if (ret > -1) {
85                      filePointer += ret;
86                  }
87                  return ret;
88              }
89  
90              @Override
91              public int read(final byte[] b, final int off, final int len) throws IOException {
92                  final int ret = super.read(b, off, len);
93                  if (ret > -1) {
94                      filePointer += ret;
95                  }
96                  return ret;
97              }
98          });
99  
100         return dis;
101     }
102 
103     @Override
104     public long getFilePointer() throws IOException {
105         return filePointer;
106     }
107 
108     @Override
109     public long length() throws IOException {
110         return fileObject.getContent().getSize();
111     }
112 
113     @Override
114     public void seek(final long pos) throws IOException {
115         if (pos == filePointer) {
116             // no change
117             return;
118         }
119 
120         if (pos < 0) {
121             throw new FileSystemException("vfs.provider/random-access-invalid-position.error", Long.valueOf(pos));
122         }
123         if (dis != null) {
124             close();
125         }
126 
127         filePointer = pos;
128     }
129 }