Completely rewrote object data access to improve performance.
[egit.git] / org.spearce.jgit / src / org / spearce / jgit / lib / ByteBufferWindow.java
blob0faacbb6739a794089dd49f59fc59dc0d3df391c
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 Lesser General Public
6 * License, version 2.1, 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 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser 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.nio.ByteBuffer;
20 import java.util.zip.DataFormatException;
21 import java.util.zip.Inflater;
23 public final class ByteBufferWindow extends ByteWindow {
24 private final ByteBuffer buffer;
26 public ByteBufferWindow(final WindowProvider o, final int d,
27 final ByteBuffer b) {
28 super(o, d);
29 buffer = b;
32 public final int copy(final int p, final byte[] b, final int o, int n) {
33 final ByteBuffer s = buffer.slice();
34 s.position(p);
35 n = Math.min(s.remaining(), n);
36 s.get(b, o, n);
37 return n;
40 public int inflate(final int pos, final byte[] b, int o, final Inflater inf)
41 throws DataFormatException {
42 final byte[] tmp = new byte[512];
43 final ByteBuffer s = buffer.slice();
44 s.position(pos);
45 while (s.remaining() > 0 && !inf.finished()) {
46 if (inf.needsInput()) {
47 final int n = Math.min(s.remaining(), tmp.length);
48 s.get(tmp, 0, n);
49 inf.setInput(tmp, 0, n);
51 o += inf.inflate(b, o, b.length - o);
53 while (!inf.finished() && !inf.needsInput())
54 o += inf.inflate(b, o, b.length - o);
55 return o;
58 public int size() {
59 return buffer.capacity();