1 /* 2 * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com> 3 * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org> and others 4 * 5 * This program and the accompanying materials are made available under the 6 * terms of the Eclipse Distribution License v. 1.0 which is available at 7 * https://www.eclipse.org/org/documents/edl-v10.php. 8 * 9 * SPDX-License-Identifier: BSD-3-Clause 10 */ 11 12 package org.eclipse.jgit.internal.storage.file; 13 14 import java.io.IOException; 15 import java.util.zip.DataFormatException; 16 import java.util.zip.Inflater; 17 18 import org.eclipse.jgit.internal.storage.pack.PackOutputStream; 19 20 /** 21 * A window of data currently stored within a cache. 22 * <p> 23 * All bytes in the window can be assumed to be "immediately available", that is 24 * they are very likely already in memory, unless the operating system's memory 25 * is very low and has paged part of this process out to disk. Therefore copying 26 * bytes from a window is very inexpensive. 27 * </p> 28 */ 29 abstract class ByteWindow { 30 protected final PackFile pack; 31 32 protected final long start; 33 34 protected final long end; 35 36 /** 37 * Constructor for ByteWindow. 38 * 39 * @param p 40 * a {@link org.eclipse.jgit.internal.storage.file.PackFile}. 41 * @param s 42 * where the byte window starts in the pack file 43 * @param n 44 * size of the byte window 45 */ 46 protected ByteWindow(PackFile p, long s, int n) { 47 pack = p; 48 start = s; 49 end = start + n; 50 } 51 52 final int size() { 53 return (int) (end - start); 54 } 55 56 final boolean contains(PackFile neededFile, long neededPos) { 57 return pack == neededFile && start <= neededPos && neededPos < end; 58 } 59 60 /** 61 * Copy bytes from the window to a caller supplied buffer. 62 * 63 * @param pos 64 * offset within the file to start copying from. 65 * @param dstbuf 66 * destination buffer to copy into. 67 * @param dstoff 68 * offset within <code>dstbuf</code> to start copying into. 69 * @param cnt 70 * number of bytes to copy. This value may exceed the number of 71 * bytes remaining in the window starting at offset 72 * <code>pos</code>. 73 * @return number of bytes actually copied; this may be less than 74 * <code>cnt</code> if <code>cnt</code> exceeded the number of 75 * bytes available. 76 */ 77 final int copy(long pos, byte[] dstbuf, int dstoff, int cnt) { 78 return copy((int) (pos - start), dstbuf, dstoff, cnt); 79 } 80 81 /** 82 * Copy bytes from the window to a caller supplied buffer. 83 * 84 * @param pos 85 * offset within the window to start copying from. 86 * @param dstbuf 87 * destination buffer to copy into. 88 * @param dstoff 89 * offset within <code>dstbuf</code> to start copying into. 90 * @param cnt 91 * number of bytes to copy. This value may exceed the number of 92 * bytes remaining in the window starting at offset 93 * <code>pos</code>. 94 * @return number of bytes actually copied; this may be less than 95 * <code>cnt</code> if <code>cnt</code> exceeded the number of 96 * bytes available. 97 */ 98 protected abstract int copy(int pos, byte[] dstbuf, int dstoff, int cnt); 99 100 abstract void write(PackOutputStream out, long pos, int cnt) 101 throws IOException; 102 103 final int setInput(long pos, Inflater inf) throws DataFormatException { 104 return setInput((int) (pos - start), inf); 105 } 106 107 /** 108 * Set the input 109 * 110 * @param pos 111 * position 112 * @param inf 113 * an {@link java.util.zip.Inflater} object. 114 * @return size of the byte window 115 * @throws java.util.zip.DataFormatException 116 * if any. 117 */ 118 protected abstract int setInput(int pos, Inflater inf) 119 throws DataFormatException; 120 }