Fix UnpackedObjectLoader.getBytes to return a copy
[egit/charleso.git] / org.spearce.jgit / src / org / spearce / jgit / lib / ObjectLoader.java
blob87e861fdd0519e0295a830993ac3b0085690b75a
1 /*
2 * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
9 * conditions are met:
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
22 * written permission.
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;
44 /**
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;
51 /**
52 * @return the id of this object, possibly computed on demand
53 * @throws IOException
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()));
61 md.update((byte) 0);
62 md.update(getCachedBytes());
63 objectId = ObjectId.fromRaw(md.digest());
65 return objectId;
68 /**
69 * @return true if id of loaded object is already known, false otherwise.
71 protected boolean hasComputedId() {
72 return objectId != null;
75 /**
76 * Set the SHA-1 id of the object handled by this loader
78 * @param id
80 protected void setId(final ObjectId id) {
81 if (objectId != null)
82 throw new IllegalStateException("Id already set.");
83 objectId = id;
86 /**
87 * @return Git in pack object type, see {@link Constants}.
88 * @throws IOException
90 public abstract int getType() throws IOException;
92 /**
93 * @return size of object in bytes
94 * @throws IOException
96 public abstract long getSize() throws IOException;
98 /**
99 * Obtain a copy of the bytes of this object.
100 * <p>
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 final byte[] getBytes() throws IOException {
109 final byte[] data = getCachedBytes();
110 final byte[] copy = new byte[data.length];
111 System.arraycopy(data, 0, copy, 0, data.length);
112 return data;
116 * Obtain a reference to the (possibly cached) bytes of this object.
117 * <p>
118 * This method offers direct access to the internal caches, potentially
119 * saving on data copies between the internal cache and higher level code.
120 * Callers who receive this reference <b>must not</b> modify its contents.
121 * Changes (if made) will affect the cache but not the repository itself.
123 * @return the cached bytes of this object. Do not modify it.
124 * @throws IOException
125 * the object cannot be read.
127 public abstract byte[] getCachedBytes() throws IOException;
130 * @return raw object type from object header, as stored in storage (pack,
131 * loose file). This may be different from {@link #getType()} result
132 * for packs (see {@link Constants}).
133 * @throws IOException
134 * when type cannot be read from the object header.
136 public abstract int getRawType() throws IOException;
139 * @return raw size of object from object header (pack, loose file).
140 * Interpretation of this value depends on {@link #getRawType()}.
141 * @throws IOException
142 * when raw size cannot be read from the object header.
144 public abstract long getRawSize() throws IOException;