Use try-with-resources (#999)

Co-authored-by: Jeroen van Erp <jeroen@hierynomus.com>
This commit is contained in:
Simon Legner
2025-03-19 09:30:01 +01:00
committed by GitHub
parent 4fe605289b
commit 11921e2d3a
5 changed files with 14 additions and 36 deletions

View File

@@ -47,8 +47,7 @@ public class OpenSSHKeyFileUtil {
* @param publicKey Public key accessible through a {@code Reader} * @param publicKey Public key accessible through a {@code Reader}
*/ */
public static ParsedPubKey initPubKey(Reader publicKey) throws IOException { public static ParsedPubKey initPubKey(Reader publicKey) throws IOException {
final BufferedReader br = new BufferedReader(publicKey); try (BufferedReader br = new BufferedReader(publicKey)) {
try {
String keydata; String keydata;
while ((keydata = br.readLine()) != null) { while ((keydata = br.readLine()) != null) {
keydata = keydata.trim(); keydata = keydata.trim();
@@ -68,8 +67,6 @@ public class OpenSSHKeyFileUtil {
throw new IOException("Public key file is blank"); throw new IOException("Public key file is blank");
} catch (Base64DecodingException err) { } catch (Base64DecodingException err) {
throw new IOException("Public key decoding failed", err); throw new IOException("Public key decoding failed", err);
} finally {
br.close();
} }
} }

View File

@@ -317,13 +317,10 @@ public class RandomAccessRemoteFile
@Override @Override
public void writeUTF(String str) public void writeUTF(String str)
throws IOException { throws IOException {
final DataOutputStream dos = new DataOutputStream(rf.new RemoteFileOutputStream(fp)); try (DataOutputStream dos = new DataOutputStream(rf.new RemoteFileOutputStream(fp));) {
try {
dos.writeUTF(str); dos.writeUTF(str);
} finally {
dos.close();
}
fp += dos.size(); fp += dos.size();
} }
}
} }

View File

@@ -140,12 +140,9 @@ public class SFTPFileTransfer
final LocalDestFile local) final LocalDestFile local)
throws IOException { throws IOException {
final LocalDestFile adjusted = local.getTargetDirectory(remote.getName()); final LocalDestFile adjusted = local.getTargetDirectory(remote.getName());
final RemoteDirectory rd = engine.openDir(remote.getPath()); try (RemoteDirectory rd = engine.openDir(remote.getPath())) {
try {
for (RemoteResourceInfo rri : rd.scan(getDownloadFilter())) for (RemoteResourceInfo rri : rd.scan(getDownloadFilter()))
download(listener, rri, adjusted.getChild(rri.getName()), 0); // not supporting individual byte offsets for these files download(listener, rri, adjusted.getChild(rri.getName()), 0); // not supporting individual byte offsets for these files
} finally {
rd.close();
} }
return adjusted; return adjusted;
} }
@@ -156,23 +153,16 @@ public class SFTPFileTransfer
final long byteOffset) final long byteOffset)
throws IOException { throws IOException {
final LocalDestFile adjusted = local.getTargetFile(remote.getName()); final LocalDestFile adjusted = local.getTargetFile(remote.getName());
final RemoteFile rf = engine.open(remote.getPath()); try (RemoteFile rf = engine.open(remote.getPath())) {
try {
log.debug("Attempting to download {} with offset={}", remote.getPath(), byteOffset); log.debug("Attempting to download {} with offset={}", remote.getPath(), byteOffset);
final RemoteFile.ReadAheadRemoteFileInputStream rfis = rf.new ReadAheadRemoteFileInputStream(16, byteOffset); try (RemoteFile.ReadAheadRemoteFileInputStream rfis = rf.new ReadAheadRemoteFileInputStream(16, byteOffset);
final OutputStream os = adjusted.getOutputStream(byteOffset != 0); OutputStream os = adjusted.getOutputStream(byteOffset != 0)) {
try {
new StreamCopier(rfis, os, engine.getLoggerFactory()) new StreamCopier(rfis, os, engine.getLoggerFactory())
.bufSize(engine.getSubsystem().getLocalMaxPacketSize()) .bufSize(engine.getSubsystem().getLocalMaxPacketSize())
.keepFlushing(false) .keepFlushing(false)
.listener(listener) .listener(listener)
.copy(); .copy();
} finally {
rfis.close();
os.close();
} }
} finally {
rf.close();
} }
return adjusted; return adjusted;
} }

View File

@@ -187,12 +187,9 @@ public class OpenSSHKnownHosts
public void write() public void write()
throws IOException { throws IOException {
final BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(khFile)); try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(khFile))) {
try {
for (KnownHostEntry entry : entries) for (KnownHostEntry entry : entries)
bos.write((entry.getLine() + LS).getBytes(StandardCharsets.UTF_8)); bos.write((entry.getLine() + LS).getBytes(StandardCharsets.UTF_8));
} finally {
bos.close();
} }
} }

View File

@@ -200,9 +200,8 @@ public class PuTTYKeyFile extends BaseFileKeyProvider {
protected void parseKeyPair() throws IOException { protected void parseKeyPair() throws IOException {
this.keyFileVersion = null; this.keyFileVersion = null;
BufferedReader r = new BufferedReader(resource.getReader());
// Parse the text into headers and payloads // Parse the text into headers and payloads
try { try (BufferedReader r = new BufferedReader(resource.getReader())) {
String headerName = null; String headerName = null;
String line; String line;
while ((line = r.readLine()) != null) { while ((line = r.readLine()) != null) {
@@ -225,8 +224,6 @@ public class PuTTYKeyFile extends BaseFileKeyProvider {
payload.put(headerName, s); payload.put(headerName, s);
} }
} }
} finally {
r.close();
} }
if (this.keyFileVersion == null) { if (this.keyFileVersion == null) {
throw new IOException("Invalid key file format: missing \"PuTTY-User-Key-File-?\" entry"); throw new IOException("Invalid key file format: missing \"PuTTY-User-Key-File-?\" entry");