Add tests to increase coverage of WindowCache
[jgit.git] / org.spearce.jgit / src / org / spearce / jgit / lib / PackedObjectLoader.java
blob29ec6d3101e02f067f9a797848fc0e256b50cd02
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.io.OutputStream;
44 /**
45 * Base class for a set of object loader classes for packed objects.
47 abstract class PackedObjectLoader extends ObjectLoader {
48 protected final PackFile pack;
50 protected final long dataOffset;
52 protected final long objectOffset;
54 protected int objectType;
56 protected int objectSize;
58 protected byte[] cachedBytes;
60 PackedObjectLoader(final PackFile pr, final long dataOffset,
61 final long objectOffset) {
62 pack = pr;
63 this.dataOffset = dataOffset;
64 this.objectOffset = objectOffset;
67 /**
68 * Force this object to be loaded into memory and pinned in this loader.
69 * <p>
70 * Once materialized, subsequent get operations for the following methods
71 * will always succeed without raising an exception, as all information is
72 * pinned in memory by this loader instance.
73 * <ul>
74 * <li>{@link #getType()}</li>
75 * <li>{@link #getSize()}</li>
76 * <li>{@link #getBytes()}, {@link #getCachedBytes}</li>
77 * <li>{@link #getRawSize()}</li>
78 * <li>{@link #getRawType()}</li>
79 * </ul>
81 * @param curs
82 * temporary thread storage during data access.
83 * @throws IOException
84 * the object cannot be read.
86 public abstract void materialize(WindowCursor curs) throws IOException;
88 public final int getType() {
89 return objectType;
92 public final long getSize() {
93 return objectSize;
96 @Override
97 public final byte[] getCachedBytes() {
98 return cachedBytes;
102 * @return offset of object header within pack file
104 public final long getObjectOffset() {
105 return objectOffset;
109 * @return offset of object data within pack file
111 public final long getDataOffset() {
112 return dataOffset;
116 * Peg the pack file open to support data copying.
117 * <p>
118 * Applications trying to copy raw pack data should ensure the pack stays
119 * open and available throughout the entire copy. To do that use:
121 * <pre>
122 * loader.beginCopyRawData();
123 * try {
124 * loader.copyRawData(out, tmpbuf, curs);
125 * } finally {
126 * loader.endCopyRawData();
128 * </pre>
130 * @throws IOException
131 * this loader contains stale information and cannot be used.
132 * The most likely cause is the underlying pack file has been
133 * deleted, and the object has moved to another pack file.
135 public void beginCopyRawData() throws IOException {
136 pack.beginCopyRawData();
140 * Copy raw object representation from storage to provided output stream.
141 * <p>
142 * Copied data doesn't include object header. User must provide temporary
143 * buffer used during copying by underlying I/O layer.
144 * </p>
146 * @param out
147 * output stream when data is copied. No buffering is guaranteed.
148 * @param buf
149 * temporary buffer used during copying. Recommended size is at
150 * least few kB.
151 * @param curs
152 * temporary thread storage during data access.
153 * @throws IOException
154 * when the object cannot be read.
155 * @see #beginCopyRawData()
157 public void copyRawData(OutputStream out, byte buf[], WindowCursor curs)
158 throws IOException {
159 pack.copyRawData(this, out, buf, curs);
162 /** Release resources after {@link #beginCopyRawData()}. */
163 public void endCopyRawData() {
164 pack.endCopyRawData();
168 * @return true if this loader is capable of fast raw-data copying basing on
169 * compressed data checksum; false if raw-data copying needs
170 * uncompressing and compressing data
171 * @throws IOException
172 * the index file format cannot be determined.
174 public boolean supportsFastCopyRawData() throws IOException {
175 return pack.supportsFastCopyRawData();
179 * @return id of delta base object for this object representation. null if
180 * object is not stored as delta.
181 * @throws IOException
182 * when delta base cannot read.
184 public abstract ObjectId getDeltaBase() throws IOException;