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

View File

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

View File

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

View File

@@ -187,12 +187,9 @@ public class OpenSSHKnownHosts
public void write()
throws IOException {
final BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(khFile));
try {
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(khFile))) {
for (KnownHostEntry entry : entries)
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 {
this.keyFileVersion = null;
BufferedReader r = new BufferedReader(resource.getReader());
// Parse the text into headers and payloads
try {
try (BufferedReader r = new BufferedReader(resource.getReader())) {
String headerName = null;
String line;
while ((line = r.readLine()) != null) {
@@ -225,8 +224,6 @@ public class PuTTYKeyFile extends BaseFileKeyProvider {
payload.put(headerName, s);
}
}
} finally {
r.close();
}
if (this.keyFileVersion == null) {
throw new IOException("Invalid key file format: missing \"PuTTY-User-Key-File-?\" entry");