Always use a single WindowCache for the entire JVM
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / lib / ByteWindow.java
blob9d098deb701c7a5ca22fe27f9de70351e01fb0c4
1 /*
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;
24 /**
25 * A window of data currently stored within a cache.
26 * <p>
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.
31 * </p>
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;
38 final int id;
40 final int size;
42 int lastAccessed;
44 /**
45 * Constructor for ByteWindow.
47 * @param o
48 * the WindowedFile providing data access
49 * @param d
50 * an id provided by the WindowedFile. See
51 * {@link WindowCache#get(WindowCursor, WindowedFile, int)}.
52 * @param ref
53 * the object value required to perform data access.
54 * @param sz
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);
60 provider = o;
61 size = sz;
62 id = d;
65 /**
66 * Copy bytes from the window to a caller supplied buffer.
68 * @param ref
69 * the object value required to perform data access.
70 * @param pos
71 * offset within the window to start copying from.
72 * @param dstbuf
73 * destination buffer to copy into.
74 * @param dstoff
75 * offset within <code>dstbuf</code> to start copying into.
76 * @param cnt
77 * number of bytes to copy. This value may exceed the number of
78 * bytes remaining in the window starting at offset
79 * <code>pos</code>.
80 * @return number of bytes actually copied; this may be less than
81 * <code>cnt</code> if <code>cnt</code> exceeded the number of
82 * bytes available.
84 abstract int copy(T ref, int pos, byte[] dstbuf, int dstoff, int cnt);
86 /**
87 * Pump bytes into the supplied inflater as input.
89 * @param ref
90 * the object value required to perform data access.
91 * @param pos
92 * offset within the window to start supplying input from.
93 * @param dstbuf
94 * destination buffer the inflater should output decompressed
95 * data to.
96 * @param dstoff
97 * current offset within <code>dstbuf</code> to inflate into.
98 * @param inf
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
102 * something.
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
107 * decompression.
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;