2 * Copyright (C) 2006 Shawn Pearce <spearce@spearce.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License, version 2, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 package org
.spearce
.jgit
.lib
;
19 import java
.io
.IOException
;
20 import java
.security
.MessageDigest
;
23 * Base class for a set of loaders for different representations of Git objects.
24 * New loaders are constructed for every object.
26 public abstract class ObjectLoader
{
27 private ObjectId objectId
;
30 * @return the id of this object, possibly computed on demand
33 public ObjectId
getId() throws IOException
{
34 if (objectId
== null) {
35 final MessageDigest md
= Constants
.newMessageDigest();
36 md
.update(Constants
.encodedTypeString(getType()));
37 md
.update((byte) ' ');
38 md
.update(Constants
.encodeASCII(getSize()));
40 md
.update(getCachedBytes());
41 objectId
= ObjectId
.fromRaw(md
.digest());
47 * Set the SHA-1 id of the object handled by this loader
51 protected void setId(final ObjectId id
) {
53 throw new IllegalStateException("Id already set.");
58 * @return Git in pack object type, see {@link Constants}.
61 public abstract int getType() throws IOException
;
64 * @return size of object in bytes
67 public abstract long getSize() throws IOException
;
70 * Obtain a copy of the bytes of this object.
72 * Unlike {@link #getCachedBytes()} this method returns an array that might
73 * be modified by the caller.
75 * @return the bytes of this object.
77 * the object cannot be read.
79 public abstract byte[] getBytes() throws IOException
;
82 * Obtain a reference to the (possibly cached) bytes of this object.
84 * This method offers direct access to the internal caches, potentially
85 * saving on data copies between the internal cache and higher level code.
86 * Callers who receive this reference <b>must not</b> modify its contents.
87 * Changes (if made) will affect the cache but not the repository itself.
89 * @return the cached bytes of this object. Do not modify it.
91 * the object cannot be read.
93 public abstract byte[] getCachedBytes() throws IOException
;