893N/A/*
5195N/A * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
893N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/Apackage sun.nio.fs;
893N/A
893N/Aimport java.nio.file.*;
893N/Aimport java.io.IOException;
893N/A
893N/A/**
893N/A * Internal exception thrown by native methods when error detected.
893N/A */
893N/A
893N/Aclass UnixException extends Exception {
893N/A static final long serialVersionUID = 7227016794320723218L;
893N/A
893N/A private int errno;
893N/A private String msg;
893N/A
893N/A UnixException(int errno) {
893N/A this.errno = errno;
893N/A this.msg = null;
893N/A }
893N/A
893N/A UnixException(String msg) {
893N/A this.errno = 0;
893N/A this.msg = msg;
893N/A }
893N/A
893N/A int errno() {
893N/A return errno;
893N/A }
893N/A
893N/A void setError(int errno) {
893N/A this.errno = errno;
893N/A this.msg = null;
893N/A }
893N/A
893N/A String errorString() {
893N/A if (msg != null) {
893N/A return msg;
893N/A } else {
893N/A return new String(UnixNativeDispatcher.strerror(errno()));
893N/A }
893N/A }
893N/A
893N/A @Override
893N/A public String getMessage() {
893N/A return errorString();
893N/A }
893N/A
893N/A /**
893N/A * Map well known errors to specific exceptions where possible; otherwise
893N/A * return more general FileSystemException.
893N/A */
893N/A private IOException translateToIOException(String file, String other) {
893N/A // created with message rather than errno
893N/A if (msg != null)
893N/A return new IOException(msg);
893N/A
893N/A // handle specific cases
893N/A if (errno() == UnixConstants.EACCES)
893N/A return new AccessDeniedException(file, other, null);
893N/A if (errno() == UnixConstants.ENOENT)
893N/A return new NoSuchFileException(file, other, null);
893N/A if (errno() == UnixConstants.EEXIST)
893N/A return new FileAlreadyExistsException(file, other, null);
893N/A
893N/A // fallback to the more general exception
893N/A return new FileSystemException(file, other, errorString());
893N/A }
893N/A
893N/A void rethrowAsIOException(String file) throws IOException {
893N/A IOException x = translateToIOException(file, null);
893N/A throw x;
893N/A }
893N/A
893N/A void rethrowAsIOException(UnixPath file, UnixPath other) throws IOException {
5195N/A String a = (file == null) ? null : file.getPathForExceptionMessage();
5195N/A String b = (other == null) ? null : other.getPathForExceptionMessage();
893N/A IOException x = translateToIOException(a, b);
893N/A throw x;
893N/A }
893N/A
893N/A void rethrowAsIOException(UnixPath file) throws IOException {
893N/A rethrowAsIOException(file, null);
893N/A }
893N/A
893N/A IOException asIOException(UnixPath file) {
5195N/A return translateToIOException(file.getPathForExceptionMessage(), null);
893N/A }
893N/A}