More code cleanup

This commit is contained in:
Jeroen van Erp
2017-01-23 15:33:22 +01:00
parent ef3f7a2eaf
commit 40f956b4b6
7 changed files with 38 additions and 44 deletions

View File

@@ -311,7 +311,7 @@ public class Buffer<T extends Buffer<T>> {
public T putUInt32(long uint32) { public T putUInt32(long uint32) {
ensureCapacity(4); ensureCapacity(4);
if (uint32 < 0 || uint32 > 0xffffffffL) 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 >> 24);
data[wpos++] = (byte) (uint32 >> 16); data[wpos++] = (byte) (uint32 >> 16);
data[wpos++] = (byte) (uint32 >> 8); data[wpos++] = (byte) (uint32 >> 8);
@@ -346,7 +346,7 @@ public class Buffer<T extends Buffer<T>> {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public T putUInt64(long uint64) { public T putUInt64(long uint64) {
if (uint64 < 0) if (uint64 < 0)
throw new RuntimeException("Invalid value: " + uint64); throw new IllegalArgumentException("Invalid value: " + uint64);
data[wpos++] = (byte) (uint64 >> 56); data[wpos++] = (byte) (uint64 >> 56);
data[wpos++] = (byte) (uint64 >> 48); data[wpos++] = (byte) (uint64 >> 48);
data[wpos++] = (byte) (uint64 >> 40); data[wpos++] = (byte) (uint64 >> 40);

View File

@@ -15,7 +15,6 @@
*/ */
package net.schmizz.sshj.common; package net.schmizz.sshj.common;
import net.schmizz.sshj.common.LoggerFactory;
import net.schmizz.concurrent.Event; import net.schmizz.concurrent.Event;
import net.schmizz.concurrent.ExceptionChainer; import net.schmizz.concurrent.ExceptionChainer;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -129,11 +128,13 @@ public class StreamCopier {
final long startTime = System.currentTimeMillis(); final long startTime = System.currentTimeMillis();
if (length == -1) { if (length == -1) {
while ((read = in.read(buf)) != -1) while ((read = in.read(buf)) != -1) {
count = write(buf, count, read); count += write(buf, count, read);
}
} else { } else {
while (count < length && (read = in.read(buf, 0, (int) Math.min(bufSize, length - count))) != -1) while (count < length && (read = in.read(buf, 0, (int) Math.min(bufSize, length - count))) != -1) {
count = write(buf, count, read); count += write(buf, count, read);
}
} }
if (!keepFlushing) if (!keepFlushing)
@@ -149,14 +150,13 @@ public class StreamCopier {
return count; return count;
} }
private long write(byte[] buf, long count, int read) private long write(byte[] buf, long curPos, int len)
throws IOException { throws IOException {
out.write(buf, 0, read); out.write(buf, 0, len);
count += read;
if (keepFlushing) if (keepFlushing)
out.flush(); out.flush();
listener.reportProgress(count); listener.reportProgress(curPos + len);
return count; return len;
} }
} }

View File

@@ -126,10 +126,9 @@ public class ConnectionImpl
@Override @Override
public void handle(Message msg, SSHPacket buf) public void handle(Message msg, SSHPacket buf)
throws SSHException { throws SSHException {
if (msg.in(91, 100)) if (msg.in(91, 100)) {
getChannel(buf).handle(msg, buf); getChannel(buf).handle(msg, buf);
} else if (msg.in(80, 90)) {
else if (msg.in(80, 90))
switch (msg) { switch (msg) {
case REQUEST_SUCCESS: case REQUEST_SUCCESS:
gotGlobalReqResponse(buf); gotGlobalReqResponse(buf);
@@ -142,11 +141,12 @@ public class ConnectionImpl
break; break;
default: default:
super.handle(msg, buf); super.handle(msg, buf);
break;
} }
} else {
else
super.handle(msg, buf); super.handle(msg, buf);
} }
}
@Override @Override
public int getMaxPacketSize() { public int getMaxPacketSize() {
@@ -174,13 +174,13 @@ public class ConnectionImpl
} }
@Override @Override
public void join() public void join() throws InterruptedException {
throws InterruptedException {
synchronized (internalSynchronizer) { synchronized (internalSynchronizer) {
while (!channels.isEmpty()) while (!channels.isEmpty()) {
internalSynchronizer.wait(); internalSynchronizer.wait();
} }
} }
}
@Override @Override
public int nextID() { public int nextID() {

View File

@@ -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 * {@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()}. * flushed via {@link #flush()} and is also flushed on {@link #close()}.
*/ */
public final class ChannelOutputStream public final class ChannelOutputStream extends OutputStream implements ErrorNotifiable {
extends OutputStream
implements ErrorNotifiable {
private final Channel chan; private final Channel chan;
private final Transport trans; private final Transport trans;
@@ -56,8 +54,7 @@ public final class ChannelOutputStream
dataOffset = packet.wpos(); dataOffset = packet.wpos();
} }
int write(byte[] data, int off, int len) int write(byte[] data, int off, int len) throws TransportException, ConnectionException {
throws TransportException, ConnectionException {
final int bufferSize = packet.wpos() - dataOffset; final int bufferSize = packet.wpos() - dataOffset;
if (bufferSize >= win.getMaxPacketSize()) { if (bufferSize >= win.getMaxPacketSize()) {
flush(bufferSize, true); flush(bufferSize, true);
@@ -69,15 +66,12 @@ public final class ChannelOutputStream
} }
} }
boolean flush(boolean canAwaitExpansion) boolean flush(boolean canAwaitExpansion) throws TransportException, ConnectionException {
throws TransportException, ConnectionException {
return flush(packet.wpos() - dataOffset, canAwaitExpansion); return flush(packet.wpos() - dataOffset, canAwaitExpansion);
} }
boolean flush(int bufferSize, boolean canAwaitExpansion) boolean flush(int bufferSize, boolean canAwaitExpansion) throws TransportException, ConnectionException {
throws TransportException, ConnectionException {
while (bufferSize > 0) { while (bufferSize > 0) {
long remoteWindowSize = win.getSize(); long remoteWindowSize = win.getSize();
if (remoteWindowSize == 0) { if (remoteWindowSize == 0) {
if (canAwaitExpansion) { if (canAwaitExpansion) {
@@ -140,10 +134,12 @@ public final class ChannelOutputStream
public synchronized void write(final byte[] data, int off, int len) public synchronized void write(final byte[] data, int off, int len)
throws IOException { throws IOException {
checkClose(); checkClose();
while (len > 0) { int length = len;
final int n = buffer.write(data, off, len); int offset = off;
off += n; while (length > 0) {
len -= n; final int n = buffer.write(data, offset, len);
offset += n;
length -= n;
} }
} }

View File

@@ -130,7 +130,7 @@ public class RemotePortForwarder
// Addresses match up // Addresses match up
return true; 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. // Localhost special case.
return true; return true;
} }

View File

@@ -46,9 +46,10 @@ public class RemoteDirectory
final FileAttributes attrs = res.readFileAttributes(); final FileAttributes attrs = res.readFileAttributes();
final PathComponents comps = requester.getPathHelper().getComponents(path, name); final PathComponents comps = requester.getPathHelper().getComponents(path, name);
final RemoteResourceInfo inf = new RemoteResourceInfo(comps, attrs); 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); rri.add(inf);
} }
}
break; break;
case STATUS: case STATUS:

View File

@@ -60,14 +60,12 @@ public class SFTPFileTransfer
} }
@Override @Override
public void upload(LocalSourceFile localFile, String remotePath) public void upload(LocalSourceFile localFile, String remotePath) throws IOException {
throws IOException {
new Uploader(localFile, remotePath).upload(getTransferListener()); new Uploader(localFile, remotePath).upload(getTransferListener());
} }
@Override @Override
public void download(String source, LocalDestFile dest) public void download(String source, LocalDestFile dest) throws IOException {
throws IOException {
final PathComponents pathComponents = engine.getPathHelper().getComponents(source); final PathComponents pathComponents = engine.getPathHelper().getComponents(source);
final FileAttributes attributes = engine.stat(source); final FileAttributes attributes = engine.stat(source);
new Downloader().download(getTransferListener(), new RemoteResourceInfo(pathComponents, attributes), dest); new Downloader().download(getTransferListener(), new RemoteResourceInfo(pathComponents, attributes), dest);
@@ -91,10 +89,10 @@ public class SFTPFileTransfer
private class Downloader { private class Downloader {
@SuppressWarnings("PMD.MissingBreakInSwitch")
private void download(final TransferListener listener, private void download(final TransferListener listener,
final RemoteResourceInfo remote, final RemoteResourceInfo remote,
final LocalDestFile local) final LocalDestFile local) throws IOException {
throws IOException {
final LocalDestFile adjustedFile; final LocalDestFile adjustedFile;
switch (remote.getAttributes().getType()) { switch (remote.getAttributes().getType()) {
case DIRECTORY: case DIRECTORY:
@@ -104,8 +102,7 @@ public class SFTPFileTransfer
log.warn("Server did not supply information about the type of file at `{}` " + log.warn("Server did not supply information about the type of file at `{}` " +
"-- assuming it is a regular file!", remote.getPath()); "-- assuming it is a regular file!", remote.getPath());
case REGULAR: case REGULAR:
adjustedFile = downloadFile(listener.file(remote.getName(), remote.getAttributes().getSize()), adjustedFile = downloadFile(listener.file(remote.getName(), remote.getAttributes().getSize()), remote, local);
remote, local);
break; break;
default: default:
throw new IOException(remote + " is not a regular file or directory"); throw new IOException(remote + " is not a regular file or directory");