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

Change-Id: I8d002ccc8f168f5891492a4c5742c82f8cb7a0b6
Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
This commit is contained in:
David Pursehouse 2018-03-13 20:41:36 +09:00
parent 48554989d3
commit 3e437da6ba
1 changed files with 21 additions and 25 deletions

View File

@ -1076,7 +1076,6 @@ private void whole(final long pos, final int type, final long sz)
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,15 +1084,16 @@ private void whole(final long pos, final int type, final long sz)
checker = BlobObjectChecker.NULL_CHECKER;
}
long cnt = 0;
while (cnt < sz) {
int r = inf.read(readBuffer);
if (r <= 0)
break;
objectDigest.update(readBuffer, 0, r);
checker.update(readBuffer, 0, r);
cnt += r;
try (InputStream inf = inflate(Source.INPUT, sz)) {
while (cnt < sz) {
int r = inf.read(readBuffer);
if (r <= 0)
break;
objectDigest.update(readBuffer, 0, r);
checker.update(readBuffer, 0, r);
cnt += r;
}
}
inf.close();
objectDigest.digest(tempObjectId);
checker.endBlob(tempObjectId);
data = null;
@ -1162,33 +1162,29 @@ private void checkObjectCollision(PackedObjectInfo obj)
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);
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()));
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()));
}
}
sz -= n;
}
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();
}
}
}