Path: blob/master/src/java.base/share/classes/sun/net/ftp/FtpDirEntry.java
41159 views
/*1* Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.net.ftp;2526import java.util.Date;27import java.util.HashMap;2829/**30* A {@code FtpDirEntry} is a class agregating all the information that the FTP client31* can gather from the server by doing a {@code LST} (or {@code NLST}) command and32* parsing the output. It will typically contain the name, type, size, last modification33* time, owner and group of the file, although some of these could be unavailable34* due to specific FTP server limitations.35*36* @see sun.net.ftp.FtpDirParser37* @since 1.738*/39public class FtpDirEntry {4041public enum Type {4243FILE, DIR, PDIR, CDIR, LINK44};4546public enum Permission {4748USER(0), GROUP(1), OTHERS(2);49int value;5051Permission(int v) {52value = v;53}54};55private final String name;56private String user = null;57private String group = null;58private long size = -1;59private java.util.Date created = null;60private java.util.Date lastModified = null;61private Type type = Type.FILE;62private boolean[][] permissions = null;63private HashMap<String, String> facts = new HashMap<String, String>();6465private FtpDirEntry() {66name = null;67}6869/**70* Creates an FtpDirEntry instance with only the name being set.71*72* @param name The name of the file73*/74public FtpDirEntry(String name) {75this.name = name;76}7778/**79* Returns the name of the remote file.80*81* @return a {@code String} containing the name of the remote file.82*/83public String getName() {84return name;85}8687/**88* Returns the user name of the owner of the file as returned by the FTP89* server, if provided. This could be a name or a user id (number).90*91* @return a {@code String} containing the user name or92* {@code null} if that information is not available.93*/94public String getUser() {95return user;96}9798/**99* Sets the user name of the owner of the file. Intended mostly to be100* used from inside a {@link java.net.FtpDirParser} implementation.101*102* @param user The user name of the owner of the file, or {@code null}103* if that information is not available.104* @return this FtpDirEntry105*/106public FtpDirEntry setUser(String user) {107this.user = user;108return this;109}110111/**112* Returns the group name of the file as returned by the FTP113* server, if provided. This could be a name or a group id (number).114*115* @return a {@code String} containing the group name or116* {@code null} if that information is not available.117*/118public String getGroup() {119return group;120}121122/**123* Sets the name of the group to which the file belong. Intended mostly to be124* used from inside a {@link java.net.FtpDirParser} implementation.125*126* @param group The name of the group to which the file belong, or {@code null}127* if that information is not available.128* @return this FtpDirEntry129*/130public FtpDirEntry setGroup(String group) {131this.group = group;132return this;133}134135/**136* Returns the size of the remote file as it was returned by the FTP137* server, if provided.138*139* @return the size of the file or -1 if that information is not available.140*/141public long getSize() {142return size;143}144145/**146* Sets the size of that file. Intended mostly to be used from inside an147* {@link java.net.FtpDirParser} implementation.148*149* @param size The size, in bytes, of that file. or -1 if unknown.150* @return this FtpDirEntry151*/152public FtpDirEntry setSize(long size) {153this.size = size;154return this;155}156157/**158* Returns the type of the remote file as it was returned by the FTP159* server, if provided.160* It returns a FtpDirEntry.Type enum and the values can be:161* - FtpDirEntry.Type.FILE for a normal file162* - FtpDirEntry.Type.DIR for a directory163* - FtpDirEntry.Type.LINK for a symbolic link164*165* @return a {@code FtpDirEntry.Type} describing the type of the file166* or {@code null} if that information is not available.167*/168public Type getType() {169return type;170}171172/**173* Sets the type of the file. Intended mostly to be used from inside an174* {@link java.net.FtpDirParser} implementation.175*176* @param type the type of this file or {@code null} if that information177* is not available.178* @return this FtpDirEntry179*/180public FtpDirEntry setType(Type type) {181this.type = type;182return this;183}184185/**186* Returns the last modification time of the remote file as it was returned187* by the FTP server, if provided, {@code null} otherwise.188*189* @return a <code>Date</code> representing the last time the file was190* modified on the server, or {@code null} if that191* information is not available.192*/193public java.util.Date getLastModified() {194return this.lastModified;195}196197/**198* Sets the last modification time of the file. Intended mostly to be used199* from inside an {@link java.net.FtpDirParser} implementation.200*201* @param lastModified The Date representing the last modification time, or202* {@code null} if that information is not available.203* @return this FtpDirEntry204*/205public FtpDirEntry setLastModified(Date lastModified) {206this.lastModified = lastModified;207return this;208}209210/**211* Returns whether read access is granted for a specific permission.212*213* @param p the Permission (user, group, others) to check.214* @return {@code true} if read access is granted.215*/216public boolean canRead(Permission p) {217if (permissions != null) {218return permissions[p.value][0];219}220return false;221}222223/**224* Returns whether write access is granted for a specific permission.225*226* @param p the Permission (user, group, others) to check.227* @return {@code true} if write access is granted.228*/229public boolean canWrite(Permission p) {230if (permissions != null) {231return permissions[p.value][1];232}233return false;234}235236/**237* Returns whether execute access is granted for a specific permission.238*239* @param p the Permission (user, group, others) to check.240* @return {@code true} if execute access is granted.241*/242public boolean canExexcute(Permission p) {243if (permissions != null) {244return permissions[p.value][2];245}246return false;247}248249/**250* Sets the permissions for that file. Intended mostly to be used251* from inside an {@link java.net.FtpDirParser} implementation.252* The permissions array is a 3x3 {@code boolean} array, the first index being253* the User, group or owner (0, 1 and 2 respectively) while the second254* index is read, write or execute (0, 1 and 2 respectively again).255* <p>E.G.: {@code permissions[1][2]} is the group/execute permission.</p>256*257* @param permissions a 3x3 {@code boolean} array258* @return this {@code FtpDirEntry}259*/260public FtpDirEntry setPermissions(boolean[][] permissions) {261this.permissions = permissions;262return this;263}264265/**266* Adds a 'fact', as defined in RFC 3659, to the list of facts of this file.267* Intended mostly to be used from inside a {@link java.net.FtpDirParser}268* implementation.269*270* @param fact the name of the fact (e.g. "Media-Type"). It is not case-sensitive.271* @param value the value associated with this fact.272* @return this {@code FtpDirEntry}273*/274public FtpDirEntry addFact(String fact, String value) {275facts.put(fact.toLowerCase(), value);276return this;277}278279/**280* Returns the requested 'fact', as defined in RFC 3659, if available.281*282* @param fact The name of the fact *e.g. "Media-Type"). It is not case sensitive.283* @return The value of the fact or, {@code null} if that fact wasn't284* provided by the server.285*/286public String getFact(String fact) {287return facts.get(fact.toLowerCase());288}289290/**291* Returns the creation time of the file, when provided by the server.292*293* @return The Date representing the creation time, or {@code null}294* if the server didn't provide that information.295*/296public Date getCreated() {297return created;298}299300/**301* Sets the creation time for that file. Intended mostly to be used from302* inside a {@link java.net.FtpDirParser} implementation.303*304* @param created the Date representing the creation time for that file, or305* {@code null} if that information is not available.306* @return this FtpDirEntry307*/308public FtpDirEntry setCreated(Date created) {309this.created = created;310return this;311}312313/**314* Returns a string representation of the object.315* The {@code toString} method for class {@code FtpDirEntry}316* returns a string consisting of the name of the file, followed by its317* type between brackets, followed by the user and group between318* parenthesis, then size between '{', and, finally, the lastModified of last319* modification if it's available.320*321* @return a string representation of the object.322*/323@Override324public String toString() {325if (lastModified == null) {326return name + " [" + type + "] (" + user + " / " + group + ") " + size;327}328return name + " [" + type + "] (" + user + " / " + group + ") {" + size + "} " + java.text.DateFormat.getDateInstance().format(lastModified);329}330}331332333