ByteArrayWindow.java

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

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

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

  18. /**
  19.  * A {@link ByteWindow} with an underlying byte array for storage.
  20.  */
  21. final class ByteArrayWindow extends ByteWindow {
  22.     private final byte[] array;

  23.     ByteArrayWindow(Pack pack, long o, byte[] b) {
  24.         super(pack, o, b.length);
  25.         array = b;
  26.     }

  27.     /** {@inheritDoc} */
  28.     @Override
  29.     protected int copy(int p, byte[] b, int o, int n) {
  30.         n = Math.min(array.length - p, n);
  31.         System.arraycopy(array, p, b, o, n);
  32.         return n;
  33.     }

  34.     /** {@inheritDoc} */
  35.     @Override
  36.     protected int setInput(int pos, Inflater inf)
  37.             throws DataFormatException {
  38.         int n = array.length - pos;
  39.         inf.setInput(array, pos, n);
  40.         return n;
  41.     }

  42.     void crc32(CRC32 out, long pos, int cnt) {
  43.         out.update(array, (int) (pos - start), cnt);
  44.     }

  45.     @Override
  46.     void write(PackOutputStream out, long pos, int cnt)
  47.             throws IOException {
  48.         int ptr = (int) (pos - start);
  49.         out.write(array, ptr, cnt);
  50.     }

  51.     void check(Inflater inf, byte[] tmp, long pos, int cnt)
  52.             throws DataFormatException {
  53.         inf.setInput(array, (int) (pos - start), cnt);
  54.         while (inf.inflate(tmp, 0, tmp.length) > 0)
  55.             continue;
  56.     }
  57. }