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