jdk/src/share/classes/java/nio/file/FileSystemException.java
changeset 2057 3acf8e5e2ca0
child 2072 80dfe4469bbd
equal deleted inserted replaced
2056:115e09b7a004 2057:3acf8e5e2ca0
       
     1 /*
       
     2  * Copyright 2007-2009 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package java.nio.file;
       
    27 
       
    28 import java.io.IOException;
       
    29 
       
    30 /**
       
    31  * Thrown when a file system operation fails on one or two files. This class is
       
    32  * the general class for file system exceptions.
       
    33  *
       
    34  * @since 1.7
       
    35  */
       
    36 
       
    37 public class FileSystemException
       
    38     extends IOException
       
    39 {
       
    40     static final long serialVersionUID = -3055425747967319812L;
       
    41 
       
    42     private final String file;
       
    43     private final String other;
       
    44 
       
    45     /**
       
    46      * Constructs an instance of this class. This constructor should be used
       
    47      * when an operation involving one file fails and there isn't any additional
       
    48      * information to explain the reason.
       
    49      *
       
    50      * @param   file
       
    51      *          A string identifying the file or {@code null} if not known.
       
    52      */
       
    53     public FileSystemException(String file) {
       
    54         super((String)null);
       
    55         this.file = file;
       
    56         this.other = null;
       
    57     }
       
    58 
       
    59     /**
       
    60      * Constructs an instance of this class. This constructor should be used
       
    61      * when an operation involving two files fails, or there is additional
       
    62      * information to explain the reason.
       
    63      *
       
    64      * @param   file
       
    65      *          A string identifying the file or {@code null} if not known.
       
    66      * @param   other
       
    67      *          A string identifying the other file or {@code null} if there
       
    68      *          isn't another file or if not known
       
    69      * @param   reason
       
    70      *          A reason message with additional information or {@code null}
       
    71      */
       
    72     public FileSystemException(String file, String other, String reason) {
       
    73         super(reason);
       
    74         this.file = file;
       
    75         this.other = other;
       
    76     }
       
    77 
       
    78     /**
       
    79      * Returns the file used to create this exception.
       
    80      *
       
    81      * @return  The file (can be {@code null})
       
    82      */
       
    83     public String getFile() {
       
    84         return file;
       
    85     }
       
    86 
       
    87     /**
       
    88      * Returns the other file used to create this exception.
       
    89      *
       
    90      * @return  The other file (can be {@code null})
       
    91      */
       
    92     public String getOtherFile() {
       
    93         return other;
       
    94     }
       
    95 
       
    96     /**
       
    97      * Returns the string explaining why the file system operation failed.
       
    98      *
       
    99      * @return  The string explaining why the file system operation failed
       
   100      */
       
   101     public String getReason() {
       
   102         return super.getMessage();
       
   103     }
       
   104 
       
   105     /**
       
   106      * Returns the detail message string.
       
   107      */
       
   108     @Override
       
   109     public String getMessage() {
       
   110         if (file == null && other == null)
       
   111             return getReason();
       
   112         StringBuilder sb = new StringBuilder();
       
   113         if (file != null)
       
   114             sb.append(file);
       
   115         if (other != null) {
       
   116             sb.append(" -> ");
       
   117             sb.append(other);
       
   118         }
       
   119         if (getReason() != null) {
       
   120             sb.append(": ");
       
   121             sb.append(getReason());
       
   122         }
       
   123         return sb.toString();
       
   124     }
       
   125 }