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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. /** Stream of data coming from an object loaded by {@link ObjectLoader}. */
  47. public abstract class ObjectStream extends InputStream {
  48. /** @return Git object type, see {@link Constants}. */
  49. public abstract int getType();
  50. /** @return total size of object in bytes */
  51. public abstract long getSize();
  52. /**
  53. * Simple stream around the cached byte array created by a loader.
  54. * <p>
  55. * ObjectLoader implementations can use this stream type when the object's
  56. * content is small enough to be accessed as a single byte array, but the
  57. * application has still requested it in stream format.
  58. */
  59. public static class SmallStream extends ObjectStream {
  60. private final int type;
  61. private final byte[] data;
  62. private int ptr;
  63. private int mark;
  64. /**
  65. * Create the stream from an existing loader's cached bytes.
  66. *
  67. * @param loader
  68. * the loader.
  69. */
  70. public SmallStream(ObjectLoader loader) {
  71. this(loader.getType(), loader.getCachedBytes());
  72. }
  73. /**
  74. * Create the stream from an existing byte array and type.
  75. *
  76. *@param type
  77. * the type constant for the object.
  78. *@param data
  79. * the fully inflated content of the object.
  80. */
  81. public SmallStream(int type, byte[] data) {
  82. this.type = type;
  83. this.data = data;
  84. }
  85. @Override
  86. public int getType() {
  87. return type;
  88. }
  89. @Override
  90. public long getSize() {
  91. return data.length;
  92. }
  93. @Override
  94. public int available() {
  95. return data.length - ptr;
  96. }
  97. @Override
  98. public long skip(long n) {
  99. int s = (int) Math.min(available(), Math.max(0, n));
  100. ptr += s;
  101. return s;
  102. }
  103. @Override
  104. public int read() {
  105. if (ptr == data.length)
  106. return -1;
  107. return data[ptr++] & 0xff;
  108. }
  109. @Override
  110. public int read(byte[] b, int off, int len) {
  111. if (ptr == data.length)
  112. return -1;
  113. int n = Math.min(available(), len);
  114. System.arraycopy(data, ptr, b, off, n);
  115. ptr += n;
  116. return n;
  117. }
  118. @Override
  119. public boolean markSupported() {
  120. return true;
  121. }
  122. @Override
  123. public void mark(int readlimit) {
  124. mark = ptr;
  125. }
  126. @Override
  127. public void reset() {
  128. ptr = mark;
  129. }
  130. }
  131. /**
  132. * Simple filter stream around another stream.
  133. * <p>
  134. * ObjectLoader implementations can use this stream type when the object's
  135. * content is available from a standard InputStream.
  136. */
  137. public static class Filter extends ObjectStream {
  138. private final int type;
  139. private final long size;
  140. private final InputStream in;
  141. /**
  142. * Create a filter stream for an object.
  143. *
  144. * @param type
  145. * the type of the object.
  146. * @param size
  147. * total size of the object, in bytes.
  148. * @param in
  149. * stream the object's raw data is available from. This
  150. * stream should be buffered with some reasonable amount of
  151. * buffering.
  152. */
  153. public Filter(int type, long size, InputStream in) {
  154. this.type = type;
  155. this.size = size;
  156. this.in = in;
  157. }
  158. @Override
  159. public int getType() {
  160. return type;
  161. }
  162. @Override
  163. public long getSize() {
  164. return size;
  165. }
  166. @Override
  167. public int available() throws IOException {
  168. return in.available();
  169. }
  170. @Override
  171. public long skip(long n) throws IOException {
  172. return in.skip(n);
  173. }
  174. @Override
  175. public int read() throws IOException {
  176. return in.read();
  177. }
  178. @Override
  179. public int read(byte[] b, int off, int len) throws IOException {
  180. return in.read(b, off, len);
  181. }
  182. @Override
  183. public boolean markSupported() {
  184. return in.markSupported();
  185. }
  186. @Override
  187. public void mark(int readlimit) {
  188. in.mark(readlimit);
  189. }
  190. @Override
  191. public void reset() throws IOException {
  192. in.reset();
  193. }
  194. @Override
  195. public void close() throws IOException {
  196. in.close();
  197. }
  198. }
  199. }