2 * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
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
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
;
47 * A window of data currently stored within a cache.
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.
56 * type of object reference used to manage the window data.
58 abstract class ByteWindow
<T
> extends SoftReference
<T
> {
59 final WindowedFile provider
;
72 * Constructor for ByteWindow.
75 * the WindowedFile providing data access
77 * the position in the file the data comes from.
79 * an id provided by the WindowedFile. See
80 * {@link WindowCache#get(WindowCursor, WindowedFile, long)}.
82 * the object value required to perform data access.
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
,
89 super(ref
, (ReferenceQueue
<T
>) WindowCache
.clearedWindowQueue
);
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.
105 * the object value required to perform data access.
107 * offset within the file to start copying from.
109 * destination buffer to copy into.
111 * offset within <code>dstbuf</code> to start copying into.
113 * number of bytes to copy. This value may exceed the number of
114 * bytes remaining in the window starting at offset
116 * @return number of bytes actually copied; this may be less than
117 * <code>cnt</code> if <code>cnt</code> exceeded the number of
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.
128 * the object value required to perform data access.
130 * offset within the window to start copying from.
132 * destination buffer to copy into.
134 * offset within <code>dstbuf</code> to start copying into.
136 * number of bytes to copy. This value may exceed the number of
137 * bytes remaining in the window starting at offset
139 * @return number of bytes actually copied; this may be less than
140 * <code>cnt</code> if <code>cnt</code> exceeded the number of
143 abstract int copy(T ref
, int pos
, byte[] dstbuf
, int dstoff
, int cnt
);
146 * Pump bytes into the supplied inflater as input.
149 * the object value required to perform data access.
151 * offset within the window to start supplying input from.
153 * destination buffer the inflater should output decompressed
156 * current offset within <code>dstbuf</code> to inflate into.
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
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
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.
180 * the object value required to perform data access.
182 * offset within the window to start supplying input from.
184 * destination buffer the inflater should output decompressed
187 * current offset within <code>dstbuf</code> to inflate into.
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
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
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
;