Make PGPUtil.pipeFileContents use buffer and not leak file handle
@@ -282,7 +282,7 @@ public static void writeFileToLiteralData(
{
PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator();
OutputStream pOut = lData.open(out, fileType, file);
- pipeFileContents(file, pOut, 4096);
+ pipeFileContents(file, pOut, new byte[4096]);
}
/**
@@ -305,15 +305,15 @@ public static void writeFileToLiteralData(
{
PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator();
OutputStream pOut = lData.open(out, fileType, file.getName(), new Date(file.lastModified()), buffer);
- pipeFileContents(file, pOut, buffer.length);
+ pipeFileContents(file, pOut, buffer);
}
- private static void pipeFileContents(File file, OutputStream pOut, int bufSize)
+ private static void pipeFileContents(File file, OutputStream pOut, byte[] buf)
throws IOException
{
FileInputStream in = new FileInputStream(file);
- byte[] buf = new byte[bufSize];
-
+ try
+ {
int len;
while ((len = in.read(buf)) > 0)
{
@@ -321,7 +321,9 @@ private static void pipeFileContents(File file, OutputStream pOut, int bufSize)
}
pOut.close();
- in.close();
+ } finally {
+ try { in.close(); } catch (IOException ignored) { /* */ }
+ }
}
private static final int READ_AHEAD = 60;
