|
| 1 | +/* |
| 2 | + * Copyright © 2024 XDEV Software (https://xdev.software) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +/* |
| 17 | + * Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved. |
| 18 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 19 | + * |
| 20 | + * This code is free software; you can redistribute it and/or modify it |
| 21 | + * under the terms of the GNU General Public License version 2 only, as |
| 22 | + * published by the Free Software Foundation. Oracle designates this |
| 23 | + * particular file as subject to the "Classpath" exception as provided |
| 24 | + * by Oracle in the LICENSE file that accompanied this code. |
| 25 | + * |
| 26 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 27 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 28 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 29 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 30 | + * accompanied this code). |
| 31 | + * |
| 32 | + * You should have received a copy of the GNU General Public License version |
| 33 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 34 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 35 | + * |
| 36 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 37 | + * or visit www.oracle.com if you need additional information or have any |
| 38 | + * questions. |
| 39 | + */ |
| 40 | +package software.xdev.testcontainers.imagebuilder.transfer.java.nio.file.winntfs; |
| 41 | + |
| 42 | +import java.io.Closeable; |
| 43 | +import java.io.IOException; |
| 44 | +import java.io.UncheckedIOException; |
| 45 | +import java.nio.file.FileVisitOption; |
| 46 | +import java.nio.file.Path; |
| 47 | +import java.util.Iterator; |
| 48 | +import java.util.List; |
| 49 | +import java.util.NoSuchElementException; |
| 50 | + |
| 51 | +import software.xdev.testcontainers.imagebuilder.transfer.java.nio.file.winntfs.FileTreeWalker.Event; |
| 52 | + |
| 53 | + |
| 54 | +/** |
| 55 | + * An {@code Iterator} to iterate over the nodes of a file tree. |
| 56 | + * <p> |
| 57 | + * {@snippet lang = java: |
| 58 | + * try (FileTreeIterator iterator = new FileTreeIterator(start, maxDepth, options)) { |
| 59 | + * while (iterator.hasNext()) { |
| 60 | + * Event ev = iterator.next(); |
| 61 | + * Path path = ev.file(); |
| 62 | + * BasicFileAttributes attrs = ev.attributes(); |
| 63 | + * } |
| 64 | + * } |
| 65 | + *} |
| 66 | + */ |
| 67 | +public class FileTreeIterator implements Iterator<Event>, Closeable |
| 68 | +{ |
| 69 | + private final FileTreeWalker walker; |
| 70 | + private Event next; |
| 71 | + |
| 72 | + /** |
| 73 | + * Creates a new iterator to walk the file tree starting at the given file. |
| 74 | + * |
| 75 | + * @throws IllegalArgumentException if {@code maxDepth} is negative |
| 76 | + * @throws IOException if an I/O errors occurs opening the starting file |
| 77 | + * @throws SecurityException if the security manager denies access to the starting file |
| 78 | + * @throws NullPointerException if {@code start} or {@code options} is {@code null} or the options array |
| 79 | + * contains a {@code null} element |
| 80 | + */ |
| 81 | + FileTreeIterator(final Path start, final int maxDepth, final FileVisitOption... options) |
| 82 | + throws IOException |
| 83 | + { |
| 84 | + this.walker = new FileTreeWalker(List.of(options), maxDepth); |
| 85 | + this.next = this.walker.walk(start); |
| 86 | + assert this.next.type() == FileTreeWalker.EventType.ENTRY |
| 87 | + || this.next.type() == FileTreeWalker.EventType.START_DIRECTORY; |
| 88 | + |
| 89 | + // IOException if there a problem accessing the starting file |
| 90 | + final IOException ioe = this.next.ioeException(); |
| 91 | + if(ioe != null) |
| 92 | + { |
| 93 | + throw ioe; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private void fetchNextIfNeeded() |
| 98 | + { |
| 99 | + if(this.next == null) |
| 100 | + { |
| 101 | + FileTreeWalker.Event ev = this.walker.next(); |
| 102 | + while(ev != null) |
| 103 | + { |
| 104 | + final IOException ioe = ev.ioeException(); |
| 105 | + if(ioe != null) |
| 106 | + { |
| 107 | + throw new UncheckedIOException(ioe); |
| 108 | + } |
| 109 | + |
| 110 | + // END_DIRECTORY events are ignored |
| 111 | + if(ev.type() != FileTreeWalker.EventType.END_DIRECTORY) |
| 112 | + { |
| 113 | + this.next = ev; |
| 114 | + return; |
| 115 | + } |
| 116 | + ev = this.walker.next(); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public boolean hasNext() |
| 123 | + { |
| 124 | + if(!this.walker.isOpen()) |
| 125 | + { |
| 126 | + throw new IllegalStateException(); |
| 127 | + } |
| 128 | + this.fetchNextIfNeeded(); |
| 129 | + return this.next != null; |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public Event next() |
| 134 | + { |
| 135 | + if(!this.walker.isOpen()) |
| 136 | + { |
| 137 | + throw new IllegalStateException(); |
| 138 | + } |
| 139 | + this.fetchNextIfNeeded(); |
| 140 | + if(this.next == null) |
| 141 | + { |
| 142 | + throw new NoSuchElementException(); |
| 143 | + } |
| 144 | + final Event result = this.next; |
| 145 | + this.next = null; |
| 146 | + return result; |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public void close() |
| 151 | + { |
| 152 | + this.walker.close(); |
| 153 | + } |
| 154 | +} |
0 commit comments