2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
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
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
;
42 import org
.spearce
.jgit
.errors
.CorruptObjectException
;
43 import org
.spearce
.jgit
.errors
.IncorrectObjectTypeException
;
44 import org
.spearce
.jgit
.errors
.MissingObjectException
;
45 import org
.spearce
.jgit
.lib
.Constants
;
46 import org
.spearce
.jgit
.lib
.FileMode
;
47 import org
.spearce
.jgit
.lib
.ObjectId
;
48 import org
.spearce
.jgit
.lib
.ObjectLoader
;
49 import org
.spearce
.jgit
.lib
.Repository
;
51 /** Parses raw Git trees from the canonical semi-text/semi-binary format. */
52 public class CanonicalTreeParser
extends AbstractTreeIterator
{
57 /** Create a new parser. */
58 public CanonicalTreeParser() {
62 private CanonicalTreeParser(final CanonicalTreeParser p
) {
67 * Reset this parser to walk through the given tree data.
70 * the raw tree content.
72 public void reset(final byte[] treeData
) {
78 * Reset this parser to walk through the given tree.
81 * repository to load the tree data from.
83 * identity of the tree being parsed; used only in exception
84 * messages if data corruption is found.
85 * @throws MissingObjectException
86 * the object supplied is not available from the repository.
87 * @throws IncorrectObjectTypeException
88 * the object supplied as an argument is not actually a tree and
89 * cannot be parsed as though it were a tree.
91 * a loose object or pack file could not be read.
93 public void reset(final Repository repo
, final ObjectId id
)
94 throws IncorrectObjectTypeException
, IOException
{
95 final ObjectLoader ldr
= repo
.openObject(id
);
97 throw new MissingObjectException(id
, Constants
.TYPE_TREE
);
98 final byte[] subtreeData
= ldr
.getCachedBytes();
99 if (ldr
.getType() != Constants
.OBJ_TREE
)
100 throw new IncorrectObjectTypeException(id
, Constants
.TYPE_TREE
);
104 public CanonicalTreeParser
createSubtreeIterator(final Repository repo
)
105 throws IncorrectObjectTypeException
, IOException
{
106 final ObjectId id
= getEntryObjectId();
107 if (!FileMode
.TREE
.equals(mode
))
108 throw new IncorrectObjectTypeException(id
, Constants
.TYPE_TREE
);
109 final CanonicalTreeParser p
= new CanonicalTreeParser(this);
115 protected byte[] idBuffer() {
120 protected int idOffset() {
121 return rawPtr
- Constants
.OBJECT_ID_LENGTH
;
124 public boolean eof() {
128 public void next() throws CorruptObjectException
{
130 if (ptr
>= raw
.length
) {
153 } catch (ArrayIndexOutOfBoundsException e
) {
159 rawPtr
= ptr
+ Constants
.OBJECT_ID_LENGTH
;