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