IO: use JDK convenience methods
The benefit is that certain InputStreams can override the default implementation for performance reasons. Change-Id: I4c924157ec0f0ec63b0eca7cdbdc9325af24cab6
This commit is contained in:
parent
055a7c20e0
commit
0d5e017612
|
@ -66,19 +66,7 @@ public static final byte[] readFully(File path)
|
||||||
public static final byte[] readSome(File path, int limit)
|
public static final byte[] readSome(File path, int limit)
|
||||||
throws FileNotFoundException, IOException {
|
throws FileNotFoundException, IOException {
|
||||||
try (SilentFileInputStream in = new SilentFileInputStream(path)) {
|
try (SilentFileInputStream in = new SilentFileInputStream(path)) {
|
||||||
byte[] buf = new byte[limit];
|
return in.readNBytes(limit);
|
||||||
int cnt = 0;
|
|
||||||
for (;;) {
|
|
||||||
int n = in.read(buf, cnt, buf.length - cnt);
|
|
||||||
if (n <= 0)
|
|
||||||
break;
|
|
||||||
cnt += n;
|
|
||||||
}
|
|
||||||
if (cnt == buf.length)
|
|
||||||
return buf;
|
|
||||||
byte[] res = new byte[cnt];
|
|
||||||
System.arraycopy(buf, 0, res, 0, cnt);
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,37 +87,10 @@ public static final byte[] readSome(File path, int limit)
|
||||||
public static final byte[] readFully(File path, int max)
|
public static final byte[] readFully(File path, int max)
|
||||||
throws FileNotFoundException, IOException {
|
throws FileNotFoundException, IOException {
|
||||||
try (SilentFileInputStream in = new SilentFileInputStream(path)) {
|
try (SilentFileInputStream in = new SilentFileInputStream(path)) {
|
||||||
long sz = Math.max(path.length(), 1);
|
byte[] buf = in.readNBytes(max);
|
||||||
if (sz > max)
|
if (in.read() != -1) {
|
||||||
throw new IOException(MessageFormat.format(
|
throw new IOException(MessageFormat.format(
|
||||||
JGitText.get().fileIsTooLarge, path));
|
JGitText.get().fileIsTooLarge, path));
|
||||||
|
|
||||||
byte[] buf = new byte[(int) sz];
|
|
||||||
int valid = 0;
|
|
||||||
for (;;) {
|
|
||||||
if (buf.length == valid) {
|
|
||||||
if (buf.length == max) {
|
|
||||||
int next = in.read();
|
|
||||||
if (next < 0)
|
|
||||||
break;
|
|
||||||
|
|
||||||
throw new IOException(MessageFormat.format(
|
|
||||||
JGitText.get().fileIsTooLarge, path));
|
|
||||||
}
|
|
||||||
|
|
||||||
byte[] nb = new byte[Math.min(buf.length * 2, max)];
|
|
||||||
System.arraycopy(buf, 0, nb, 0, valid);
|
|
||||||
buf = nb;
|
|
||||||
}
|
|
||||||
int n = in.read(buf, valid, buf.length - valid);
|
|
||||||
if (n < 0)
|
|
||||||
break;
|
|
||||||
valid += n;
|
|
||||||
}
|
|
||||||
if (valid < buf.length) {
|
|
||||||
byte[] nb = new byte[valid];
|
|
||||||
System.arraycopy(buf, 0, nb, 0, valid);
|
|
||||||
buf = nb;
|
|
||||||
}
|
}
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
@ -157,26 +118,7 @@ public static final byte[] readFully(File path, int max)
|
||||||
*/
|
*/
|
||||||
public static ByteBuffer readWholeStream(InputStream in, int sizeHint)
|
public static ByteBuffer readWholeStream(InputStream in, int sizeHint)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
byte[] out = new byte[sizeHint];
|
return ByteBuffer.wrap(in.readAllBytes());
|
||||||
int pos = 0;
|
|
||||||
while (pos < out.length) {
|
|
||||||
int read = in.read(out, pos, out.length - pos);
|
|
||||||
if (read < 0)
|
|
||||||
return ByteBuffer.wrap(out, 0, pos);
|
|
||||||
pos += read;
|
|
||||||
}
|
|
||||||
|
|
||||||
int last = in.read();
|
|
||||||
if (last < 0)
|
|
||||||
return ByteBuffer.wrap(out, 0, pos);
|
|
||||||
|
|
||||||
try (TemporaryBuffer.Heap tmp = new TemporaryBuffer.Heap(
|
|
||||||
Integer.MAX_VALUE)) {
|
|
||||||
tmp.write(out);
|
|
||||||
tmp.write(last);
|
|
||||||
tmp.copy(in);
|
|
||||||
return ByteBuffer.wrap(tmp.toByteArray());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -197,13 +139,9 @@ public static ByteBuffer readWholeStream(InputStream in, int sizeHint)
|
||||||
*/
|
*/
|
||||||
public static void readFully(final InputStream fd, final byte[] dst,
|
public static void readFully(final InputStream fd, final byte[] dst,
|
||||||
int off, int len) throws IOException {
|
int off, int len) throws IOException {
|
||||||
while (len > 0) {
|
int read = fd.readNBytes(dst, off, len);
|
||||||
final int r = fd.read(dst, off, len);
|
if (read != len)
|
||||||
if (r <= 0)
|
throw new EOFException(JGitText.get().shortReadOfBlock);
|
||||||
throw new EOFException(JGitText.get().shortReadOfBlock);
|
|
||||||
off += r;
|
|
||||||
len -= r;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -271,14 +209,7 @@ public static int read(ReadableByteChannel channel, byte[] dst, int off,
|
||||||
*/
|
*/
|
||||||
public static int readFully(InputStream fd, byte[] dst, int off)
|
public static int readFully(InputStream fd, byte[] dst, int off)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
int r;
|
return fd.readNBytes(dst, off, dst.length - off);
|
||||||
int len = 0;
|
|
||||||
while (off < dst.length
|
|
||||||
&& (r = fd.read(dst, off, dst.length - off)) >= 0) {
|
|
||||||
off += r;
|
|
||||||
len += r;
|
|
||||||
}
|
|
||||||
return len;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -300,6 +231,7 @@ public static int readFully(InputStream fd, byte[] dst, int off)
|
||||||
*/
|
*/
|
||||||
public static void skipFully(InputStream fd, long toSkip)
|
public static void skipFully(InputStream fd, long toSkip)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
// same as fd.skipNBytes(toSkip) of JDK 12;
|
||||||
while (toSkip > 0) {
|
while (toSkip > 0) {
|
||||||
final long r = fd.skip(toSkip);
|
final long r = fd.skip(toSkip);
|
||||||
if (r <= 0)
|
if (r <= 0)
|
||||||
|
|
Loading…
Reference in New Issue