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  * @since 1.7
34  */
35 
36 public class InvalidPathException
37     extends IllegalArgumentException
38 {
39     static final long serialVersionUID = 4355821422286746137L;
40 
41     private String input;
42     private int index;
43 
44     /**
45      * Constructs an instance from the given input string, reason, and error
46      * index.
47      *
48      * @param  input   the input string
49      * @param  reason  a string explaining why the input was rejected
50      * @param  index   the index at which the error occurred,
51      *                 or {@code -1} if the index is not known
52      *
53      * @throws  NullPointerException
54      *          if either the input or reason strings are {@code null}
55      *
56      * @throws  IllegalArgumentException
57      *          if the error index is less than {@code -1}
58      */
InvalidPathException(String input, String reason, int index)59     public InvalidPathException(String input, String reason, int index) {
60         super(reason);
61         if ((input == null) || (reason == null))
62             throw new NullPointerException();
63         if (index < -1)
64             throw new IllegalArgumentException();
65         this.input = input;
66         this.index = index;
67     }
68 
69     /**
70      * Constructs an instance from the given input string and reason.  The
71      * resulting object will have an error index of {@code -1}.
72      *
73      * @param  input   the input string
74      * @param  reason  a string explaining why the input was rejected
75      *
76      * @throws  NullPointerException
77      *          if either the input or reason strings are {@code null}
78      */
InvalidPathException(String input, String reason)79     public InvalidPathException(String input, String reason) {
80         this(input, reason, -1);
81     }
82 
83     /**
84      * Returns the input string.
85      *
86      * @return  the input string
87      */
getInput()88     public String getInput() {
89         return input;
90     }
91 
92     /**
93      * Returns a string explaining why the input string was rejected.
94      *
95      * @return  the reason string
96      */
getReason()97     public String getReason() {
98         return super.getMessage();
99     }
100 
101     /**
102      * Returns an index into the input string of the position at which the
103      * error occurred, or {@code -1} if this position is not known.
104      *
105      * @return  the error index
106      */
getIndex()107     public int getIndex() {
108         return index;
109     }
110 
111     /**
112      * Returns a string describing the error.  The resulting string
113      * consists of the reason string followed by a colon character
114      * ({@code ':'}), a space, and the input string.  If the error index is
115      * defined then the string {@code " at index "} followed by the index, in
116      * decimal, is inserted after the reason string and before the colon
117      * character.
118      *
119      * @return  a string describing the error
120      */
getMessage()121     public String getMessage() {
122         StringBuilder sb = new StringBuilder();
123         sb.append(getReason());
124         if (index > -1) {
125             sb.append(" at index ");
126             sb.append(index);
127         }
128         sb.append(": ");
129         sb.append(input);
130         return sb.toString();
131     }
132 }
133