2 * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
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
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
.lib
;
41 import java
.io
.IOException
;
42 import java
.security
.MessageDigest
;
45 * Base class for a set of loaders for different representations of Git objects.
46 * New loaders are constructed for every object.
48 public abstract class ObjectLoader
{
49 private ObjectId objectId
;
52 * @return the id of this object, possibly computed on demand
55 public ObjectId
getId() throws IOException
{
56 if (objectId
== null) {
57 final MessageDigest md
= Constants
.newMessageDigest();
58 md
.update(Constants
.encodedTypeString(getType()));
59 md
.update((byte) ' ');
60 md
.update(Constants
.encodeASCII(getSize()));
62 md
.update(getCachedBytes());
63 objectId
= ObjectId
.fromRaw(md
.digest());
69 * @return true if id of loaded object is already known, false otherwise.
71 protected boolean hasComputedId() {
72 return objectId
!= null;
76 * Set the SHA-1 id of the object handled by this loader
80 protected void setId(final ObjectId id
) {
82 throw new IllegalStateException("Id already set.");
87 * @return Git in pack object type, see {@link Constants}.
90 public abstract int getType() throws IOException
;
93 * @return size of object in bytes
96 public abstract long getSize() throws IOException
;
99 * Obtain a copy of the bytes of this object.
101 * Unlike {@link #getCachedBytes()} this method returns an array that might
102 * be modified by the caller.
104 * @return the bytes of this object.
105 * @throws IOException
106 * the object cannot be read.
108 public abstract byte[] getBytes() throws IOException
;
111 * Obtain a reference to the (possibly cached) bytes of this object.
113 * This method offers direct access to the internal caches, potentially
114 * saving on data copies between the internal cache and higher level code.
115 * Callers who receive this reference <b>must not</b> modify its contents.
116 * Changes (if made) will affect the cache but not the repository itself.
118 * @return the cached bytes of this object. Do not modify it.
119 * @throws IOException
120 * the object cannot be read.
122 public abstract byte[] getCachedBytes() throws IOException
;
125 * @return raw object type from object header, as stored in storage (pack,
126 * loose file). This may be different from {@link #getType()} result
127 * for packs (see {@link Constants}).
128 * @throws IOException
129 * when type cannot be read from the object header.
131 public abstract int getRawType() throws IOException
;
134 * @return raw size of object from object header (pack, loose file).
135 * Interpretation of this value depends on {@link #getRawType()}.
136 * @throws IOException
137 * when raw size cannot be read from the object header.
139 public abstract long getRawSize() throws IOException
;