Switch jgit library to the EDL (3-clause BSD)
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / lib / ByteWindow.java
blob1f6beb81aadb53227d4b9081bfcc836fcd8171b9
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.lang.ref.ReferenceQueue;
42 import java.lang.ref.SoftReference;
43 import java.util.zip.DataFormatException;
44 import java.util.zip.Inflater;
46 /**
47 * A window of data currently stored within a cache.
48 * <p>
49 * All bytes in the window can be assumed to be "immediately available", that is
50 * they are very likely already in memory, unless the operating system's memory
51 * is very low and has paged part of this process out to disk. Therefore copying
52 * bytes from a window is very inexpensive.
53 * </p>
55 * @param <T>
56 * type of object reference used to manage the window data.
58 abstract class ByteWindow<T> extends SoftReference<T> {
59 final WindowedFile provider;
61 final int id;
63 final int size;
65 int lastAccessed;
67 final long start;
69 final long end;
71 /**
72 * Constructor for ByteWindow.
74 * @param o
75 * the WindowedFile providing data access
76 * @param pos
77 * the position in the file the data comes from.
78 * @param d
79 * an id provided by the WindowedFile. See
80 * {@link WindowCache#get(WindowCursor, WindowedFile, long)}.
81 * @param ref
82 * the object value required to perform data access.
83 * @param sz
84 * the total number of bytes in this window.
86 @SuppressWarnings("unchecked")
87 ByteWindow(final WindowedFile o, final long pos, final int d, final T ref,
88 final int sz) {
89 super(ref, (ReferenceQueue<T>) WindowCache.clearedWindowQueue);
90 provider = o;
91 size = sz;
92 id = d;
93 start = pos;
94 end = start + size;
97 final boolean contains(final WindowedFile neededFile, final long neededPos) {
98 return provider == neededFile && start <= neededPos && neededPos < end;
102 * Copy bytes from the window to a caller supplied buffer.
104 * @param ref
105 * the object value required to perform data access.
106 * @param pos
107 * offset within the file to start copying from.
108 * @param dstbuf
109 * destination buffer to copy into.
110 * @param dstoff
111 * offset within <code>dstbuf</code> to start copying into.
112 * @param cnt
113 * number of bytes to copy. This value may exceed the number of
114 * bytes remaining in the window starting at offset
115 * <code>pos</code>.
116 * @return number of bytes actually copied; this may be less than
117 * <code>cnt</code> if <code>cnt</code> exceeded the number of
118 * bytes available.
120 final int copy(T ref, long pos, byte[] dstbuf, int dstoff, int cnt) {
121 return copy(ref, (int) (pos - start), dstbuf, dstoff, cnt);
125 * Copy bytes from the window to a caller supplied buffer.
127 * @param ref
128 * the object value required to perform data access.
129 * @param pos
130 * offset within the window to start copying from.
131 * @param dstbuf
132 * destination buffer to copy into.
133 * @param dstoff
134 * offset within <code>dstbuf</code> to start copying into.
135 * @param cnt
136 * number of bytes to copy. This value may exceed the number of
137 * bytes remaining in the window starting at offset
138 * <code>pos</code>.
139 * @return number of bytes actually copied; this may be less than
140 * <code>cnt</code> if <code>cnt</code> exceeded the number of
141 * bytes available.
143 abstract int copy(T ref, int pos, byte[] dstbuf, int dstoff, int cnt);
146 * Pump bytes into the supplied inflater as input.
148 * @param ref
149 * the object value required to perform data access.
150 * @param pos
151 * offset within the window to start supplying input from.
152 * @param dstbuf
153 * destination buffer the inflater should output decompressed
154 * data to.
155 * @param dstoff
156 * current offset within <code>dstbuf</code> to inflate into.
157 * @param inf
158 * the inflater to feed input to. The caller is responsible for
159 * initializing the inflater as multiple windows may need to
160 * supply data to the same inflater to completely decompress
161 * something.
162 * @return updated <code>dstoff</code> based on the number of bytes
163 * successfully copied into <code>dstbuf</code> by
164 * <code>inf</code>. If the inflater is not yet finished then
165 * another window's data must still be supplied as input to finish
166 * decompression.
167 * @throws DataFormatException
168 * the inflater encountered an invalid chunk of data. Data
169 * stream corruption is likely.
171 final int inflate(T ref, long pos, byte[] dstbuf, int dstoff, Inflater inf)
172 throws DataFormatException {
173 return inflate(ref, (int) (pos - start), dstbuf, dstoff, inf);
177 * Pump bytes into the supplied inflater as input.
179 * @param ref
180 * the object value required to perform data access.
181 * @param pos
182 * offset within the window to start supplying input from.
183 * @param dstbuf
184 * destination buffer the inflater should output decompressed
185 * data to.
186 * @param dstoff
187 * current offset within <code>dstbuf</code> to inflate into.
188 * @param inf
189 * the inflater to feed input to. The caller is responsible for
190 * initializing the inflater as multiple windows may need to
191 * supply data to the same inflater to completely decompress
192 * something.
193 * @return updated <code>dstoff</code> based on the number of bytes
194 * successfully copied into <code>dstbuf</code> by
195 * <code>inf</code>. If the inflater is not yet finished then
196 * another window's data must still be supplied as input to finish
197 * decompression.
198 * @throws DataFormatException
199 * the inflater encountered an invalid chunk of data. Data
200 * stream corruption is likely.
202 abstract int inflate(T ref, int pos, byte[] dstbuf, int dstoff, Inflater inf)
203 throws DataFormatException;