ByteWindow.java

  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. package org.eclipse.jgit.internal.storage.file;

  12. import java.io.IOException;
  13. import java.util.zip.DataFormatException;
  14. import java.util.zip.Inflater;

  15. import org.eclipse.jgit.internal.storage.pack.PackOutputStream;

  16. /**
  17.  * A window of data currently stored within a cache.
  18.  * <p>
  19.  * All bytes in the window can be assumed to be "immediately available", that is
  20.  * they are very likely already in memory, unless the operating system's memory
  21.  * is very low and has paged part of this process out to disk. Therefore copying
  22.  * bytes from a window is very inexpensive.
  23.  * </p>
  24.  */
  25. abstract class ByteWindow {
  26.     protected final Pack pack;

  27.     protected final long start;

  28.     protected final long end;

  29.     /**
  30.      * Constructor for ByteWindow.
  31.      *
  32.      * @param p
  33.      *            a {@link org.eclipse.jgit.internal.storage.file.Pack}.
  34.      * @param s
  35.      *            where the byte window starts in the pack file
  36.      * @param n
  37.      *            size of the byte window
  38.      */
  39.     protected ByteWindow(Pack p, long s, int n) {
  40.         pack = p;
  41.         start = s;
  42.         end = start + n;
  43.     }

  44.     final int size() {
  45.         return (int) (end - start);
  46.     }

  47.     final boolean contains(Pack neededPack, long neededPos) {
  48.         return pack == neededPack && start <= neededPos && neededPos < end;
  49.     }

  50.     /**
  51.      * Copy bytes from the window to a caller supplied buffer.
  52.      *
  53.      * @param pos
  54.      *            offset within the file to start copying from.
  55.      * @param dstbuf
  56.      *            destination buffer to copy into.
  57.      * @param dstoff
  58.      *            offset within <code>dstbuf</code> to start copying into.
  59.      * @param cnt
  60.      *            number of bytes to copy. This value may exceed the number of
  61.      *            bytes remaining in the window starting at offset
  62.      *            <code>pos</code>.
  63.      * @return number of bytes actually copied; this may be less than
  64.      *         <code>cnt</code> if <code>cnt</code> exceeded the number of
  65.      *         bytes available.
  66.      */
  67.     final int copy(long pos, byte[] dstbuf, int dstoff, int cnt) {
  68.         return copy((int) (pos - start), dstbuf, dstoff, cnt);
  69.     }

  70.     /**
  71.      * Copy bytes from the window to a caller supplied buffer.
  72.      *
  73.      * @param pos
  74.      *            offset within the window to start copying from.
  75.      * @param dstbuf
  76.      *            destination buffer to copy into.
  77.      * @param dstoff
  78.      *            offset within <code>dstbuf</code> to start copying into.
  79.      * @param cnt
  80.      *            number of bytes to copy. This value may exceed the number of
  81.      *            bytes remaining in the window starting at offset
  82.      *            <code>pos</code>.
  83.      * @return number of bytes actually copied; this may be less than
  84.      *         <code>cnt</code> if <code>cnt</code> exceeded the number of
  85.      *         bytes available.
  86.      */
  87.     protected abstract int copy(int pos, byte[] dstbuf, int dstoff, int cnt);

  88.     abstract void write(PackOutputStream out, long pos, int cnt)
  89.             throws IOException;

  90.     final int setInput(long pos, Inflater inf) throws DataFormatException {
  91.         return setInput((int) (pos - start), inf);
  92.     }

  93.     /**
  94.      * Set the input
  95.      *
  96.      * @param pos
  97.      *            position
  98.      * @param inf
  99.      *            an {@link java.util.zip.Inflater} object.
  100.      * @return size of the byte window
  101.      * @throws java.util.zip.DataFormatException
  102.      *             if any.
  103.      */
  104.     protected abstract int setInput(int pos, Inflater inf)
  105.             throws DataFormatException;
  106. }