Always use a single WindowCache for the entire JVM
[egit/imyousuf.git] / org.spearce.jgit / src / org / spearce / jgit / lib / ObjectLoader.java
blob8f4258355dc33a98581c01a35e0cf52713e6b19f
1 /*
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;
22 /**
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;
29 /**
30 * @return the id of this object, possibly computed on demand
31 * @throws IOException
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()));
39 md.update((byte) 0);
40 md.update(getCachedBytes());
41 objectId = ObjectId.fromRaw(md.digest());
43 return objectId;
46 /**
47 * Set the SHA-1 id of the object handled by this loader
49 * @param id
51 protected void setId(final ObjectId id) {
52 if (objectId != null)
53 throw new IllegalStateException("Id already set.");
54 objectId = id;
57 /**
58 * @return Git in pack object type, see {@link Constants}.
59 * @throws IOException
61 public abstract int getType() throws IOException;
63 /**
64 * @return size of object in bytes
65 * @throws IOException
67 public abstract long getSize() throws IOException;
69 /**
70 * Obtain a copy of the bytes of this object.
71 * <p>
72 * Unlike {@link #getCachedBytes()} this method returns an array that might
73 * be modified by the caller.
75 * @return the bytes of this object.
76 * @throws IOException
77 * the object cannot be read.
79 public abstract byte[] getBytes() throws IOException;
81 /**
82 * Obtain a reference to the (possibly cached) bytes of this object.
83 * <p>
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.
90 * @throws IOException
91 * the object cannot be read.
93 public abstract byte[] getCachedBytes() throws IOException;