893N/A/*
2362N/A * Copyright (c) 2008, 2009, 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/Aimport static sun.nio.fs.WindowsConstants.*;
893N/A
893N/A/**
893N/A * Internal exception thrown when a Win32 calls fails.
893N/A */
893N/A
893N/Aclass WindowsException extends Exception {
893N/A static final long serialVersionUID = 2765039493083748820L;
893N/A
893N/A private int lastError;
893N/A private String msg;
893N/A
893N/A WindowsException(int lastError) {
893N/A this.lastError = lastError;
893N/A this.msg = null;
893N/A }
893N/A
893N/A WindowsException(String msg) {
893N/A this.lastError = 0;
893N/A this.msg = msg;
893N/A }
893N/A
893N/A int lastError() {
893N/A return lastError;
893N/A }
893N/A
893N/A String errorString() {
893N/A if (msg == null) {
893N/A msg = WindowsNativeDispatcher.FormatMessage(lastError);
893N/A if (msg == null) {
893N/A msg = "Unknown error: 0x" + Integer.toHexString(lastError);
893N/A }
893N/A }
893N/A return msg;
893N/A }
893N/A
893N/A @Override
893N/A public String getMessage() {
893N/A return errorString();
893N/A }
893N/A
893N/A private IOException translateToIOException(String file, String other) {
893N/A // not created with last error
893N/A if (lastError() == 0)
893N/A return new IOException(errorString());
893N/A
893N/A // handle specific cases
893N/A if (lastError() == ERROR_FILE_NOT_FOUND || lastError() == ERROR_PATH_NOT_FOUND)
893N/A return new NoSuchFileException(file, other, null);
893N/A if (lastError() == ERROR_FILE_EXISTS || lastError() == ERROR_ALREADY_EXISTS)
893N/A return new FileAlreadyExistsException(file, other, null);
893N/A if (lastError() == ERROR_ACCESS_DENIED)
893N/A return new AccessDeniedException(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(WindowsPath file, WindowsPath other) throws IOException {
893N/A String a = (file == null) ? null : file.getPathForExceptionMessage();
893N/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(WindowsPath file) throws IOException {
893N/A rethrowAsIOException(file, null);
893N/A }
893N/A
893N/A IOException asIOException(WindowsPath file) {
893N/A return translateToIOException(file.getPathForExceptionMessage(), null);
893N/A }
893N/A
893N/A}