mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-06 07:10:53 +03:00
Use try-with-resources (#999)
Co-authored-by: Jeroen van Erp <jeroen@hierynomus.com>
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ public class SFTPFileTransfer
|
||||
throws IOException {
|
||||
upload(source, dest, 0);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void upload(String source, String dest, long byteOffset)
|
||||
throws IOException {
|
||||
@@ -64,7 +64,7 @@ public class SFTPFileTransfer
|
||||
throws IOException {
|
||||
download(source, dest, 0);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void download(String source, String dest, long byteOffset)
|
||||
throws IOException {
|
||||
@@ -75,7 +75,7 @@ public class SFTPFileTransfer
|
||||
public void upload(LocalSourceFile localFile, String remotePath) throws IOException {
|
||||
upload(localFile, remotePath, 0);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void upload(LocalSourceFile localFile, String remotePath, long byteOffset) throws IOException {
|
||||
new Uploader(localFile, remotePath).upload(getTransferListener(), byteOffset);
|
||||
@@ -85,7 +85,7 @@ public class SFTPFileTransfer
|
||||
public void download(String source, LocalDestFile dest) throws IOException {
|
||||
download(source, dest, 0);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void download(String source, LocalDestFile dest, long byteOffset) throws IOException {
|
||||
final PathComponents pathComponents = engine.getPathHelper().getComponents(source);
|
||||
@@ -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;
|
||||
}
|
||||
@@ -266,7 +256,7 @@ public class SFTPFileTransfer
|
||||
// Starting at some offset, append
|
||||
modes = EnumSet.of(OpenMode.WRITE, OpenMode.APPEND);
|
||||
}
|
||||
|
||||
|
||||
log.debug("Attempting to upload {} with offset={}", local.getName(), byteOffset);
|
||||
rf = engine.open(adjusted, modes);
|
||||
fis = local.getInputStream();
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user