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