Skip to content

Commit 11921e2

Browse files
simon04hierynomus
andauthored
Use try-with-resources (#999)
Co-authored-by: Jeroen van Erp <[email protected]>
1 parent 4fe6052 commit 11921e2

File tree

5 files changed

+14
-36
lines changed

5 files changed

+14
-36
lines changed

src/main/java/com/hierynomus/sshj/userauth/keyprovider/OpenSSHKeyFileUtil.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ public static File getPublicKeyFile(File privateKeyFile) {
4747
* @param publicKey Public key accessible through a {@code Reader}
4848
*/
4949
public static ParsedPubKey initPubKey(Reader publicKey) throws IOException {
50-
final BufferedReader br = new BufferedReader(publicKey);
51-
try {
50+
try (BufferedReader br = new BufferedReader(publicKey)) {
5251
String keydata;
5352
while ((keydata = br.readLine()) != null) {
5453
keydata = keydata.trim();
@@ -68,8 +67,6 @@ public static ParsedPubKey initPubKey(Reader publicKey) throws IOException {
6867
throw new IOException("Public key file is blank");
6968
} catch (Base64DecodingException err) {
7069
throw new IOException("Public key decoding failed", err);
71-
} finally {
72-
br.close();
7370
}
7471
}
7572

src/main/java/net/schmizz/sshj/sftp/RandomAccessRemoteFile.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,10 @@ public void writeShort(int v)
317317
@Override
318318
public void writeUTF(String str)
319319
throws IOException {
320-
final DataOutputStream dos = new DataOutputStream(rf.new RemoteFileOutputStream(fp));
321-
try {
320+
try (DataOutputStream dos = new DataOutputStream(rf.new RemoteFileOutputStream(fp));) {
322321
dos.writeUTF(str);
323-
} finally {
324-
dos.close();
322+
fp += dos.size();
325323
}
326-
fp += dos.size();
327324
}
328325

329326
}

src/main/java/net/schmizz/sshj/sftp/SFTPFileTransfer.java

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void upload(String source, String dest)
5252
throws IOException {
5353
upload(source, dest, 0);
5454
}
55-
55+
5656
@Override
5757
public void upload(String source, String dest, long byteOffset)
5858
throws IOException {
@@ -64,7 +64,7 @@ public void download(String source, String dest)
6464
throws IOException {
6565
download(source, dest, 0);
6666
}
67-
67+
6868
@Override
6969
public void download(String source, String dest, long byteOffset)
7070
throws IOException {
@@ -75,7 +75,7 @@ public void download(String source, String dest, long byteOffset)
7575
public void upload(LocalSourceFile localFile, String remotePath) throws IOException {
7676
upload(localFile, remotePath, 0);
7777
}
78-
78+
7979
@Override
8080
public void upload(LocalSourceFile localFile, String remotePath, long byteOffset) throws IOException {
8181
new Uploader(localFile, remotePath).upload(getTransferListener(), byteOffset);
@@ -85,7 +85,7 @@ public void upload(LocalSourceFile localFile, String remotePath, long byteOffset
8585
public void download(String source, LocalDestFile dest) throws IOException {
8686
download(source, dest, 0);
8787
}
88-
88+
8989
@Override
9090
public void download(String source, LocalDestFile dest, long byteOffset) throws IOException {
9191
final PathComponents pathComponents = engine.getPathHelper().getComponents(source);
@@ -140,12 +140,9 @@ private LocalDestFile downloadDir(final TransferListener listener,
140140
final LocalDestFile local)
141141
throws IOException {
142142
final LocalDestFile adjusted = local.getTargetDirectory(remote.getName());
143-
final RemoteDirectory rd = engine.openDir(remote.getPath());
144-
try {
143+
try (RemoteDirectory rd = engine.openDir(remote.getPath())) {
145144
for (RemoteResourceInfo rri : rd.scan(getDownloadFilter()))
146145
download(listener, rri, adjusted.getChild(rri.getName()), 0); // not supporting individual byte offsets for these files
147-
} finally {
148-
rd.close();
149146
}
150147
return adjusted;
151148
}
@@ -156,23 +153,16 @@ private LocalDestFile downloadFile(final StreamCopier.Listener listener,
156153
final long byteOffset)
157154
throws IOException {
158155
final LocalDestFile adjusted = local.getTargetFile(remote.getName());
159-
final RemoteFile rf = engine.open(remote.getPath());
160-
try {
156+
try (RemoteFile rf = engine.open(remote.getPath())) {
161157
log.debug("Attempting to download {} with offset={}", remote.getPath(), byteOffset);
162-
final RemoteFile.ReadAheadRemoteFileInputStream rfis = rf.new ReadAheadRemoteFileInputStream(16, byteOffset);
163-
final OutputStream os = adjusted.getOutputStream(byteOffset != 0);
164-
try {
158+
try (RemoteFile.ReadAheadRemoteFileInputStream rfis = rf.new ReadAheadRemoteFileInputStream(16, byteOffset);
159+
OutputStream os = adjusted.getOutputStream(byteOffset != 0)) {
165160
new StreamCopier(rfis, os, engine.getLoggerFactory())
166161
.bufSize(engine.getSubsystem().getLocalMaxPacketSize())
167162
.keepFlushing(false)
168163
.listener(listener)
169164
.copy();
170-
} finally {
171-
rfis.close();
172-
os.close();
173165
}
174-
} finally {
175-
rf.close();
176166
}
177167
return adjusted;
178168
}
@@ -266,7 +256,7 @@ private String uploadFile(final StreamCopier.Listener listener,
266256
// Starting at some offset, append
267257
modes = EnumSet.of(OpenMode.WRITE, OpenMode.APPEND);
268258
}
269-
259+
270260
log.debug("Attempting to upload {} with offset={}", local.getName(), byteOffset);
271261
rf = engine.open(adjusted, modes);
272262
fis = local.getInputStream();

src/main/java/net/schmizz/sshj/transport/verification/OpenSSHKnownHosts.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,9 @@ public List<KnownHostEntry> entries() {
187187

188188
public void write()
189189
throws IOException {
190-
final BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(khFile));
191-
try {
190+
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(khFile))) {
192191
for (KnownHostEntry entry : entries)
193192
bos.write((entry.getLine() + LS).getBytes(StandardCharsets.UTF_8));
194-
} finally {
195-
bos.close();
196193
}
197194
}
198195

src/main/java/net/schmizz/sshj/userauth/keyprovider/PuTTYKeyFile.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,8 @@ protected KeyPair readKeyPair() throws IOException {
200200

201201
protected void parseKeyPair() throws IOException {
202202
this.keyFileVersion = null;
203-
BufferedReader r = new BufferedReader(resource.getReader());
204203
// Parse the text into headers and payloads
205-
try {
204+
try (BufferedReader r = new BufferedReader(resource.getReader())) {
206205
String headerName = null;
207206
String line;
208207
while ((line = r.readLine()) != null) {
@@ -225,8 +224,6 @@ protected void parseKeyPair() throws IOException {
225224
payload.put(headerName, s);
226225
}
227226
}
228-
} finally {
229-
r.close();
230227
}
231228
if (this.keyFileVersion == null) {
232229
throw new IOException("Invalid key file format: missing \"PuTTY-User-Key-File-?\" entry");

0 commit comments

Comments
 (0)