1
2
3
4
5
6
7
8
9
10
11
12
13 package org.eclipse.jgit.util;
14
15 import java.io.EOFException;
16 import java.io.File;
17 import java.io.FileNotFoundException;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.Reader;
21 import java.nio.ByteBuffer;
22 import java.nio.channels.ReadableByteChannel;
23 import java.text.MessageFormat;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.eclipse.jgit.internal.JGitText;
28 import org.eclipse.jgit.util.io.SilentFileInputStream;
29
30
31
32
33 public class IO {
34
35
36
37
38
39
40
41
42
43
44
45
46 public static final byte[] readFully(File path)
47 throws FileNotFoundException, IOException {
48 return IO.readFully(path, Integer.MAX_VALUE);
49 }
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 public static final byte[] readSome(File path, int limit)
67 throws FileNotFoundException, IOException {
68 try (SilentFileInputStreamtStream.html#SilentFileInputStream">SilentFileInputStream in = new SilentFileInputStream(path)) {
69 byte[] buf = new byte[limit];
70 int cnt = 0;
71 for (;;) {
72 int n = in.read(buf, cnt, buf.length - cnt);
73 if (n <= 0)
74 break;
75 cnt += n;
76 }
77 if (cnt == buf.length)
78 return buf;
79 byte[] res = new byte[cnt];
80 System.arraycopy(buf, 0, res, 0, cnt);
81 return res;
82 }
83 }
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99 public static final byte[] readFully(File path, int max)
100 throws FileNotFoundException, IOException {
101 try (SilentFileInputStreamtStream.html#SilentFileInputStream">SilentFileInputStream in = new SilentFileInputStream(path)) {
102 long sz = Math.max(path.length(), 1);
103 if (sz > max)
104 throw new IOException(MessageFormat.format(
105 JGitText.get().fileIsTooLarge, path));
106
107 byte[] buf = new byte[(int) sz];
108 int valid = 0;
109 for (;;) {
110 if (buf.length == valid) {
111 if (buf.length == max) {
112 int next = in.read();
113 if (next < 0)
114 break;
115
116 throw new IOException(MessageFormat.format(
117 JGitText.get().fileIsTooLarge, path));
118 }
119
120 byte[] nb = new byte[Math.min(buf.length * 2, max)];
121 System.arraycopy(buf, 0, nb, 0, valid);
122 buf = nb;
123 }
124 int n = in.read(buf, valid, buf.length - valid);
125 if (n < 0)
126 break;
127 valid += n;
128 }
129 if (valid < buf.length) {
130 byte[] nb = new byte[valid];
131 System.arraycopy(buf, 0, nb, 0, valid);
132 buf = nb;
133 }
134 return buf;
135 }
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158 public static ByteBuffer readWholeStream(InputStream in, int sizeHint)
159 throws IOException {
160 byte[] out = new byte[sizeHint];
161 int pos = 0;
162 while (pos < out.length) {
163 int read = in.read(out, pos, out.length - pos);
164 if (read < 0)
165 return ByteBuffer.wrap(out, 0, pos);
166 pos += read;
167 }
168
169 int last = in.read();
170 if (last < 0)
171 return ByteBuffer.wrap(out, 0, pos);
172
173 try (TemporaryBuffer.Heap tmp = new TemporaryBuffer.Heap(
174 Integer.MAX_VALUE)) {
175 tmp.write(out);
176 tmp.write(last);
177 tmp.copy(in);
178 return ByteBuffer.wrap(tmp.toByteArray());
179 }
180 }
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198 public static void readFully(final InputStream fd, final byte[] dst,
199 int off, int len) throws IOException {
200 while (len > 0) {
201 final int r = fd.read(dst, off, len);
202 if (r <= 0)
203 throw new EOFException(JGitText.get().shortReadOfBlock);
204 off += r;
205 len -= r;
206 }
207 }
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224 public static int read(ReadableByteChannel channel, byte[] dst, int off,
225 int len) throws IOException {
226 if (len == 0)
227 return 0;
228 int cnt = 0;
229 while (0 < len) {
230 int r = channel.read(ByteBuffer.wrap(dst, off, len));
231 if (r <= 0)
232 break;
233 off += r;
234 len -= r;
235 cnt += r;
236 }
237 return cnt != 0 ? cnt : -1;
238 }
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253 public static int readFully(InputStream fd, byte[] dst, int off)
254 throws IOException {
255 int r;
256 int len = 0;
257 while ((r = fd.read(dst, off, dst.length - off)) >= 0
258 && len < dst.length) {
259 off += r;
260 len += r;
261 }
262 return len;
263 }
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282 public static void skipFully(InputStream fd, long toSkip)
283 throws IOException {
284 while (toSkip > 0) {
285 final long r = fd.skip(toSkip);
286 if (r <= 0)
287 throw new EOFException(JGitText.get().shortSkipOfBlock);
288 toSkip -= r;
289 }
290 }
291
292
293
294
295
296
297
298
299
300 public static List<String> readLines(String s) {
301 List<String> l = new ArrayList<>();
302 StringBuilder sb = new StringBuilder();
303 for (int i = 0; i < s.length(); i++) {
304 char c = s.charAt(i);
305 if (c == '\n') {
306 l.add(sb.toString());
307 sb.setLength(0);
308 continue;
309 }
310 if (c == '\r') {
311 if (i + 1 < s.length()) {
312 c = s.charAt(++i);
313 l.add(sb.toString());
314 sb.setLength(0);
315 if (c != '\n') {
316 sb.append(c);
317 }
318 continue;
319 }
320
321 l.add(sb.toString());
322 break;
323 }
324 sb.append(c);
325 }
326 l.add(sb.toString());
327 return l;
328 }
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346 public static String readLine(Reader in, int sizeHint) throws IOException {
347 if (in.markSupported()) {
348 if (sizeHint <= 0) {
349 sizeHint = 1024;
350 }
351 StringBuilder sb = new StringBuilder(sizeHint);
352 char[] buf = new char[sizeHint];
353 while (true) {
354 in.mark(sizeHint);
355 int n = in.read(buf);
356 if (n < 0) {
357 in.reset();
358 return sb.toString();
359 }
360 for (int i = 0; i < n; i++) {
361 if (buf[i] == '\n') {
362 resetAndSkipFully(in, ++i);
363 sb.append(buf, 0, i);
364 return sb.toString();
365 }
366 }
367 if (n > 0) {
368 sb.append(buf, 0, n);
369 }
370 resetAndSkipFully(in, n);
371 }
372 }
373 StringBuilder buf = sizeHint > 0 ? new StringBuilder(sizeHint)
374 : new StringBuilder();
375 int i;
376 while ((i = in.read()) != -1) {
377 char c = (char) i;
378 buf.append(c);
379 if (c == '\n') {
380 break;
381 }
382 }
383 return buf.toString();
384 }
385
386 private static void resetAndSkipFully(Reader fd, long toSkip) throws IOException {
387 fd.reset();
388 while (toSkip > 0) {
389 long r = fd.skip(toSkip);
390 if (r <= 0) {
391 throw new EOFException(JGitText.get().shortSkipOfBlock);
392 }
393 toSkip -= r;
394 }
395 }
396
397 private IO() {
398
399 }
400 }