Make all AbstractTreeIterator implementations bi-directional
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / treewalk / FileTreeIterator.java
blob2c71151438861072f57464145f8ce210ac928c7a
1 /*
2 * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
9 * conditions are met:
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * - Neither the name of the Git Development Community nor the
20 * names of its contributors may be used to endorse or promote
21 * products derived from this software without specific prior
22 * written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 package org.spearce.jgit.treewalk;
41 import java.io.File;
42 import java.io.FileInputStream;
43 import java.io.IOException;
44 import java.io.InputStream;
46 import org.spearce.jgit.errors.IncorrectObjectTypeException;
47 import org.spearce.jgit.lib.FileMode;
48 import org.spearce.jgit.lib.Repository;
49 import org.spearce.jgit.util.FS;
51 /**
52 * Working directory iterator for standard Java IO.
53 * <p>
54 * This iterator uses the standard <code>java.io</code> package to read the
55 * specified working directory as part of a {@link TreeWalk}.
57 public class FileTreeIterator extends WorkingTreeIterator {
58 private final File directory;
60 /**
61 * Create a new iterator to traverse the given directory and its children.
63 * @param root
64 * the starting directory. This directory should correspond to
65 * the root of the repository.
67 public FileTreeIterator(final File root) {
68 directory = root;
69 init(entries());
72 /**
73 * Create a new iterator to traverse a subdirectory.
75 * @param p
76 * the parent iterator we were created from.
77 * @param root
78 * the subdirectory. This should be a directory contained within
79 * the parent directory.
81 protected FileTreeIterator(final FileTreeIterator p, final File root) {
82 super(p);
83 directory = root;
84 init(entries());
87 @Override
88 public AbstractTreeIterator createSubtreeIterator(final Repository repo)
89 throws IncorrectObjectTypeException, IOException {
90 return new FileTreeIterator(this, ((FileEntry) current()).file);
93 private Entry[] entries() {
94 final File[] all = directory.listFiles();
95 if (all == null)
96 return EOF;
97 final Entry[] r = new Entry[all.length];
98 for (int i = 0; i < r.length; i++)
99 r[i] = new FileEntry(all[i]);
100 return r;
103 static class FileEntry extends Entry {
104 final File file;
106 private final FileMode mode;
108 private long length = -1;
110 private long lastModified;
112 FileEntry(final File f) {
113 file = f;
115 if (f.isDirectory()) {
116 if (new File(f, ".git").isDirectory())
117 mode = FileMode.GITLINK;
118 else
119 mode = FileMode.TREE;
120 } else if (FS.INSTANCE.canExecute(file))
121 mode = FileMode.EXECUTABLE_FILE;
122 else
123 mode = FileMode.REGULAR_FILE;
126 @Override
127 public FileMode getMode() {
128 return mode;
131 @Override
132 public String getName() {
133 return file.getName();
136 @Override
137 public long getLength() {
138 if (length < 0)
139 length = file.length();
140 return length;
143 @Override
144 public long getLastModified() {
145 if (lastModified == 0)
146 lastModified = file.lastModified();
147 return lastModified;
150 @Override
151 public InputStream openInputStream() throws IOException {
152 return new FileInputStream(file);