Path: blob/master/src/java.base/share/classes/java/nio/file/InvalidPathException.java
41159 views
/*1* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.nio.file;2627/**28* Unchecked exception thrown when path string cannot be converted into a29* {@link Path} because the path string contains invalid characters, or30* the path string is invalid for other file system specific reasons.31*32* @since 1.733*/3435public class InvalidPathException36extends IllegalArgumentException37{38@java.io.Serial39static final long serialVersionUID = 4355821422286746137L;4041/**42* The input string.43*/44private String input;4546/**47* The index of the input string at which the error occurred or48* {@code -1} if not known.49*/50private int index;5152/**53* Constructs an instance from the given input string, reason, and error54* index.55*56* @param input the input string57* @param reason a string explaining why the input was rejected58* @param index the index at which the error occurred,59* or {@code -1} if the index is not known60*61* @throws NullPointerException62* if either the input or reason strings are {@code null}63*64* @throws IllegalArgumentException65* if the error index is less than {@code -1}66*/67public InvalidPathException(String input, String reason, int index) {68super(reason);69if ((input == null) || (reason == null))70throw new NullPointerException();71if (index < -1)72throw new IllegalArgumentException();73this.input = input;74this.index = index;75}7677/**78* Constructs an instance from the given input string and reason. The79* resulting object will have an error index of {@code -1}.80*81* @param input the input string82* @param reason a string explaining why the input was rejected83*84* @throws NullPointerException85* if either the input or reason strings are {@code null}86*/87public InvalidPathException(String input, String reason) {88this(input, reason, -1);89}9091/**92* Returns the input string.93*94* @return the input string95*/96public String getInput() {97return input;98}99100/**101* Returns a string explaining why the input string was rejected.102*103* @return the reason string104*/105public String getReason() {106return super.getMessage();107}108109/**110* Returns an index into the input string of the position at which the111* error occurred, or {@code -1} if this position is not known.112*113* @return the error index114*/115public int getIndex() {116return index;117}118119/**120* Returns a string describing the error. The resulting string121* consists of the reason string followed by a colon character122* ({@code ':'}), a space, and the input string. If the error index is123* defined then the string {@code " at index "} followed by the index, in124* decimal, is inserted after the reason string and before the colon125* character.126*127* @return a string describing the error128*/129public String getMessage() {130StringBuilder sb = new StringBuilder();131sb.append(getReason());132if (index > -1) {133sb.append(" at index ");134sb.append(index);135}136sb.append(": ");137sb.append(input);138return sb.toString();139}140}141142143