jdk/src/java.base/share/classes/java/nio/file/InvalidPathException.java
changeset 25859 3317bb8137f4
parent 24969 afa6934dd8e8
child 32143 394ab6a6658d
equal deleted inserted replaced
25858:836adbf7a2cd 25859:3317bb8137f4
       
     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 /**
       
    29  * Unchecked exception thrown when path string cannot be converted into a
       
    30  * {@link Path} because the path string contains invalid characters, or
       
    31  * the path string is invalid for other file system specific reasons.
       
    32  */
       
    33 
       
    34 public class InvalidPathException
       
    35     extends IllegalArgumentException
       
    36 {
       
    37     static final long serialVersionUID = 4355821422286746137L;
       
    38 
       
    39     private String input;
       
    40     private int index;
       
    41 
       
    42     /**
       
    43      * Constructs an instance from the given input string, reason, and error
       
    44      * index.
       
    45      *
       
    46      * @param  input   the input string
       
    47      * @param  reason  a string explaining why the input was rejected
       
    48      * @param  index   the index at which the error occurred,
       
    49      *                 or <tt>-1</tt> if the index is not known
       
    50      *
       
    51      * @throws  NullPointerException
       
    52      *          if either the input or reason strings are <tt>null</tt>
       
    53      *
       
    54      * @throws  IllegalArgumentException
       
    55      *          if the error index is less than <tt>-1</tt>
       
    56      */
       
    57     public InvalidPathException(String input, String reason, int index) {
       
    58         super(reason);
       
    59         if ((input == null) || (reason == null))
       
    60             throw new NullPointerException();
       
    61         if (index < -1)
       
    62             throw new IllegalArgumentException();
       
    63         this.input = input;
       
    64         this.index = index;
       
    65     }
       
    66 
       
    67     /**
       
    68      * Constructs an instance from the given input string and reason.  The
       
    69      * resulting object will have an error index of <tt>-1</tt>.
       
    70      *
       
    71      * @param  input   the input string
       
    72      * @param  reason  a string explaining why the input was rejected
       
    73      *
       
    74      * @throws  NullPointerException
       
    75      *          if either the input or reason strings are <tt>null</tt>
       
    76      */
       
    77     public InvalidPathException(String input, String reason) {
       
    78         this(input, reason, -1);
       
    79     }
       
    80 
       
    81     /**
       
    82      * Returns the input string.
       
    83      *
       
    84      * @return  the input string
       
    85      */
       
    86     public String getInput() {
       
    87         return input;
       
    88     }
       
    89 
       
    90     /**
       
    91      * Returns a string explaining why the input string was rejected.
       
    92      *
       
    93      * @return  the reason string
       
    94      */
       
    95     public String getReason() {
       
    96         return super.getMessage();
       
    97     }
       
    98 
       
    99     /**
       
   100      * Returns an index into the input string of the position at which the
       
   101      * error occurred, or <tt>-1</tt> if this position is not known.
       
   102      *
       
   103      * @return  the error index
       
   104      */
       
   105     public int getIndex() {
       
   106         return index;
       
   107     }
       
   108 
       
   109     /**
       
   110      * Returns a string describing the error.  The resulting string
       
   111      * consists of the reason string followed by a colon character
       
   112      * (<tt>':'</tt>), a space, and the input string.  If the error index is
       
   113      * defined then the string <tt>" at index "</tt> followed by the index, in
       
   114      * decimal, is inserted after the reason string and before the colon
       
   115      * character.
       
   116      *
       
   117      * @return  a string describing the error
       
   118      */
       
   119     public String getMessage() {
       
   120         StringBuilder sb = new StringBuilder();
       
   121         sb.append(getReason());
       
   122         if (index > -1) {
       
   123             sb.append(" at index ");
       
   124             sb.append(index);
       
   125         }
       
   126         sb.append(": ");
       
   127         sb.append(input);
       
   128         return sb.toString();
       
   129     }
       
   130 }