View Javadoc
1   /*
2    * Copyright (C) 2008-2009, Google Inc.
3    * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
4    * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org> and others
5    *
6    * This program and the accompanying materials are made available under the
7    * terms of the Eclipse Distribution License v. 1.0 which is available at
8    * https://www.eclipse.org/org/documents/edl-v10.php.
9    *
10   * SPDX-License-Identifier: BSD-3-Clause
11   */
12  
13  package org.eclipse.jgit.internal.storage.file;
14  
15  import java.io.IOException;
16  import java.util.zip.CRC32;
17  import java.util.zip.DataFormatException;
18  import java.util.zip.Inflater;
19  
20  import org.eclipse.jgit.internal.storage.pack.PackOutputStream;
21  
22  /**
23   * A {@link ByteWindow} with an underlying byte array for storage.
24   */
25  final class ByteArrayWindow extends ByteWindow {
26  	private final byte[] array;
27  
28  	ByteArrayWindow(PackFile pack, long o, byte[] b) {
29  		super(pack, o, b.length);
30  		array = b;
31  	}
32  
33  	/** {@inheritDoc} */
34  	@Override
35  	protected int copy(int p, byte[] b, int o, int n) {
36  		n = Math.min(array.length - p, n);
37  		System.arraycopy(array, p, b, o, n);
38  		return n;
39  	}
40  
41  	/** {@inheritDoc} */
42  	@Override
43  	protected int setInput(int pos, Inflater inf)
44  			throws DataFormatException {
45  		int n = array.length - pos;
46  		inf.setInput(array, pos, n);
47  		return n;
48  	}
49  
50  	void crc32(CRC32 out, long pos, int cnt) {
51  		out.update(array, (int) (pos - start), cnt);
52  	}
53  
54  	@Override
55  	void write(PackOutputStream out, long pos, int cnt)
56  			throws IOException {
57  		int ptr = (int) (pos - start);
58  		out.write(array, ptr, cnt);
59  	}
60  
61  	void check(Inflater inf, byte[] tmp, long pos, int cnt)
62  			throws DataFormatException {
63  		inf.setInput(array, (int) (pos - start), cnt);
64  		while (inf.inflate(tmp, 0, tmp.length) > 0)
65  			continue;
66  	}
67  }