You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LargePackedWholeObject.java 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (C) 2010, Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.internal.storage.file;
  11. import java.io.BufferedInputStream;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.util.zip.InflaterInputStream;
  15. import org.eclipse.jgit.errors.LargeObjectException;
  16. import org.eclipse.jgit.errors.MissingObjectException;
  17. import org.eclipse.jgit.lib.ObjectId;
  18. import org.eclipse.jgit.lib.ObjectLoader;
  19. import org.eclipse.jgit.lib.ObjectStream;
  20. class LargePackedWholeObject extends ObjectLoader {
  21. private final int type;
  22. private final long size;
  23. private final long objectOffset;
  24. private final int headerLength;
  25. private final Pack pack;
  26. private final FileObjectDatabase db;
  27. LargePackedWholeObject(int type, long size, long objectOffset,
  28. int headerLength, Pack pack, FileObjectDatabase db) {
  29. this.type = type;
  30. this.size = size;
  31. this.objectOffset = objectOffset;
  32. this.headerLength = headerLength;
  33. this.pack = pack;
  34. this.db = db;
  35. }
  36. /** {@inheritDoc} */
  37. @Override
  38. public int getType() {
  39. return type;
  40. }
  41. /** {@inheritDoc} */
  42. @Override
  43. public long getSize() {
  44. return size;
  45. }
  46. /** {@inheritDoc} */
  47. @Override
  48. public boolean isLarge() {
  49. return true;
  50. }
  51. /** {@inheritDoc} */
  52. @Override
  53. public byte[] getCachedBytes() throws LargeObjectException {
  54. try {
  55. throw new LargeObjectException(getObjectId());
  56. } catch (IOException cannotObtainId) {
  57. throw new LargeObjectException(cannotObtainId);
  58. }
  59. }
  60. /** {@inheritDoc} */
  61. @Override
  62. public ObjectStream openStream() throws MissingObjectException, IOException {
  63. WindowCursor wc = new WindowCursor(db);
  64. InputStream in;
  65. try {
  66. in = new PackInputStream(pack, objectOffset + headerLength, wc);
  67. } catch (IOException packGone) {
  68. // If the pack file cannot be pinned into the cursor, it
  69. // probably was repacked recently. Go find the object
  70. // again and open the stream from that location instead.
  71. //
  72. return wc.open(getObjectId(), type).openStream();
  73. }
  74. in = new BufferedInputStream( //
  75. new InflaterInputStream( //
  76. in, //
  77. wc.inflater(), //
  78. 8192), //
  79. 8192);
  80. return new ObjectStream.Filter(type, size, in);
  81. }
  82. private ObjectId getObjectId() throws IOException {
  83. return pack.findObjectForOffset(objectOffset);
  84. }
  85. }