Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public static File getPublicKeyFile(File privateKeyFile) {
* @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();
Expand All @@ -68,8 +67,6 @@ public static ParsedPubKey initPubKey(Reader publicKey) throws IOException {
throw new IOException("Public key file is blank");
} catch (Base64DecodingException err) {
throw new IOException("Public key decoding failed", err);
} finally {
br.close();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,10 @@ public void writeShort(int v)
@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();
}

}
28 changes: 9 additions & 19 deletions src/main/java/net/schmizz/sshj/sftp/SFTPFileTransfer.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void upload(String source, String dest)
throws IOException {
upload(source, dest, 0);
}

@Override
public void upload(String source, String dest, long byteOffset)
throws IOException {
Expand All @@ -64,7 +64,7 @@ public void download(String source, String dest)
throws IOException {
download(source, dest, 0);
}

@Override
public void download(String source, String dest, long byteOffset)
throws IOException {
Expand All @@ -75,7 +75,7 @@ public void download(String source, String dest, long byteOffset)
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);
Expand All @@ -85,7 +85,7 @@ public void upload(LocalSourceFile localFile, String remotePath, long byteOffset
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);
Expand Down Expand Up @@ -140,12 +140,9 @@ private LocalDestFile downloadDir(final TransferListener listener,
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;
}
Expand All @@ -156,23 +153,16 @@ private LocalDestFile downloadFile(final StreamCopier.Listener listener,
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;
}
Expand Down Expand Up @@ -266,7 +256,7 @@ private String uploadFile(final StreamCopier.Listener listener,
// 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,9 @@ public List<KnownHostEntry> entries() {

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();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,8 @@ protected KeyPair readKeyPair() throws IOException {

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) {
Expand All @@ -225,8 +224,6 @@ protected void parseKeyPair() throws IOException {
payload.put(headerName, s);
}
}
} finally {
r.close();
}
if (this.keyFileVersion == null) {
throw new IOException("Invalid key file format: missing \"PuTTY-User-Key-File-?\" entry");
Expand Down