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.

ObjectStream.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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.lib;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. /**
  14. * Stream of data coming from an object loaded by {@link org.eclipse.jgit.lib.ObjectLoader}.
  15. */
  16. public abstract class ObjectStream extends InputStream {
  17. /**
  18. * Get Git object type, see {@link Constants}.
  19. *
  20. * @return Git object type, see {@link Constants}.
  21. */
  22. public abstract int getType();
  23. /**
  24. * Get total size of object in bytes
  25. *
  26. * @return total size of object in bytes
  27. */
  28. public abstract long getSize();
  29. /**
  30. * Simple stream around the cached byte array created by a loader.
  31. * <p>
  32. * ObjectLoader implementations can use this stream type when the object's
  33. * content is small enough to be accessed as a single byte array, but the
  34. * application has still requested it in stream format.
  35. */
  36. public static class SmallStream extends ObjectStream {
  37. private final int type;
  38. private final byte[] data;
  39. private int ptr;
  40. private int mark;
  41. /**
  42. * Create the stream from an existing loader's cached bytes.
  43. *
  44. * @param loader
  45. * the loader.
  46. */
  47. public SmallStream(ObjectLoader loader) {
  48. this(loader.getType(), loader.getCachedBytes());
  49. }
  50. /**
  51. * Create the stream from an existing byte array and type.
  52. *
  53. *@param type
  54. * the type constant for the object.
  55. *@param data
  56. * the fully inflated content of the object.
  57. */
  58. public SmallStream(int type, byte[] data) {
  59. this.type = type;
  60. this.data = data;
  61. }
  62. @Override
  63. public int getType() {
  64. return type;
  65. }
  66. @Override
  67. public long getSize() {
  68. return data.length;
  69. }
  70. @Override
  71. public int available() {
  72. return data.length - ptr;
  73. }
  74. @Override
  75. public long skip(long n) {
  76. int s = (int) Math.min(available(), Math.max(0, n));
  77. ptr += s;
  78. return s;
  79. }
  80. @Override
  81. public int read() {
  82. if (ptr == data.length)
  83. return -1;
  84. return data[ptr++] & 0xff;
  85. }
  86. @Override
  87. public int read(byte[] b, int off, int len) {
  88. if (ptr == data.length)
  89. return -1;
  90. int n = Math.min(available(), len);
  91. System.arraycopy(data, ptr, b, off, n);
  92. ptr += n;
  93. return n;
  94. }
  95. @Override
  96. public boolean markSupported() {
  97. return true;
  98. }
  99. @Override
  100. public void mark(int readlimit) {
  101. mark = ptr;
  102. }
  103. @Override
  104. public void reset() {
  105. ptr = mark;
  106. }
  107. }
  108. /**
  109. * Simple filter stream around another stream.
  110. * <p>
  111. * ObjectLoader implementations can use this stream type when the object's
  112. * content is available from a standard InputStream.
  113. */
  114. public static class Filter extends ObjectStream {
  115. private final int type;
  116. private final long size;
  117. private final InputStream in;
  118. /**
  119. * Create a filter stream for an object.
  120. *
  121. * @param type
  122. * the type of the object.
  123. * @param size
  124. * total size of the object, in bytes.
  125. * @param in
  126. * stream the object's raw data is available from. This
  127. * stream should be buffered with some reasonable amount of
  128. * buffering.
  129. */
  130. public Filter(int type, long size, InputStream in) {
  131. this.type = type;
  132. this.size = size;
  133. this.in = in;
  134. }
  135. @Override
  136. public int getType() {
  137. return type;
  138. }
  139. @Override
  140. public long getSize() {
  141. return size;
  142. }
  143. @Override
  144. public int available() throws IOException {
  145. return in.available();
  146. }
  147. @Override
  148. public long skip(long n) throws IOException {
  149. return in.skip(n);
  150. }
  151. @Override
  152. public int read() throws IOException {
  153. return in.read();
  154. }
  155. @Override
  156. public int read(byte[] b, int off, int len) throws IOException {
  157. return in.read(b, off, len);
  158. }
  159. @Override
  160. public boolean markSupported() {
  161. return in.markSupported();
  162. }
  163. @Override
  164. public void mark(int readlimit) {
  165. in.mark(readlimit);
  166. }
  167. @Override
  168. public void reset() throws IOException {
  169. in.reset();
  170. }
  171. @Override
  172. public void close() throws IOException {
  173. in.close();
  174. }
  175. }
  176. }