Browse Source

ObjectDirectoryInserter: Open FileOutputStream in try-with-resource

Change-Id: Icc569aeefdc79baee5dfb71fb34d881c561dcf52
Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
tags/v5.8.0.202006091008-r
David Pursehouse 4 years ago
parent
commit
c0c7f445f4

+ 16
- 16
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/ObjectDirectoryInserter.java View File

} }
} }


@SuppressWarnings("resource" /* java 7 */)
private File toTemp(final SHA1 md, final int type, long len, private File toTemp(final SHA1 md, final int type, long len,
final InputStream is) throws IOException { final InputStream is) throws IOException {
boolean delete = true; boolean delete = true;
File tmp = newTempFile(); File tmp = newTempFile();
try {
FileOutputStream fOut = new FileOutputStream(tmp);
try (FileOutputStream fOut = new FileOutputStream(tmp)) {
try { try {
OutputStream out = fOut; OutputStream out = fOut;
if (config.getFSyncObjectFiles())
if (config.getFSyncObjectFiles()) {
out = Channels.newOutputStream(fOut.getChannel()); out = Channels.newOutputStream(fOut.getChannel());
}
DeflaterOutputStream cOut = compress(out); DeflaterOutputStream cOut = compress(out);
SHA1OutputStream dOut = new SHA1OutputStream(cOut, md); SHA1OutputStream dOut = new SHA1OutputStream(cOut, md);
writeHeader(dOut, type, len); writeHeader(dOut, type, len);
final byte[] buf = buffer(); final byte[] buf = buffer();
while (len > 0) { while (len > 0) {
int n = is.read(buf, 0, (int) Math.min(len, buf.length)); int n = is.read(buf, 0, (int) Math.min(len, buf.length));
if (n <= 0)
if (n <= 0) {
throw shortInput(len); throw shortInput(len);
}
dOut.write(buf, 0, n); dOut.write(buf, 0, n);
len -= n; len -= n;
} }
dOut.flush(); dOut.flush();
cOut.finish(); cOut.finish();
} finally { } finally {
if (config.getFSyncObjectFiles())
if (config.getFSyncObjectFiles()) {
fOut.getChannel().force(true); fOut.getChannel().force(true);
fOut.close();
}
} }


delete = false; delete = false;
return tmp; return tmp;
} finally { } finally {
if (delete)
if (delete) {
FileUtils.delete(tmp, FileUtils.RETRY); FileUtils.delete(tmp, FileUtils.RETRY);
}
} }
} }


@SuppressWarnings("resource" /* java 7 */)
private File toTemp(final int type, final byte[] buf, final int pos, private File toTemp(final int type, final byte[] buf, final int pos,
final int len) throws IOException { final int len) throws IOException {
boolean delete = true; boolean delete = true;
File tmp = newTempFile(); File tmp = newTempFile();
try {
FileOutputStream fOut = new FileOutputStream(tmp);
try (FileOutputStream fOut = new FileOutputStream(tmp)) {
try { try {
OutputStream out = fOut; OutputStream out = fOut;
if (config.getFSyncObjectFiles())
if (config.getFSyncObjectFiles()) {
out = Channels.newOutputStream(fOut.getChannel()); out = Channels.newOutputStream(fOut.getChannel());
}
DeflaterOutputStream cOut = compress(out); DeflaterOutputStream cOut = compress(out);
writeHeader(cOut, type, len); writeHeader(cOut, type, len);
cOut.write(buf, pos, len); cOut.write(buf, pos, len);
cOut.finish(); cOut.finish();
} finally { } finally {
if (config.getFSyncObjectFiles())
if (config.getFSyncObjectFiles()) {
fOut.getChannel().force(true); fOut.getChannel().force(true);
fOut.close();
}
} }

delete = false; delete = false;
return tmp; return tmp;
} finally { } finally {
if (delete)
if (delete) {
FileUtils.delete(tmp, FileUtils.RETRY); FileUtils.delete(tmp, FileUtils.RETRY);
}
} }
} }



Loading…
Cancel
Save