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 { fp += dos.size();
dos.close();
} }
fp += dos.size();
} }
} }

View File

@@ -52,7 +52,7 @@ public class SFTPFileTransfer
throws IOException { throws IOException {
upload(source, dest, 0); upload(source, dest, 0);
} }
@Override @Override
public void upload(String source, String dest, long byteOffset) public void upload(String source, String dest, long byteOffset)
throws IOException { throws IOException {
@@ -64,7 +64,7 @@ public class SFTPFileTransfer
throws IOException { throws IOException {
download(source, dest, 0); download(source, dest, 0);
} }
@Override @Override
public void download(String source, String dest, long byteOffset) public void download(String source, String dest, long byteOffset)
throws IOException { throws IOException {
@@ -75,7 +75,7 @@ public class SFTPFileTransfer
public void upload(LocalSourceFile localFile, String remotePath) throws IOException { public void upload(LocalSourceFile localFile, String remotePath) throws IOException {
upload(localFile, remotePath, 0); upload(localFile, remotePath, 0);
} }
@Override @Override
public void upload(LocalSourceFile localFile, String remotePath, long byteOffset) throws IOException { public void upload(LocalSourceFile localFile, String remotePath, long byteOffset) throws IOException {
new Uploader(localFile, remotePath).upload(getTransferListener(), byteOffset); new Uploader(localFile, remotePath).upload(getTransferListener(), byteOffset);
@@ -85,7 +85,7 @@ public class SFTPFileTransfer
public void download(String source, LocalDestFile dest) throws IOException { public void download(String source, LocalDestFile dest) throws IOException {
download(source, dest, 0); download(source, dest, 0);
} }
@Override @Override
public void download(String source, LocalDestFile dest, long byteOffset) throws IOException { public void download(String source, LocalDestFile dest, long byteOffset) throws IOException {
final PathComponents pathComponents = engine.getPathHelper().getComponents(source); final PathComponents pathComponents = engine.getPathHelper().getComponents(source);
@@ -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;
} }
@@ -266,7 +256,7 @@ public class SFTPFileTransfer
// Starting at some offset, append // Starting at some offset, append
modes = EnumSet.of(OpenMode.WRITE, OpenMode.APPEND); modes = EnumSet.of(OpenMode.WRITE, OpenMode.APPEND);
} }
log.debug("Attempting to upload {} with offset={}", local.getName(), byteOffset); log.debug("Attempting to upload {} with offset={}", local.getName(), byteOffset);
rf = engine.open(adjusted, modes); rf = engine.open(adjusted, modes);
fis = local.getInputStream(); fis = local.getInputStream();

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");