    View Javadoc
    1   package org.apache.commons.net.ftp;
    2
    3   /* ====================================================================
    4    * The Apache Software License, Version 1.1
    5    *
    6    * Copyright (c) 2001 The Apache Software Foundation.  All rights
    7    * reserved.
    8    *
    9    * Redistribution and use in source and binary forms, with or without
    10   * modification, are permitted provided that the following conditions
    11   * are met:
    12   *
    13   * 1. Redistributions of source code must retain the above copyright
    14   *    notice, this list of conditions and the following disclaimer.
    15   *
    16   * 2. Redistributions in binary form must reproduce the above copyright
    17   *    notice, this list of conditions and the following disclaimer in
    18   *    the documentation and/or other materials provided with the
    19   *    distribution.
    20   *
    21   * 3. The end-user documentation included with the redistribution,
    22   *    if any, must include the following acknowledgment:
    23   *       "This product includes software developed by the
    24   *        Apache Software Foundation (http://www.apache.org/)."
    25   *    Alternately, this acknowledgment may appear in the software itself,
    26   *    if and wherever such third-party acknowledgments normally appear.
    27   *
    28   * 4. The names "Apache" and "Apache Software Foundation" and
    29   *    "Apache Commons" must not be used to endorse or promote products
    30   *    derived from this software without prior written permission. For
    31   *    written permission, please contact apache@apache.org.
    32   *
    33   * 5. Products derived from this software may not be called "Apache",
    34   *    nor may "Apache" appear in their name, without
    35   *    prior written permission of the Apache Software Foundation.
    36   *
    37   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
    38   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    39   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    40   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
    41   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    42   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    43   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
    44   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    45   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    46   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
    47   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    48   * SUCH DAMAGE.
    49   * ====================================================================
    50   *
    51   * This software consists of voluntary contributions made by many
    52   * individuals on behalf of the Apache Software Foundation.  For more
    53   * information on the Apache Software Foundation, please see
    54   * <http://www.apache.org/>;.
    55   */
    56
    57  import java.io.Serializable;
    58  import java.util.Calendar;
    59
    60  /****
    61   * The FTPFile class is used to represent information about files stored
    62   * on an FTP server.  Because there is no standard representation for
    63   * file information on FTP servers, it may not always be possible to
    64   * extract all the information that can be represented by FTPFile, or
    65   * it may even be possible to extract more information.  In cases where
    66   * more information can be extracted, you will want to subclass FTPFile
    67   * and implement your own <a href="org.apache.commons.net.ftp.FTPFileListParser.html">
    68   * FTPFileListParser </a> to extract the information.
    69   * However, most FTP servers return file information in a format that
    70   * can be completely parsed by
    71   * <a href="org.apache.commons.net.ftp.DefaultFTPFileListParser.html">
    72   * DefaultFTPFileListParser </a> and stored in FTPFile.
    73   * <p>
    74   * <p>
    75   * @author Daniel F. Savarese
    76   * @see FTPFileListParser
    77   * @see DefaultFTPFileListParser
    78   * @see FTPClient#listFiles
    79   ***/
    80
    81  public class FTPFile implements Serializable
    82  {
    83      /*** A constant indicating an FTPFile is a file. ***/
    84      public static final int FILE_TYPE = 0;
    85      /*** A constant indicating an FTPFile is a directory. ***/
    86      public static final int DIRECTORY_TYPE = 1;
    87      /*** A constant indicating an FTPFile is a symbolic link. ***/
    88      public static final int SYMBOLIC_LINK_TYPE = 2;
    89      /*** A constant indicating an FTPFile is of unknown type. ***/
    90      public static final int UNKNOWN_TYPE = 3;
    91
    92      /*** A constant indicating user access permissions. ***/
    93      public static final int USER_ACCESS = 0;
    94      /*** A constant indicating group access permissions. ***/
    95      public static final int GROUP_ACCESS = 1;
    96      /*** A constant indicating world access permissions. ***/
    97      public static final int WORLD_ACCESS = 2;
    98
    99      /*** A constant indicating file/directory read permission. ***/
    100     public static final int READ_PERMISSION = 0;
    101     /*** A constant indicating file/directory write permission. ***/
    102     public static final int WRITE_PERMISSION = 1;
    103     /***
    104      * A constant indicating file execute permission or directory listing
    105      * permission.
    106      ***/
    107     public static final int EXECUTE_PERMISSION = 2;
    108
    109     int _type, _hardLinkCount;
    110     long _size;
    111     String _rawListing, _user, _group, _name, _link;
    112     Calendar _date;
    113     boolean[] _permissions[];
    114
    115     /**** Creates an empty FTPFile. ***/
    116     public FTPFile()
    117     {
    118         _permissions = new boolean[3][3];
    119         _rawListing = null;
    120         _type = UNKNOWN_TYPE;
    121         _hardLinkCount = 0;
    122         _size = 0;
    123         _user = null;
    124         _group = null;
    125         _date = null;
    126         _name = null;
    127     }
    128
    129
    130     /****
    131      * Set the original FTP server raw listing from which the FTPFile was
    132      * created.
    133      * <p>
    134      * @param rawListing  The raw FTP server listing.
    135      ***/
    136     public void setRawListing(String rawListing)
    137     {
    138         _rawListing = rawListing;
    139     }
    140
    141     /****
    142      * Get the original FTP server raw listing used to initialize the FTPFile.
    143      * <p>
    144      * @return The original FTP server raw listing used to initialize the
    145      *         FTPFile.
    146      ***/
    147     public String getRawListing()
    148     {
    149         return _rawListing;
    150     }
    151
    152
    153     /****
    154      * Determine if the file is a directory.
    155      * <p>
    156      * @return True if the file is of type <code>DIRECTORY_TYPE</code>, false if
    157      *         not.
    158      ***/
    159     public boolean isDirectory()
    160     {
    161         return (_type == DIRECTORY_TYPE);
    162     }
    163
    164     /****
    165      * Determine if the file is a regular file.
    166      * <p>
    167      * @return True if the file is of type <code>FILE_TYPE</code>, false if
    168      *         not.
    169      ***/
    170     public boolean isFile()
    171     {
    172         return (_type == FILE_TYPE);
    173     }
    174
    175     /****
    176      * Determine if the file is a symbolic link.
    177      * <p>
    178      * @return True if the file is of type <code>UNKNOWN_TYPE</code>, false if
    179      *         not.
    180      ***/
    181     public boolean isSymbolicLink()
    182     {
    183         return (_type == SYMBOLIC_LINK_TYPE);
    184     }
    185
    186     /****
    187      * Determine if the type of the file is unknown.
    188      * <p>
    189      * @return True if the file is of type <code>UNKNOWN_TYPE</code>, false if
    190      *         not.
    191      ***/
    192     public boolean isUnknown()
    193     {
    194         return (_type == UNKNOWN_TYPE);
    195     }
    196
    197
    198     /****
    199      * Set the type of the file (<code>DIRECTORY_TYPE</code>,
    200      * <code>FILE_TYPE</code>, etc.).
    201      * <p>
    202      * @param type  The integer code representing the type of the file.
    203      ***/
    204     public void setType(int type)
    205     {
    206         _type = type;
    207     }
    208
    209
    210     /****
    211      * Return the type of the file (one of the <code>_TYPE</code> constants),
    212      * e.g., if it is a directory, a regular file, or a symbolic link.
    213      * <p>
    214      * @return The type of the file.
    215      ***/
    216     public int getType()
    217     {
    218         return _type;
    219     }
    220
    221
    222     /****
    223      * Set the name of the file.
    224      * <p>
    225      * @param name  The name of the file.
    226      ***/
    227     public void setName(String name)
    228     {
    229         _name = name;
    230     }
    231
    232     /****
    233      * Return the name of the file.
    234      * <p>
    235      * @return The name of the file.
    236      ***/
    237     public String getName()
    238     {
    239         return _name;
    240     }
    241
    242
    243     /****
    244      * Set the file size in bytes.
    245      * <p>
    246      * @param The file size in bytes.
    247      ***/
    248     public void setSize(long size)
    249     {
    250         _size = size;
    251     }
    252
    253
    254     /****
    255      * Return the file size in bytes.
    256      * <p>
    257      * @return The file size in bytes.
    258      ***/
    259     public long getSize()
    260     {
    261         return _size;
    262     }
    263
    264
    265     /****
    266      * Set the number of hard links to this file.  This is not to be
    267      * confused with symbolic links.
    268      * <p>
    269      * @param links  The number of hard links to this file.
    270      ***/
    271     public void setHardLinkCount(int links)
    272     {
    273         _hardLinkCount = links;
    274     }
    275
    276
    277     /****
    278      * Return the number of hard links to this file.  This is not to be
    279      * confused with symbolic links.
    280      * <p>
    281      * @return The number of hard links to this file.
    282      ***/
    283     public int getHardLinkCount()
    284     {
    285         return _hardLinkCount;
    286     }
    287
    288
    289     /****
    290      * Set the name of the group owning the file.  This may be
    291      * a string representation of the group number.
    292      * <p>
    293      * @param group The name of the group owning the file.
    294      ***/
    295     public void setGroup(String group)
    296     {
    297         _group = group;
    298     }
    299
    300
    301     /****
    302      * Returns the name of the group owning the file.  Sometimes this will be
    303      * a string representation of the group number.
    304      * <p>
    305      * @return The name of the group owning the file.
    306      ***/
    307     public String getGroup()
    308     {
    309         return _group;
    310     }
    311
    312
    313     /****
    314      * Set the name of the user owning the file.  This may be
    315      * a string representation of the user number;
    316      * <p>
    317      * @param user The name of the user owning the file.
    318      ***/
    319     public void setUser(String user)
    320     {
    321         _user = user;
    322     }
    323
    324     /****
    325      * Returns the name of the user owning the file.  Sometimes this will be
    326      * a string representation of the user number.
    327      * <p>
    328      * @return The name of the user owning the file.
    329      ***/
    330     public String getUser()
    331     {
    332         return _user;
    333     }
    334
    335
    336     /****
    337      * If the FTPFile is a symbolic link, use this method to set the name of the
    338      * file being pointed to by the symbolic link.
    339      * <p>
    340      * @param link  The file pointed to by the symbolic link.
    341      ***/
    342     public void setLink(String link)
    343     {
    344         _link = link;
    345     }
    346
    347
    348     /****
    349      * If the FTPFile is a symbolic link, this method returns the name of the
    350      * file being pointed to by the symbolic link.  Otherwise it returns null.
    351      * <p>
    352      * @return The file pointed to by the symbolic link (null if the FTPFile
    353      *         is not a symbolic link).
    354      ***/
    355     public String getLink()
    356     {
    357         return _link;
    358     }
    359
    360
    361     /****
    362      * Set the file timestamp.  This usually the last modification time.
    363      * The parameter is not cloned, so do not alter its value after calling
    364      * this method.
    365      * <p>
    366      * @param date A Calendar instance representing the file timestamp.
    367      ***/
    368     public void setTimestamp(Calendar date)
    369     {
    370         _date = date;
    371     }
    372
    373
    374     /****
    375      * Returns the file timestamp.  This usually the last modification time.
    376      * <p>
    377      * @return A Calendar instance representing the file timestamp.
    378      ***/
    379     public Calendar getTimestamp()
    380     {
    381         return _date;
    382     }
    383
    384
    385     /****
    386      * Set if the given access group (one of the <code> _ACCESS </code>
    387      * constants) has the given access permission (one of the
    388      * <code> _PERMISSION </code> constants) to the file.
    389      * <p>
    390      * @param access The access group (one of the <code> _ACCESS </code>
    391      *               constants)
    392      * @param permission The access permission (one of the
    393      *               <code> _PERMISSION </code> constants)
    394      * @param value  True if permission is allowed, false if not.
    395      ***/
    396     public void setPermission(int access, int permission, boolean value)
    397     {
    398         _permissions[access][permission] = value;
    399     }
    400
    401
    402     /****
    403      * Determines if the given access group (one of the <code> _ACCESS </code>
    404      * constants) has the given access permission (one of the
    405      * <code> _PERMISSION </code> constants) to the file.
    406      * <p>
    407      * @param access The access group (one of the <code> _ACCESS </code>
    408      *               constants)
    409      * @param permission The access permission (one of the
    410      *               <code> _PERMISSION </code> constants)
    411      ***/
    412     public boolean hasPermission(int access, int permission)
    413     {
    414         return _permissions[access][permission];
    415     }
    416
    417
    418     /****
    419      * Returns a string representation of the FTPFile information.  This
    420      * will be the raw FTP server listing that was used to initialize the
    421      * FTPFile instance.
    422      * <p>
    423      * @return A string representation of the FTPFile information.
    424      ***/
    425     public String toString()
    426     {
    427         return _rawListing;
    428     }
    429
    430 }

This page was automatically generated by Maven