DfsBlock.java

  1. /*
  2.  * Copyright (C) 2008-2011, 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. package org.eclipse.jgit.internal.storage.dfs;

  13. import java.io.IOException;
  14. import java.nio.ByteBuffer;
  15. import java.util.zip.CRC32;
  16. import java.util.zip.DataFormatException;
  17. import java.util.zip.Inflater;

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

  19. /** A cached slice of a {@link BlockBasedFile}. */
  20. final class DfsBlock {
  21.     final DfsStreamKey stream;
  22.     final long start;
  23.     final long end;
  24.     private final byte[] block;

  25.     DfsBlock(DfsStreamKey p, long pos, byte[] buf) {
  26.         stream = p;
  27.         start = pos;
  28.         end = pos + buf.length;
  29.         block = buf;
  30.     }

  31.     int size() {
  32.         return block.length;
  33.     }

  34.     ByteBuffer zeroCopyByteBuffer(int n) {
  35.         ByteBuffer b = ByteBuffer.wrap(block);
  36.         b.position(n);
  37.         return b;
  38.     }

  39.     boolean contains(DfsStreamKey want, long pos) {
  40.         return stream.equals(want) && start <= pos && pos < end;
  41.     }

  42.     int copy(long pos, byte[] dstbuf, int dstoff, int cnt) {
  43.         int ptr = (int) (pos - start);
  44.         return copy(ptr, dstbuf, dstoff, cnt);
  45.     }

  46.     int copy(int p, byte[] b, int o, int n) {
  47.         n = Math.min(block.length - p, n);
  48.         System.arraycopy(block, p, b, o, n);
  49.         return n;
  50.     }

  51.     int setInput(long pos, Inflater inf) throws DataFormatException {
  52.         int ptr = (int) (pos - start);
  53.         int cnt = block.length - ptr;
  54.         if (cnt <= 0) {
  55.             throw new DataFormatException(cnt + " bytes to inflate:" //$NON-NLS-1$
  56.                     + " at pos=" + pos //$NON-NLS-1$
  57.                     + "; block.start=" + start //$NON-NLS-1$
  58.                     + "; ptr=" + ptr //$NON-NLS-1$
  59.                     + "; block.length=" + block.length); //$NON-NLS-1$
  60.         }
  61.         inf.setInput(block, ptr, cnt);
  62.         return cnt;
  63.     }

  64.     void crc32(CRC32 out, long pos, int cnt) {
  65.         int ptr = (int) (pos - start);
  66.         out.update(block, ptr, cnt);
  67.     }

  68.     void write(PackOutputStream out, long pos, int cnt)
  69.             throws IOException {
  70.         out.write(block, (int) (pos - start), cnt);
  71.     }

  72.     void check(Inflater inf, byte[] tmp, long pos, int cnt)
  73.             throws DataFormatException {
  74.         // Unlike inflate() above the exact byte count is known by the caller.
  75.         // Push all of it in a single invocation to avoid unnecessary loops.
  76.         //
  77.         inf.setInput(block, (int) (pos - start), cnt);
  78.         while (inf.inflate(tmp, 0, tmp.length) > 0)
  79.             continue;
  80.     }
  81. }