Expose idBuffer,idOffset in AbstractTreeIterator to applications
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / treewalk / WorkingTreeIterator.java
blobacaf33dcde9dbd08aaa8eac0c75c74d7fc67a1e0
1 /*
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org.spearce.jgit.treewalk;
40 import java.io.IOException;
41 import java.io.InputStream;
42 import java.nio.ByteBuffer;
43 import java.nio.CharBuffer;
44 import java.nio.charset.CharacterCodingException;
45 import java.nio.charset.CharsetEncoder;
46 import java.security.MessageDigest;
47 import java.util.Arrays;
48 import java.util.Comparator;
50 import org.spearce.jgit.errors.CorruptObjectException;
51 import org.spearce.jgit.lib.Constants;
52 import org.spearce.jgit.lib.FileMode;
54 /**
55 * Walks a working directory tree as part of a {@link TreeWalk}.
56 * <p>
57 * Most applications will want to use the standard implementation of this
58 * iterator, {@link FileTreeIterator}, as that does all IO through the standard
59 * <code>java.io</code> package. Plugins for a Java based IDE may however wish
60 * to create their own implementations of this class to allow traversal of the
61 * IDE's project space, as well as benefit from any caching the IDE may have.
63 * @see FileTreeIterator
65 public abstract class WorkingTreeIterator extends AbstractTreeIterator {
66 /** An empty entry array, suitable for return from {@link #getEntries()}. */
67 protected static final Entry[] EOF = {};
69 /** Size we perform file IO in if we have to read and hash a file. */
70 private static final int BUFFER_SIZE = 2048;
72 /** The {@link #idBuffer()} for the current entry. */
73 private byte[] contentId;
75 /** Value of {@link #ptr} when {@link #contentId} was last populated. */
76 private int contentIdFromPtr;
78 /** Buffer used to perform {@link #contentId} computations. */
79 private byte[] contentReadBuffer;
81 /** Digest computer for {@link #contentId} computations. */
82 private MessageDigest contentDigest;
84 /** File name character encoder. */
85 private final CharsetEncoder nameEncoder;
87 /** List of entries obtained from the subclass. */
88 private Entry[] entries;
90 /** Total number of entries in {@link #entries} that are valid. */
91 private int entryCnt;
93 /** Current position within {@link #entries}. */
94 private int ptr;
96 /** Create a new iterator with no parent. */
97 protected WorkingTreeIterator() {
98 super();
99 nameEncoder = Constants.CHARSET.newEncoder();
103 * Create a new iterator with no parent and a prefix.
104 * <p>
105 * The prefix path supplied is inserted in front of all paths generated by
106 * this iterator. It is intended to be used when an iterator is being
107 * created for a subsection of an overall repository and needs to be
108 * combined with other iterators that are created to run over the entire
109 * repository namespace.
111 * @param prefix
112 * position of this iterator in the repository tree. The value
113 * may be null or the empty string to indicate the prefix is the
114 * root of the repository. A trailing slash ('/') is
115 * automatically appended if the prefix does not end in '/'.
117 protected WorkingTreeIterator(final String prefix) {
118 super(prefix);
119 nameEncoder = Constants.CHARSET.newEncoder();
123 * Create an iterator for a subtree of an existing iterator.
125 * @param p
126 * parent tree iterator.
128 protected WorkingTreeIterator(final WorkingTreeIterator p) {
129 super(p);
130 nameEncoder = p.nameEncoder;
133 @Override
134 public byte[] idBuffer() {
135 if (contentIdFromPtr == ptr - 1)
136 return contentId;
137 if (entries == EOF)
138 return zeroid;
140 switch (mode & 0170000) {
141 case 0100000: /* normal files */
142 contentIdFromPtr = ptr - 1;
143 return contentId = idBufferBlob(entries[contentIdFromPtr]);
144 case 0120000: /* symbolic links */
145 // Java does not support symbolic links, so we should not
146 // have reached this particular part of the walk code.
148 return zeroid;
149 case 0160000: /* gitlink */
150 // TODO: Support obtaining current HEAD SHA-1 from nested repository
152 return zeroid;
154 return zeroid;
157 private void initializeDigest() {
158 if (contentDigest != null)
159 return;
161 if (parent == null) {
162 contentReadBuffer = new byte[BUFFER_SIZE];
163 contentDigest = Constants.newMessageDigest();
164 } else {
165 final WorkingTreeIterator p = (WorkingTreeIterator) parent;
166 p.initializeDigest();
167 contentReadBuffer = p.contentReadBuffer;
168 contentDigest = p.contentDigest;
172 private static final byte[] digits = { '0', '1', '2', '3', '4', '5', '6',
173 '7', '8', '9' };
175 private static final byte[] hblob = Constants
176 .encodedTypeString(Constants.OBJ_BLOB);
178 private byte[] idBufferBlob(final Entry e) {
179 try {
180 final InputStream is = e.openInputStream();
181 if (is == null)
182 return zeroid;
183 try {
184 initializeDigest();
186 contentDigest.reset();
187 contentDigest.update(hblob);
188 contentDigest.update((byte) ' ');
190 final long blobLength = e.getLength();
191 long sz = blobLength;
192 if (sz == 0) {
193 contentDigest.update((byte) '0');
194 } else {
195 final int bufn = contentReadBuffer.length;
196 int p = bufn;
197 do {
198 contentReadBuffer[--p] = digits[(int) (sz % 10)];
199 sz /= 10;
200 } while (sz > 0);
201 contentDigest.update(contentReadBuffer, p, bufn - p);
203 contentDigest.update((byte) 0);
205 for (;;) {
206 final int r = is.read(contentReadBuffer);
207 if (r <= 0)
208 break;
209 contentDigest.update(contentReadBuffer, 0, r);
210 sz += r;
212 if (sz != blobLength)
213 return zeroid;
214 return contentDigest.digest();
215 } finally {
216 try {
217 is.close();
218 } catch (IOException err2) {
219 // Suppress any error related to closing an input
220 // stream. We don't care, we should not have any
221 // outstanding data to flush or anything like that.
224 } catch (IOException err) {
225 // Can't read the file? Don't report the failure either.
227 return zeroid;
231 @Override
232 public int idOffset() {
233 return 0;
236 @Override
237 public boolean eof() {
238 return entries == EOF;
241 @Override
242 public void next() throws CorruptObjectException {
243 if (entries == null)
244 loadEntries();
245 if (ptr == entryCnt) {
246 entries = EOF;
247 return;
249 if (entries == EOF)
250 return;
252 final Entry e = entries[ptr++];
253 mode = e.getMode().getBits();
255 final int nameLen = e.encodedNameLen;
256 while (pathOffset + nameLen > path.length)
257 growPath(pathOffset);
258 System.arraycopy(e.encodedName, 0, path, pathOffset, nameLen);
259 pathLen = pathOffset + nameLen;
263 * Get the byte length of this entry.
265 * @return size of this file, in bytes.
267 public long getEntryLength() {
268 return current().getLength();
272 * Get the last modified time of this entry.
274 * @return last modified time of this file, in milliseconds since the epoch
275 * (Jan 1, 1970 UTC).
277 public long getEntryLastModified() {
278 return current().getLastModified();
281 private static final Comparator<Entry> ENTRY_CMP = new Comparator<Entry>() {
282 public int compare(final Entry o1, final Entry o2) {
283 final byte[] a = o1.encodedName;
284 final byte[] b = o2.encodedName;
285 final int aLen = o1.encodedNameLen;
286 final int bLen = o2.encodedNameLen;
287 int cPos;
289 for (cPos = 0; cPos < aLen && cPos < bLen; cPos++) {
290 final int cmp = (a[cPos] & 0xff) - (b[cPos] & 0xff);
291 if (cmp != 0)
292 return cmp;
295 if (cPos < aLen) {
296 final int aj = a[cPos] & 0xff;
297 final int lastb = lastPathChar(o2);
298 if (aj < lastb)
299 return -1;
300 else if (aj > lastb)
301 return 1;
302 else if (cPos == aLen - 1)
303 return 0;
304 else
305 return -1;
308 if (cPos < bLen) {
309 final int bk = b[cPos] & 0xff;
310 final int lasta = lastPathChar(o1);
311 if (lasta < bk)
312 return -1;
313 else if (lasta > bk)
314 return 1;
315 else if (cPos == bLen - 1)
316 return 0;
317 else
318 return 1;
321 final int lasta = lastPathChar(o1);
322 final int lastb = lastPathChar(o2);
323 if (lasta < lastb)
324 return -1;
325 else if (lasta > lastb)
326 return 1;
328 if (aLen == bLen)
329 return 0;
330 else if (aLen < bLen)
331 return -1;
332 else
333 return 1;
337 static int lastPathChar(final Entry e) {
338 return e.getMode() == FileMode.TREE ? '/' : '\0';
341 private void loadEntries() throws CorruptObjectException {
342 // Filter out nulls, . and .. as these are not valid tree entries,
343 // also cache the encoded forms of the path names for efficient use
344 // later on during sorting and iteration.
346 try {
347 entries = getEntries();
348 int i, o;
350 for (i = 0, o = 0; i < entries.length; i++) {
351 final Entry e = entries[i];
352 if (e == null)
353 continue;
354 final String name = e.getName();
355 if (".".equals(name) || "..".equals(name))
356 continue;
357 if (parent == null && ".git".equals(name))
358 continue;
359 if (i != o)
360 entries[o] = e;
361 e.encodeName(nameEncoder);
362 o++;
364 entryCnt = o;
365 contentIdFromPtr = -1;
366 Arrays.sort(entries, 0, entryCnt, ENTRY_CMP);
367 } catch (CharacterCodingException e) {
368 final CorruptObjectException why;
369 why = new CorruptObjectException("Invalid file name encoding");
370 why.initCause(e);
371 throw why;
372 } catch (IOException e) {
373 final CorruptObjectException why;
374 why = new CorruptObjectException("Error reading directory");
375 why.initCause(e);
376 throw why;
381 * Obtain the current entry from this iterator.
383 * @return the currently selected entry.
385 protected Entry current() {
386 return entries[ptr - 1];
390 * Obtain an unsorted list of this iterator's contents.
391 * <p>
392 * Implementations only need to provide the unsorted contents of their lower
393 * level directory. The caller will automatically prune out ".", "..",
394 * ".git", as well as null entries as necessary, and then sort the array
395 * for iteration within a TreeWalk instance.
396 * <p>
397 * The returned array will be modified by the caller.
398 * <p>
399 * This method is only invoked once per iterator instance.
401 * @return unsorted list of the immediate children. Never null, but may be
402 * {@link #EOF} if no items can be obtained.
403 * @throws IOException
404 * reading the contents failed due to IO errors.
406 protected abstract Entry[] getEntries() throws IOException;
408 /** A single entry within a working directory tree. */
409 protected static abstract class Entry {
410 byte[] encodedName;
412 int encodedNameLen;
414 void encodeName(final CharsetEncoder enc)
415 throws CharacterCodingException {
416 final ByteBuffer b = enc.encode(CharBuffer.wrap(getName()));
417 encodedNameLen = b.limit();
418 if (b.hasArray())
419 encodedName = b.array();
420 else
421 b.get(encodedName = new byte[encodedNameLen]);
424 public String toString() {
425 return getMode().toString() + " " + getName();
429 * Get the type of this entry.
430 * <p>
431 * <b>Note: Efficient implementation required.</b>
432 * <p>
433 * The implementation of this method must be efficient. If a subclass
434 * needs to compute the value they should cache the reference within an
435 * instance member instead.
437 * @return a file mode constant from {@link FileMode}.
439 public abstract FileMode getMode();
442 * Get the byte length of this entry.
443 * <p>
444 * <b>Note: Efficient implementation required.</b>
445 * <p>
446 * The implementation of this method must be efficient. If a subclass
447 * needs to compute the value they should cache the reference within an
448 * instance member instead.
450 * @return size of this file, in bytes.
452 public abstract long getLength();
455 * Get the last modified time of this entry.
456 * <p>
457 * <b>Note: Efficient implementation required.</b>
458 * <p>
459 * The implementation of this method must be efficient. If a subclass
460 * needs to compute the value they should cache the reference within an
461 * instance member instead.
463 * @return time since the epoch (in ms) of the last change.
465 public abstract long getLastModified();
468 * Get the name of this entry within its directory.
469 * <p>
470 * Efficient implementations are not required. The caller will obtain
471 * the name only once and cache it once obtained.
473 * @return name of the entry.
475 public abstract String getName();
478 * Obtain an input stream to read the file content.
479 * <p>
480 * Efficient implementations are not required. The caller will usually
481 * obtain the stream only once per entry, if at all.
482 * <p>
483 * The input stream should not use buffering if the implementation can
484 * avoid it. The caller will buffer as necessary to perform efficient
485 * block IO operations.
486 * <p>
487 * The caller will close the stream once complete.
489 * @return a stream to read from the file.
490 * @throws IOException
491 * the file could not be opened for reading.
493 public abstract InputStream openInputStream() throws IOException;