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 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.lib;
  44. import java.io.IOException;
  45. import java.io.InputStream;
  46. /**
  47. * Stream of data coming from an object loaded by {@link org.eclipse.jgit.lib.ObjectLoader}.
  48. */
  49. public abstract class ObjectStream extends InputStream {
  50. /**
  51. * Get Git object type, see {@link Constants}.
  52. *
  53. * @return Git object type, see {@link Constants}.
  54. */
  55. public abstract int getType();
  56. /**
  57. * Get total size of object in bytes
  58. *
  59. * @return total size of object in bytes
  60. */
  61. public abstract long getSize();
  62. /**
  63. * Simple stream around the cached byte array created by a loader.
  64. * <p>
  65. * ObjectLoader implementations can use this stream type when the object's
  66. * content is small enough to be accessed as a single byte array, but the
  67. * application has still requested it in stream format.
  68. */
  69. public static class SmallStream extends ObjectStream {
  70. private final int type;
  71. private final byte[] data;
  72. private int ptr;
  73. private int mark;
  74. /**
  75. * Create the stream from an existing loader's cached bytes.
  76. *
  77. * @param loader
  78. * the loader.
  79. */
  80. public SmallStream(ObjectLoader loader) {
  81. this(loader.getType(), loader.getCachedBytes());
  82. }
  83. /**
  84. * Create the stream from an existing byte array and type.
  85. *
  86. *@param type
  87. * the type constant for the object.
  88. *@param data
  89. * the fully inflated content of the object.
  90. */
  91. public SmallStream(int type, byte[] data) {
  92. this.type = type;
  93. this.data = data;
  94. }
  95. @Override
  96. public int getType() {
  97. return type;
  98. }
  99. @Override
  100. public long getSize() {
  101. return data.length;
  102. }
  103. @Override
  104. public int available() {
  105. return data.length - ptr;
  106. }
  107. @Override
  108. public long skip(long n) {
  109. int s = (int) Math.min(available(), Math.max(0, n));
  110. ptr += s;
  111. return s;
  112. }
  113. @Override
  114. public int read() {
  115. if (ptr == data.length)
  116. return -1;
  117. return data[ptr++] & 0xff;
  118. }
  119. @Override
  120. public int read(byte[] b, int off, int len) {
  121. if (ptr == data.length)
  122. return -1;
  123. int n = Math.min(available(), len);
  124. System.arraycopy(data, ptr, b, off, n);
  125. ptr += n;
  126. return n;
  127. }
  128. @Override
  129. public boolean markSupported() {
  130. return true;
  131. }
  132. @Override
  133. public void mark(int readlimit) {
  134. mark = ptr;
  135. }
  136. @Override
  137. public void reset() {
  138. ptr = mark;
  139. }
  140. }
  141. /**
  142. * Simple filter stream around another stream.
  143. * <p>
  144. * ObjectLoader implementations can use this stream type when the object's
  145. * content is available from a standard InputStream.
  146. */
  147. public static class Filter extends ObjectStream {
  148. private final int type;
  149. private final long size;
  150. private final InputStream in;
  151. /**
  152. * Create a filter stream for an object.
  153. *
  154. * @param type
  155. * the type of the object.
  156. * @param size
  157. * total size of the object, in bytes.
  158. * @param in
  159. * stream the object's raw data is available from. This
  160. * stream should be buffered with some reasonable amount of
  161. * buffering.
  162. */
  163. public Filter(int type, long size, InputStream in) {
  164. this.type = type;
  165. this.size = size;
  166. this.in = in;
  167. }
  168. @Override
  169. public int getType() {
  170. return type;
  171. }
  172. @Override
  173. public long getSize() {
  174. return size;
  175. }
  176. @Override
  177. public int available() throws IOException {
  178. return in.available();
  179. }
  180. @Override
  181. public long skip(long n) throws IOException {
  182. return in.skip(n);
  183. }
  184. @Override
  185. public int read() throws IOException {
  186. return in.read();
  187. }
  188. @Override
  189. public int read(byte[] b, int off, int len) throws IOException {
  190. return in.read(b, off, len);
  191. }
  192. @Override
  193. public boolean markSupported() {
  194. return in.markSupported();
  195. }
  196. @Override
  197. public void mark(int readlimit) {
  198. in.mark(readlimit);
  199. }
  200. @Override
  201. public void reset() throws IOException {
  202. in.reset();
  203. }
  204. @Override
  205. public void close() throws IOException {
  206. in.close();
  207. }
  208. }
  209. }