1 /*
   2  * Copyright (c) 2007, 2009, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * 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     @java.io.Serial
  41     static final long serialVersionUID = -3055425747967319812L;
  42 
  43     private final String file;
  44     private final String other;
  45 
  46     /**
  47      * Constructs an instance of this class. This constructor should be used
  48      * when an operation involving one file fails and there isn't any additional
  49      * information to explain the reason.
  50      *
  51      * @param   file
  52      *          a string identifying the file or {@code null} if not known.
  53      */
  54     public FileSystemException(String file) {
  55         super((String)null);
  56         this.file = file;
  57         this.other = null;
  58     }
  59 
  60     /**
  61      * Constructs an instance of this class. This constructor should be used
  62      * when an operation involving two files fails, or there is additional
  63      * information to explain the reason.
  64      *
  65      * @param   file
  66      *          a string identifying the file or {@code null} if not known.
  67      * @param   other
  68      *          a string identifying the other file or {@code null} if there
  69      *          isn't another file or if not known
  70      * @param   reason
  71      *          a reason message with additional information or {@code null}
  72      */
  73     public FileSystemException(String file, String other, String reason) {
  74         super(reason);
  75         this.file = file;
  76         this.other = other;
  77     }
  78 
  79     /**
  80      * Returns the file used to create this exception.
  81      *
  82      * @return  the file (can be {@code null})
  83      */
  84     public String getFile() {
  85         return file;
  86     }
  87 
  88     /**
  89      * Returns the other file used to create this exception.
  90      *
  91      * @return  the other file (can be {@code null})
  92      */
  93     public String getOtherFile() {
  94         return other;
  95     }
  96 
  97     /**
  98      * Returns the string explaining why the file system operation failed.
  99      *
 100      * @return  the string explaining why the file system operation failed
 101      */
 102     public String getReason() {
 103         return super.getMessage();
 104     }
 105 
 106     /**
 107      * Returns the detail message string.
 108      */
 109     @Override
 110     public String getMessage() {
 111         if (file == null && other == null)
 112             return getReason();
 113         StringBuilder sb = new StringBuilder();
 114         if (file != null)
 115             sb.append(file);
 116         if (other != null) {
 117             sb.append(" -> ");
 118             sb.append(other);
 119         }
 120         if (getReason() != null) {
 121             sb.append(": ");
 122             sb.append(getReason());
 123         }
 124         return sb.toString();
 125     }
 126 }