|
1 /* |
|
2 * Copyright 2008-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 sun.nio.fs; |
|
27 |
|
28 import java.nio.file.*; |
|
29 import java.io.IOException; |
|
30 |
|
31 /** |
|
32 * Internal exception thrown by native methods when error detected. |
|
33 */ |
|
34 |
|
35 class UnixException extends Exception { |
|
36 static final long serialVersionUID = 7227016794320723218L; |
|
37 |
|
38 private int errno; |
|
39 private String msg; |
|
40 |
|
41 UnixException(int errno) { |
|
42 this.errno = errno; |
|
43 this.msg = null; |
|
44 } |
|
45 |
|
46 UnixException(String msg) { |
|
47 this.errno = 0; |
|
48 this.msg = msg; |
|
49 } |
|
50 |
|
51 int errno() { |
|
52 return errno; |
|
53 } |
|
54 |
|
55 void setError(int errno) { |
|
56 this.errno = errno; |
|
57 this.msg = null; |
|
58 } |
|
59 |
|
60 String errorString() { |
|
61 if (msg != null) { |
|
62 return msg; |
|
63 } else { |
|
64 return new String(UnixNativeDispatcher.strerror(errno())); |
|
65 } |
|
66 } |
|
67 |
|
68 @Override |
|
69 public String getMessage() { |
|
70 return errorString(); |
|
71 } |
|
72 |
|
73 /** |
|
74 * Map well known errors to specific exceptions where possible; otherwise |
|
75 * return more general FileSystemException. |
|
76 */ |
|
77 private IOException translateToIOException(String file, String other) { |
|
78 // created with message rather than errno |
|
79 if (msg != null) |
|
80 return new IOException(msg); |
|
81 |
|
82 // handle specific cases |
|
83 if (errno() == UnixConstants.EACCES) |
|
84 return new AccessDeniedException(file, other, null); |
|
85 if (errno() == UnixConstants.ENOENT) |
|
86 return new NoSuchFileException(file, other, null); |
|
87 if (errno() == UnixConstants.EEXIST) |
|
88 return new FileAlreadyExistsException(file, other, null); |
|
89 |
|
90 // fallback to the more general exception |
|
91 return new FileSystemException(file, other, errorString()); |
|
92 } |
|
93 |
|
94 void rethrowAsIOException(String file) throws IOException { |
|
95 IOException x = translateToIOException(file, null); |
|
96 throw x; |
|
97 } |
|
98 |
|
99 void rethrowAsIOException(UnixPath file, UnixPath other) throws IOException { |
|
100 String a = (file == null) ? null : file.getPathForExecptionMessage(); |
|
101 String b = (other == null) ? null : other.getPathForExecptionMessage(); |
|
102 IOException x = translateToIOException(a, b); |
|
103 throw x; |
|
104 } |
|
105 |
|
106 void rethrowAsIOException(UnixPath file) throws IOException { |
|
107 rethrowAsIOException(file, null); |
|
108 } |
|
109 |
|
110 IOException asIOException(UnixPath file) { |
|
111 return translateToIOException(file.getPathForExecptionMessage(), null); |
|
112 } |
|
113 } |