1789N/A/*
2362N/A * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
1789N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1789N/A *
1789N/A * This code is free software; you can redistribute it and/or modify it
1789N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
1789N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1789N/A *
1789N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1789N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1789N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1789N/A * version 2 for more details (a copy is included in the LICENSE file that
1789N/A * accompanied this code).
1789N/A *
1789N/A * You should have received a copy of the GNU General Public License version
1789N/A * 2 along with this work; if not, write to the Free Software Foundation,
1789N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1789N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1789N/A */
1789N/Apackage sun.net.ftp;
1789N/A
1789N/Aimport java.util.Date;
1789N/Aimport java.util.HashMap;
1789N/A
1789N/A/**
1789N/A * A {@code FtpDirEntry} is a class agregating all the information that the FTP client
1789N/A * can gather from the server by doing a {@code LST} (or {@code NLST}) command and
1789N/A * parsing the output. It will typically contain the name, type, size, last modification
1789N/A * time, owner and group of the file, although some of these could be unavailable
1789N/A * due to specific FTP server limitations.
1789N/A *
1789N/A * @see sun.net.ftp.FtpDirParser
1789N/A * @since 1.7
1789N/A */
1789N/Apublic class FtpDirEntry {
1789N/A
1789N/A public enum Type {
1789N/A
1789N/A FILE, DIR, PDIR, CDIR, LINK
1789N/A };
1789N/A
1789N/A public enum Permission {
1789N/A
1789N/A USER(0), GROUP(1), OTHERS(2);
1789N/A int value;
1789N/A
1789N/A Permission(int v) {
1789N/A value = v;
1789N/A }
1789N/A };
1789N/A private final String name;
1789N/A private String user = null;
1789N/A private String group = null;
1789N/A private long size = -1;
1789N/A private java.util.Date created = null;
1789N/A private java.util.Date lastModified = null;
1789N/A private Type type = Type.FILE;
1789N/A private boolean[][] permissions = null;
1789N/A private HashMap<String, String> facts = new HashMap<String, String>();
1789N/A
1789N/A private FtpDirEntry() {
1789N/A name = null;
1789N/A }
1789N/A
1789N/A /**
1789N/A * Creates an FtpDirEntry instance with only the name being set.
1789N/A *
1789N/A * @param name The name of the file
1789N/A */
1789N/A public FtpDirEntry(String name) {
1789N/A this.name = name;
1789N/A }
1789N/A
1789N/A /**
1789N/A * Returns the name of the remote file.
1789N/A *
1789N/A * @return a {@code String} containing the name of the remote file.
1789N/A */
1789N/A public String getName() {
1789N/A return name;
1789N/A }
1789N/A
1789N/A /**
1789N/A * Returns the user name of the owner of the file as returned by the FTP
1789N/A * server, if provided. This could be a name or a user id (number).
1789N/A *
1789N/A * @return a {@code String} containing the user name or
1789N/A * {@code null} if that information is not available.
1789N/A */
1789N/A public String getUser() {
1789N/A return user;
1789N/A }
1789N/A
1789N/A /**
1789N/A * Sets the user name of the owner of the file. Intended mostly to be
1789N/A * used from inside a {@link java.net.FtpDirParser} implementation.
1789N/A *
1789N/A * @param user The user name of the owner of the file, or {@code null}
1789N/A * if that information is not available.
1789N/A * @return this FtpDirEntry
1789N/A */
1789N/A public FtpDirEntry setUser(String user) {
1789N/A this.user = user;
1789N/A return this;
1789N/A }
1789N/A
1789N/A /**
1789N/A * Returns the group name of the file as returned by the FTP
1789N/A * server, if provided. This could be a name or a group id (number).
1789N/A *
1789N/A * @return a {@code String} containing the group name or
1789N/A * {@code null} if that information is not available.
1789N/A */
1789N/A public String getGroup() {
1789N/A return group;
1789N/A }
1789N/A
1789N/A /**
1789N/A * Sets the name of the group to which the file belong. Intended mostly to be
1789N/A * used from inside a {@link java.net.FtpDirParser} implementation.
1789N/A *
1789N/A * @param group The name of the group to which the file belong, or {@code null}
1789N/A * if that information is not available.
1789N/A * @return this FtpDirEntry
1789N/A */
1789N/A public FtpDirEntry setGroup(String group) {
1789N/A this.group = group;
1789N/A return this;
1789N/A }
1789N/A
1789N/A /**
1789N/A * Returns the size of the remote file as it was returned by the FTP
1789N/A * server, if provided.
1789N/A *
1789N/A * @return the size of the file or -1 if that information is not available.
1789N/A */
1789N/A public long getSize() {
1789N/A return size;
1789N/A }
1789N/A
1789N/A /**
1789N/A * Sets the size of that file. Intended mostly to be used from inside an
1789N/A * {@link java.net.FtpDirParser} implementation.
1789N/A *
1789N/A * @param size The size, in bytes, of that file. or -1 if unknown.
1789N/A * @return this FtpDirEntry
1789N/A */
1789N/A public FtpDirEntry setSize(long size) {
1789N/A this.size = size;
1789N/A return this;
1789N/A }
1789N/A
1789N/A /**
1789N/A * Returns the type of the remote file as it was returned by the FTP
1789N/A * server, if provided.
1789N/A * It returns a FtpDirEntry.Type enum and the values can be:
1789N/A * - FtpDirEntry.Type.FILE for a normal file
1789N/A * - FtpDirEntry.Type.DIR for a directory
1789N/A * - FtpDirEntry.Type.LINK for a symbolic link
1789N/A *
1789N/A * @return a {@code FtpDirEntry.Type} describing the type of the file
1789N/A * or {@code null} if that information is not available.
1789N/A */
1789N/A public Type getType() {
1789N/A return type;
1789N/A }
1789N/A
1789N/A /**
1789N/A * Sets the type of the file. Intended mostly to be used from inside an
1789N/A * {@link java.net.FtpDirParser} implementation.
1789N/A *
1789N/A * @param type the type of this file or {@code null} if that information
1789N/A * is not available.
1789N/A * @return this FtpDirEntry
1789N/A */
1789N/A public FtpDirEntry setType(Type type) {
1789N/A this.type = type;
1789N/A return this;
1789N/A }
1789N/A
1789N/A /**
1789N/A * Returns the last modification time of the remote file as it was returned
1789N/A * by the FTP server, if provided, {@code null} otherwise.
1789N/A *
1789N/A * @return a <code>Date</code> representing the last time the file was
1789N/A * modified on the server, or {@code null} if that
1789N/A * information is not available.
1789N/A */
1789N/A public java.util.Date getLastModified() {
1789N/A return this.lastModified;
1789N/A }
1789N/A
1789N/A /**
1789N/A * Sets the last modification time of the file. Intended mostly to be used
1789N/A * from inside an {@link java.net.FtpDirParser} implementation.
1789N/A *
1789N/A * @param lastModified The Date representing the last modification time, or
1789N/A * {@code null} if that information is not available.
1789N/A * @return this FtpDirEntry
1789N/A */
1789N/A public FtpDirEntry setLastModified(Date lastModified) {
1789N/A this.lastModified = lastModified;
1789N/A return this;
1789N/A }
1789N/A
1789N/A /**
1789N/A * Returns whether read access is granted for a specific permission.
1789N/A *
1789N/A * @param p the Permission (user, group, others) to check.
1789N/A * @return {@code true} if read access is granted.
1789N/A */
1789N/A public boolean canRead(Permission p) {
1789N/A if (permissions != null) {
1789N/A return permissions[p.value][0];
1789N/A }
1789N/A return false;
1789N/A }
1789N/A
1789N/A /**
1789N/A * Returns whether write access is granted for a specific permission.
1789N/A *
1789N/A * @param p the Permission (user, group, others) to check.
1789N/A * @return {@code true} if write access is granted.
1789N/A */
1789N/A public boolean canWrite(Permission p) {
1789N/A if (permissions != null) {
1789N/A return permissions[p.value][1];
1789N/A }
1789N/A return false;
1789N/A }
1789N/A
1789N/A /**
1789N/A * Returns whether execute access is granted for a specific permission.
1789N/A *
1789N/A * @param p the Permission (user, group, others) to check.
1789N/A * @return {@code true} if execute access is granted.
1789N/A */
1789N/A public boolean canExexcute(Permission p) {
1789N/A if (permissions != null) {
1789N/A return permissions[p.value][2];
1789N/A }
1789N/A return false;
1789N/A }
1789N/A
1789N/A /**
1789N/A * Sets the permissions for that file. Intended mostly to be used
1789N/A * from inside an {@link java.net.FtpDirParser} implementation.
1789N/A * The permissions array is a 3x3 {@code boolean} array, the first index being
1789N/A * the User, group or owner (0, 1 and 2 respectively) while the second
1789N/A * index is read, write or execute (0, 1 and 2 respectively again).
1789N/A * <p>E.G.: {@code permissions[1][2]} is the group/execute permission.</p>
1789N/A *
1789N/A * @param permissions a 3x3 {@code boolean} array
1789N/A * @return this {@code FtpDirEntry}
1789N/A */
1789N/A public FtpDirEntry setPermissions(boolean[][] permissions) {
1789N/A this.permissions = permissions;
1789N/A return this;
1789N/A }
1789N/A
1789N/A /**
1789N/A * Adds a 'fact', as defined in RFC 3659, to the list of facts of this file.
1789N/A * Intended mostly to be used from inside a {@link java.net.FtpDirParser}
1789N/A * implementation.
1789N/A *
1789N/A * @param fact the name of the fact (e.g. "Media-Type"). It is not case-sensitive.
1789N/A * @param value the value associated with this fact.
1789N/A * @return this {@code FtpDirEntry}
1789N/A */
1789N/A public FtpDirEntry addFact(String fact, String value) {
1789N/A facts.put(fact.toLowerCase(), value);
1789N/A return this;
1789N/A }
1789N/A
1789N/A /**
1789N/A * Returns the requested 'fact', as defined in RFC 3659, if available.
1789N/A *
1789N/A * @param fact The name of the fact *e.g. "Media-Type"). It is not case sensitive.
1789N/A * @return The value of the fact or, {@code null} if that fact wasn't
1789N/A * provided by the server.
1789N/A */
1789N/A public String getFact(String fact) {
1789N/A return facts.get(fact.toLowerCase());
1789N/A }
1789N/A
1789N/A /**
1789N/A * Returns the creation time of the file, when provided by the server.
1789N/A *
1789N/A * @return The Date representing the creation time, or {@code null}
1789N/A * if the server didn't provide that information.
1789N/A */
1789N/A public Date getCreated() {
1789N/A return created;
1789N/A }
1789N/A
1789N/A /**
1789N/A * Sets the creation time for that file. Intended mostly to be used from
1789N/A * inside a {@link java.net.FtpDirParser} implementation.
1789N/A *
1789N/A * @param created the Date representing the creation time for that file, or
1789N/A * {@code null} if that information is not available.
1789N/A * @return this FtpDirEntry
1789N/A */
1789N/A public FtpDirEntry setCreated(Date created) {
1789N/A this.created = created;
1789N/A return this;
1789N/A }
1789N/A
1789N/A /**
1789N/A * Returns a string representation of the object.
1789N/A * The {@code toString} method for class {@code FtpDirEntry}
1789N/A * returns a string consisting of the name of the file, followed by its
1789N/A * type between brackets, followed by the user and group between
1789N/A * parenthesis, then size between '{', and, finally, the lastModified of last
1789N/A * modification if it's available.
1789N/A *
1789N/A * @return a string representation of the object.
1789N/A */
1789N/A @Override
1789N/A public String toString() {
1789N/A if (lastModified == null) {
1789N/A return name + " [" + type + "] (" + user + " / " + group + ") " + size;
1789N/A }
1789N/A return name + " [" + type + "] (" + user + " / " + group + ") {" + size + "} " + java.text.DateFormat.getDateInstance().format(lastModified);
1789N/A }
1789N/A}