Browse Source

PackParser: Refactor to open InputStream in try-with-resource

Change-Id: I8d002ccc8f168f5891492a4c5742c82f8cb7a0b6
Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
stable-5.0
David Pursehouse 7 years ago
parent
commit
3e437da6ba
  1. 18
      org.eclipse.jgit/src/org/eclipse/jgit/transport/PackParser.java

18
org.eclipse.jgit/src/org/eclipse/jgit/transport/PackParser.java

@ -1076,7 +1076,6 @@ public abstract class PackParser {
final byte[] data;
if (type == Constants.OBJ_BLOB) {
byte[] readBuffer = buffer();
InputStream inf = inflate(Source.INPUT, sz);
BlobObjectChecker checker = null;
if (objCheck != null) {
checker = objCheck.newBlobObjectChecker();
@ -1085,6 +1084,7 @@ public abstract class PackParser {
checker = BlobObjectChecker.NULL_CHECKER;
}
long cnt = 0;
try (InputStream inf = inflate(Source.INPUT, sz)) {
while (cnt < sz) {
int r = inf.read(readBuffer);
if (r <= 0)
@ -1093,7 +1093,7 @@ public abstract class PackParser {
checker.update(readBuffer, 0, r);
cnt += r;
}
inf.close();
}
objectDigest.digest(tempObjectId);
checker.endBlob(tempObjectId);
data = null;
@ -1162,33 +1162,29 @@ public abstract class PackParser {
final byte[] readBuffer = buffer();
final byte[] curBuffer = new byte[readBuffer.length];
long sz = info.size;
InputStream pck = null;
try (ObjectStream cur = readCurs.open(obj, info.type).openStream()) {
if (cur.getSize() != sz) {
throw new IOException(MessageFormat.format(
JGitText.get().collisionOn, obj.name()));
}
pck = inflate(Source.DATABASE, sz);
try (InputStream pck = inflate(Source.DATABASE, sz)) {
while (0 < sz) {
int n = (int) Math.min(readBuffer.length, sz);
IO.readFully(cur, curBuffer, 0, n);
IO.readFully(pck, readBuffer, 0, n);
for (int i = 0; i < n; i++) {
if (curBuffer[i] != readBuffer[i]) {
throw new IOException(MessageFormat.format(JGitText
.get().collisionOn, obj.name()));
throw new IOException(MessageFormat.format(
JGitText.get().collisionOn, obj.name()));
}
}
sz -= n;
}
}
} catch (MissingObjectException notLocal) {
// This is OK, we don't have a copy of the object locally
// but the API throws when we try to read it as usually its
// but the API throws when we try to read it as usually it's
// an error to read something that doesn't exist.
} finally {
if (pck != null) {
pck.close();
}
}
}

Loading…
Cancel
Save