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.vfs.provider.sftp;
    18
    19  import org.apache.commons.vfs.FileSystemException;
    20  import org.apache.commons.vfs.provider.AbstractRandomAccessStreamContent;
    21  import org.apache.commons.vfs.util.RandomAccessMode;
    22
    23  import java.io.DataInputStream;
    24  import java.io.FilterInputStream;
    25  import java.io.IOException;
    26  import java.io.InputStream;
    27
    28  class SftpRandomAccessContent extends AbstractRandomAccessStreamContent
    29  {
    30      private final SftpFileObject fileObject;
    31
    32      protected long filePointer = 0;
    33      private DataInputStream dis = null;
    34      private InputStream mis = null;
    35
    36      SftpRandomAccessContent(final SftpFileObject fileObject, RandomAccessMode mode)
    37      {
    38          super(mode);
    39
    40          this.fileObject = fileObject;
    41          // fileSystem = (FtpFileSystem) this.fileObject.getFileSystem();
    42      }
    43
    44      public long getFilePointer() throws IOException
    45      {
    46          return filePointer;
    47      }
    48
    49      public void seek(long pos) throws IOException
    50      {
    51          if (pos == filePointer)
    52          {
    53              // no change
    54              return;
    55          }
    56
    57          if (pos < 0)
    58          {
    59              throw new FileSystemException("vfs.provider/random-access-invalid-position.error",
    60                  new Object[]
    61                  {
    62                      new Long(pos)
    63                  });
    64          }
    65          if (dis != null)
    66          {
    67              close();
    68          }
    69
    70          filePointer = pos;
    71      }
    72
    73  	protected DataInputStream getDataInputStream() throws IOException
    74      {
    75          if (dis != null)
    76          {
    77              return dis;
    78          }
    79
    80          // FtpClient client = fileSystem.getClient();
    81          mis = fileObject.getInputStream(filePointer);
    82          dis = new DataInputStream(new FilterInputStream(mis)
    83          {
    84              public int read() throws IOException
    85              {
    86                  int ret = super.read();
    87                  if (ret > -1)
    88                  {
    89                      filePointer++;
    90                  }
    91                  return ret;
    92              }
    93
    94              public int read(byte b[]) throws IOException
    95              {
    96                  int ret = super.read(b);
    97                  if (ret > -1)
    98                  {
    99                      filePointer+=ret;
    100                 }
    101                 return ret;
    102             }
    103
    104             public int read(byte b[], int off, int len) throws IOException
    105             {
    106                 int ret = super.read(b, off, len);
    107                 if (ret > -1)
    108                 {
    109                     filePointer+=ret;
    110                 }
    111                 return ret;
    112             }
    113
    114             public void close() throws IOException
    115             {
    116                 SftpRandomAccessContent.this.close();
    117             }
    118         });
    119
    120 		return dis;
    121 	}
    122
    123
    124     public void close() throws IOException
    125     {
    126         if (dis != null)
    127         {
    128             // mis.abort();
    129             mis.close();
    130
    131             // this is to avoid recursive close
    132             DataInputStream oldDis = dis;
    133             dis = null;
    134             oldDis.close();
    135             mis = null;
    136         }
    137     }
    138
    139     public long length() throws IOException
    140     {
    141         return fileObject.getContent().getSize();
    142     }
    143 }