1 /*
2  * Copyright (c) 2010, 2011, 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 import java.util.ConcurrentModificationException;
29 import java.util.Objects;
30 import java.io.IOException;
31 import java.io.ObjectInputStream;
32 import java.io.InvalidObjectException;
33 
34 /**
35  * Runtime exception thrown if an I/O error is encountered when iterating over
36  * the entries in a directory. The I/O error is retrieved as an {@link
37  * IOException} using the {@link #getCause() getCause()} method.
38  *
39  * @since 1.7
40  * @see DirectoryStream
41  */
42 
43 public final class DirectoryIteratorException
44     extends ConcurrentModificationException
45 {
46     private static final long serialVersionUID = -6012699886086212874L;
47 
48     /**
49      * Constructs an instance of this class.
50      *
51      * @param   cause
52      *          the {@code IOException} that caused the directory iteration
53      *          to fail
54      *
55      * @throws  NullPointerException
56      *          if the cause is {@code null}
57      */
DirectoryIteratorException(IOException cause)58     public DirectoryIteratorException(IOException cause) {
59         super(Objects.requireNonNull(cause));
60     }
61 
62     /**
63      * Returns the cause of this exception.
64      *
65      * @return  the cause
66      */
67     @Override
getCause()68     public IOException getCause() {
69         return (IOException)super.getCause();
70     }
71 
72     /**
73      * Called to read the object from a stream.
74      *
75      * @throws  InvalidObjectException
76      *          if the object is invalid or has a cause that is not
77      *          an {@code IOException}
78      */
readObject(ObjectInputStream s)79     private void readObject(ObjectInputStream s)
80         throws IOException, ClassNotFoundException
81     {
82         s.defaultReadObject();
83         Throwable cause = super.getCause();
84         if (!(cause instanceof IOException))
85             throw new InvalidObjectException("Cause must be an IOException");
86     }
87 }
88