diff --git a/src/main/java/net/schmizz/sshj/common/Buffer.java b/src/main/java/net/schmizz/sshj/common/Buffer.java index 2c809774..d5887f2c 100644 --- a/src/main/java/net/schmizz/sshj/common/Buffer.java +++ b/src/main/java/net/schmizz/sshj/common/Buffer.java @@ -311,7 +311,7 @@ public class Buffer> { public T putUInt32(long uint32) { ensureCapacity(4); if (uint32 < 0 || uint32 > 0xffffffffL) - throw new RuntimeException("Invalid value: " + uint32); + throw new IllegalArgumentException("Invalid value: " + uint32); data[wpos++] = (byte) (uint32 >> 24); data[wpos++] = (byte) (uint32 >> 16); data[wpos++] = (byte) (uint32 >> 8); @@ -346,7 +346,7 @@ public class Buffer> { @SuppressWarnings("unchecked") public T putUInt64(long uint64) { if (uint64 < 0) - throw new RuntimeException("Invalid value: " + uint64); + throw new IllegalArgumentException("Invalid value: " + uint64); data[wpos++] = (byte) (uint64 >> 56); data[wpos++] = (byte) (uint64 >> 48); data[wpos++] = (byte) (uint64 >> 40); diff --git a/src/main/java/net/schmizz/sshj/common/StreamCopier.java b/src/main/java/net/schmizz/sshj/common/StreamCopier.java index 1fedc075..2cf0d0ac 100644 --- a/src/main/java/net/schmizz/sshj/common/StreamCopier.java +++ b/src/main/java/net/schmizz/sshj/common/StreamCopier.java @@ -15,7 +15,6 @@ */ package net.schmizz.sshj.common; -import net.schmizz.sshj.common.LoggerFactory; import net.schmizz.concurrent.Event; import net.schmizz.concurrent.ExceptionChainer; import org.slf4j.Logger; @@ -129,11 +128,13 @@ public class StreamCopier { final long startTime = System.currentTimeMillis(); if (length == -1) { - while ((read = in.read(buf)) != -1) - count = write(buf, count, read); + while ((read = in.read(buf)) != -1) { + count += write(buf, count, read); + } } else { - while (count < length && (read = in.read(buf, 0, (int) Math.min(bufSize, length - count))) != -1) - count = write(buf, count, read); + while (count < length && (read = in.read(buf, 0, (int) Math.min(bufSize, length - count))) != -1) { + count += write(buf, count, read); + } } if (!keepFlushing) @@ -149,14 +150,13 @@ public class StreamCopier { return count; } - private long write(byte[] buf, long count, int read) + private long write(byte[] buf, long curPos, int len) throws IOException { - out.write(buf, 0, read); - count += read; + out.write(buf, 0, len); if (keepFlushing) out.flush(); - listener.reportProgress(count); - return count; + listener.reportProgress(curPos + len); + return len; } } diff --git a/src/main/java/net/schmizz/sshj/connection/ConnectionImpl.java b/src/main/java/net/schmizz/sshj/connection/ConnectionImpl.java index 1a7b2ce5..5657d3a1 100644 --- a/src/main/java/net/schmizz/sshj/connection/ConnectionImpl.java +++ b/src/main/java/net/schmizz/sshj/connection/ConnectionImpl.java @@ -126,10 +126,9 @@ public class ConnectionImpl @Override public void handle(Message msg, SSHPacket buf) throws SSHException { - if (msg.in(91, 100)) + if (msg.in(91, 100)) { getChannel(buf).handle(msg, buf); - - else if (msg.in(80, 90)) + } else if (msg.in(80, 90)) { switch (msg) { case REQUEST_SUCCESS: gotGlobalReqResponse(buf); @@ -142,10 +141,11 @@ public class ConnectionImpl break; default: super.handle(msg, buf); + break; } - - else + } else { super.handle(msg, buf); + } } @Override @@ -174,11 +174,11 @@ public class ConnectionImpl } @Override - public void join() - throws InterruptedException { + public void join() throws InterruptedException { synchronized (internalSynchronizer) { - while (!channels.isEmpty()) + while (!channels.isEmpty()) { internalSynchronizer.wait(); + } } } diff --git a/src/main/java/net/schmizz/sshj/connection/channel/ChannelOutputStream.java b/src/main/java/net/schmizz/sshj/connection/channel/ChannelOutputStream.java index 184c2a55..9610dd39 100644 --- a/src/main/java/net/schmizz/sshj/connection/channel/ChannelOutputStream.java +++ b/src/main/java/net/schmizz/sshj/connection/channel/ChannelOutputStream.java @@ -27,9 +27,7 @@ import java.io.OutputStream; * {@link OutputStream} for channels. Buffers data upto the remote window's maximum packet size. Data can also be * flushed via {@link #flush()} and is also flushed on {@link #close()}. */ -public final class ChannelOutputStream - extends OutputStream - implements ErrorNotifiable { +public final class ChannelOutputStream extends OutputStream implements ErrorNotifiable { private final Channel chan; private final Transport trans; @@ -56,8 +54,7 @@ public final class ChannelOutputStream dataOffset = packet.wpos(); } - int write(byte[] data, int off, int len) - throws TransportException, ConnectionException { + int write(byte[] data, int off, int len) throws TransportException, ConnectionException { final int bufferSize = packet.wpos() - dataOffset; if (bufferSize >= win.getMaxPacketSize()) { flush(bufferSize, true); @@ -69,15 +66,12 @@ public final class ChannelOutputStream } } - boolean flush(boolean canAwaitExpansion) - throws TransportException, ConnectionException { + boolean flush(boolean canAwaitExpansion) throws TransportException, ConnectionException { return flush(packet.wpos() - dataOffset, canAwaitExpansion); } - boolean flush(int bufferSize, boolean canAwaitExpansion) - throws TransportException, ConnectionException { + boolean flush(int bufferSize, boolean canAwaitExpansion) throws TransportException, ConnectionException { while (bufferSize > 0) { - long remoteWindowSize = win.getSize(); if (remoteWindowSize == 0) { if (canAwaitExpansion) { @@ -140,10 +134,12 @@ public final class ChannelOutputStream public synchronized void write(final byte[] data, int off, int len) throws IOException { checkClose(); - while (len > 0) { - final int n = buffer.write(data, off, len); - off += n; - len -= n; + int length = len; + int offset = off; + while (length > 0) { + final int n = buffer.write(data, offset, len); + offset += n; + length -= n; } } diff --git a/src/main/java/net/schmizz/sshj/connection/channel/forwarded/RemotePortForwarder.java b/src/main/java/net/schmizz/sshj/connection/channel/forwarded/RemotePortForwarder.java index 90b673e4..01465763 100644 --- a/src/main/java/net/schmizz/sshj/connection/channel/forwarded/RemotePortForwarder.java +++ b/src/main/java/net/schmizz/sshj/connection/channel/forwarded/RemotePortForwarder.java @@ -130,7 +130,7 @@ public class RemotePortForwarder // Addresses match up return true; } - if ("localhost".equals(address) && (channelForward.address.equals("127.0.0.1") || channelForward.address.equals("::1"))) { + if ("localhost".equals(address) && ("127.0.0.1".equals(channelForward.address) || "::1".equals(channelForward.address))) { // Localhost special case. return true; } diff --git a/src/main/java/net/schmizz/sshj/sftp/RemoteDirectory.java b/src/main/java/net/schmizz/sshj/sftp/RemoteDirectory.java index 945918b7..f804a381 100644 --- a/src/main/java/net/schmizz/sshj/sftp/RemoteDirectory.java +++ b/src/main/java/net/schmizz/sshj/sftp/RemoteDirectory.java @@ -46,8 +46,9 @@ public class RemoteDirectory final FileAttributes attrs = res.readFileAttributes(); final PathComponents comps = requester.getPathHelper().getComponents(path, name); final RemoteResourceInfo inf = new RemoteResourceInfo(comps, attrs); - if (!(name.equals(".") || name.equals("..")) && (filter == null || filter.accept(inf))) + if (!(".".equals(name) || "..".equals(name)) && (filter == null || filter.accept(inf))) { rri.add(inf); + } } break; diff --git a/src/main/java/net/schmizz/sshj/sftp/SFTPFileTransfer.java b/src/main/java/net/schmizz/sshj/sftp/SFTPFileTransfer.java index e84094b5..6a260d2e 100644 --- a/src/main/java/net/schmizz/sshj/sftp/SFTPFileTransfer.java +++ b/src/main/java/net/schmizz/sshj/sftp/SFTPFileTransfer.java @@ -60,14 +60,12 @@ public class SFTPFileTransfer } @Override - public void upload(LocalSourceFile localFile, String remotePath) - throws IOException { + public void upload(LocalSourceFile localFile, String remotePath) throws IOException { new Uploader(localFile, remotePath).upload(getTransferListener()); } @Override - public void download(String source, LocalDestFile dest) - throws IOException { + public void download(String source, LocalDestFile dest) throws IOException { final PathComponents pathComponents = engine.getPathHelper().getComponents(source); final FileAttributes attributes = engine.stat(source); new Downloader().download(getTransferListener(), new RemoteResourceInfo(pathComponents, attributes), dest); @@ -91,10 +89,10 @@ public class SFTPFileTransfer private class Downloader { + @SuppressWarnings("PMD.MissingBreakInSwitch") private void download(final TransferListener listener, final RemoteResourceInfo remote, - final LocalDestFile local) - throws IOException { + final LocalDestFile local) throws IOException { final LocalDestFile adjustedFile; switch (remote.getAttributes().getType()) { case DIRECTORY: @@ -104,8 +102,7 @@ public class SFTPFileTransfer log.warn("Server did not supply information about the type of file at `{}` " + "-- assuming it is a regular file!", remote.getPath()); case REGULAR: - adjustedFile = downloadFile(listener.file(remote.getName(), remote.getAttributes().getSize()), - remote, local); + adjustedFile = downloadFile(listener.file(remote.getName(), remote.getAttributes().getSize()), remote, local); break; default: throw new IOException(remote + " is not a regular file or directory");