001    /*
002     * ftp4j - A pure Java FTP client library
003     * 
004     * Copyright (C) 2008-2009 Carlo Pelliccia (www.sauronsoftware.it)
005     * 
006     * This program is free software: you can redistribute it and/or modify
007     * it under the terms of the GNU Lesser General Public License version
008     * 2.1, as published by the Free Software Foundation.
009     *
010     * This program is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU General Public License for more details.
014     *
015     * You should have received a copy of the GNU Lesser General Public
016     * License version 2.1 along with this program.
017     * If not, see <http://www.gnu.org/licenses/>.
018     */
019    package it.sauronsoftware.ftp4j;
020    
021    import java.util.Date;
022    
023    /**
024     * The instances of this class represents the files in a remote FTP directory.
025     * 
026     * @author Carlo Pelliccia
027     */
028    public class FTPFile {
029    
030            /**
031             * The value for the type "file".
032             */
033            public static final int TYPE_FILE = 0;
034    
035            /**
036             * The value for the type "directory".
037             */
038            public static final int TYPE_DIRECTORY = 1;
039    
040            /**
041             * The value for the type "link".
042             */
043            public static final int TYPE_LINK = 2;
044    
045            /**
046             * The name of the file.
047             */
048            private String name = null;
049    
050            /**
051             * The path of the linked file, if this one is a link.
052             */
053            private String link = null;
054    
055            /**
056             * The last modified date of the file.
057             */
058            private Date modifiedDate = null;
059    
060            /**
061             * The size of the file (bytes).
062             */
063            private long size = -1;
064    
065            /**
066             * The type of the entry represented. It must be {@link FTPFile#TYPE_FILE},
067             * {@link FTPFile#TYPE_DIRECTORY} or {@link FTPFile#TYPE_LINK}.
068             */
069            private int type;
070    
071            /**
072             * Returns the last modified date of the file. Pay attention: it could be
073             * null if the information is not supplied by the server.
074             * 
075             * @return The last modified date of the file, or null if the information is
076             *         not supplied.
077             */
078            public Date getModifiedDate() {
079                    return modifiedDate;
080            }
081    
082            /**
083             * Sets the last modified date of the file.
084             * 
085             * @param modifiedDate
086             *            The last modified date of the file.
087             */
088            public void setModifiedDate(Date modifiedDate) {
089                    this.modifiedDate = modifiedDate;
090            }
091    
092            /**
093             * Returns the name of the file.
094             * 
095             * @return The name of the file.
096             */
097            public String getName() {
098                    return name;
099            }
100    
101            /**
102             * Sets the name of the file.
103             * 
104             * @param name
105             *            The name of the file.
106             */
107            public void setName(String name) {
108                    this.name = name;
109            }
110    
111            /**
112             * Returns the type of the entry represented. It must be
113             * {@link FTPFile#TYPE_FILE}, {@link FTPFile#TYPE_DIRECTORY} or
114             * {@link FTPFile#TYPE_LINK}.
115             * 
116             * @return The type of the entry represented. It must be
117             *         {@link FTPFile#TYPE_FILE}, {@link FTPFile#TYPE_DIRECTORY} or
118             *         {@link FTPFile#TYPE_LINK}.
119             */
120            public int getType() {
121                    return type;
122            }
123    
124            /**
125             * Sets the type of the entry represented. It can be
126             * {@link FTPFile#TYPE_FILE}, {@link FTPFile#TYPE_DIRECTORY} or
127             * {@link FTPFile#TYPE_LINK}.
128             * 
129             * @param type
130             *            The type of the entry represented. It can be
131             *            {@link FTPFile#TYPE_FILE}, {@link FTPFile#TYPE_DIRECTORY} or
132             *            {@link FTPFile#TYPE_LINK}.
133             */
134            public void setType(int type) {
135                    this.type = type;
136            }
137    
138            /**
139             * Returns the size of the file (bytes). A negative value is returned if the
140             * information is not available.
141             * 
142             * @return The size of the file (bytes). A negative value is returned if the
143             *         information is not available.
144             */
145            public long getSize() {
146                    return size;
147            }
148    
149            /**
150             * Sets the size of the file (bytes).
151             * 
152             * @param size
153             *            The size of the file (bytes).
154             */
155            public void setSize(long size) {
156                    this.size = size;
157            }
158    
159            /**
160             * This method returns the path of the linked file, if this one is a link.
161             * If this is not a link, or if the information is not available, it returns
162             * null.
163             * 
164             * @return The path of the linked file, if this one is a link. If this is
165             *         not a link, or if the information is not available, it returns
166             *         null.
167             */
168            public String getLink() {
169                    return link;
170            }
171    
172            /**
173             * This method sets the path of the linked file, if this one is a link.
174             * 
175             * @param link
176             *            The path of the linked file, if this one is a link.
177             */
178            public void setLink(String link) {
179                    this.link = link;
180            }
181    
182            public String toString() {
183                    StringBuffer buffer = new StringBuffer();
184                    buffer.append(getClass().getName());
185                    buffer.append(" [name=");
186                    buffer.append(name);
187                    buffer.append(", type=");
188                    if (type == TYPE_FILE) {
189                            buffer.append("FILE");
190                    } else if (type == TYPE_DIRECTORY) {
191                            buffer.append("DIRECTORY");
192                    } else if (type == TYPE_LINK) {
193                            buffer.append("LINK");
194                            buffer.append(", link=");
195                            buffer.append(link);
196                    } else {
197                            buffer.append("UNKNOWN");
198                    }
199                    buffer.append(", size=");
200                    buffer.append(size);
201                    buffer.append(", modifiedDate=");
202                    buffer.append(modifiedDate);
203                    buffer.append("]");
204                    return buffer.toString();
205            }
206    
207    }