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
.lang
.ref
.ReferenceQueue
;
20 import java
.lang
.ref
.SoftReference
;
21 import java
.util
.zip
.DataFormatException
;
22 import java
.util
.zip
.Inflater
;
25 * A window of data currently stored within a cache.
27 * All bytes in the window can be assumed to be "immediately available", that is
28 * they are very likely already in memory, unless the operating system's memory
29 * is very low and has paged part of this process out to disk. Therefore copying
30 * bytes from a window is very inexpensive.
33 * @param <T> type of object reference used to manage the window data.
35 abstract class ByteWindow
<T
> extends SoftReference
<T
> {
36 final WindowedFile provider
;
45 * Constructor for ByteWindow.
48 * the WindowedFile providing data access
50 * an id provided by the WindowedFile. See
51 * {@link WindowCache#get(WindowCursor, WindowedFile, int)}.
53 * the object value required to perform data access.
55 * the total number of bytes in this window.
57 @SuppressWarnings("unchecked")
58 ByteWindow(final WindowedFile o
, final int d
, final T ref
, final int sz
) {
59 super(ref
, (ReferenceQueue
<T
>)WindowCache
.clearedWindowQueue
);
66 * Copy bytes from the window to a caller supplied buffer.
69 * the object value required to perform data access.
71 * offset within the window to start copying from.
73 * destination buffer to copy into.
75 * offset within <code>dstbuf</code> to start copying into.
77 * number of bytes to copy. This value may exceed the number of
78 * bytes remaining in the window starting at offset
80 * @return number of bytes actually copied; this may be less than
81 * <code>cnt</code> if <code>cnt</code> exceeded the number of
84 abstract int copy(T ref
, int pos
, byte[] dstbuf
, int dstoff
, int cnt
);
87 * Pump bytes into the supplied inflater as input.
90 * the object value required to perform data access.
92 * offset within the window to start supplying input from.
94 * destination buffer the inflater should output decompressed
97 * current offset within <code>dstbuf</code> to inflate into.
99 * the inflater to feed input to. The caller is responsible for
100 * initializing the inflater as multiple windows may need to
101 * supply data to the same inflater to completely decompress
103 * @return updated <code>dstoff</code> based on the number of bytes
104 * successfully copied into <code>dstbuf</code> by
105 * <code>inf</code>. If the inflater is not yet finished then
106 * another window's data must still be supplied as input to finish
108 * @throws DataFormatException
109 * the inflater encountered an invalid chunk of data. Data stream
110 * corruption is likely.
112 abstract int inflate(T ref
, int pos
, byte[] dstbuf
, int dstoff
, Inflater inf
)
113 throws DataFormatException
;