Skip to content

Commit

Permalink
More code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
hierynomus committed Jan 23, 2017
1 parent ef3f7a2 commit 40f956b
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 44 deletions.
4 changes: 2 additions & 2 deletions src/main/java/net/schmizz/sshj/common/Buffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public long readUInt32()
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);
Expand Down Expand Up @@ -346,7 +346,7 @@ public long readUInt64()
@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);
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/net/schmizz/sshj/common/StreamCopier.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -129,11 +128,13 @@ public long copy()
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)
Expand All @@ -149,14 +150,13 @@ public long copy()
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;
}

}
16 changes: 8 additions & 8 deletions src/main/java/net/schmizz/sshj/connection/ConnectionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,9 @@ private Channel getChannel(SSHPacket buffer)
@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);
Expand All @@ -142,10 +141,11 @@ else if (msg.in(80, 90))
break;
default:
super.handle(msg, buf);
break;
}

else
} else {
super.handle(msg, buf);
}
}

@Override
Expand Down Expand Up @@ -174,11 +174,11 @@ public void setWindowSize(long windowSize) {
}

@Override
public void join()
throws InterruptedException {
public void join() throws InterruptedException {
synchronized (internalSynchronizer) {
while (!channels.isEmpty())
while (!channels.isEmpty()) {
internalSynchronizer.wait();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
* {@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;
Expand All @@ -56,8 +54,7 @@ private final class DataBuffer {
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);
Expand All @@ -69,15 +66,12 @@ int write(byte[] data, int off, int len)
}
}

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) {
Expand Down Expand Up @@ -140,10 +134,12 @@ public synchronized void write(int w)
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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private boolean handles(ForwardedTCPIPChannel channel) {
// 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;
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/schmizz/sshj/sftp/RemoteDirectory.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ public List<RemoteResourceInfo> scan(RemoteResourceFilter filter)
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;

Expand Down
13 changes: 5 additions & 8 deletions src/main/java/net/schmizz/sshj/sftp/SFTPFileTransfer.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,12 @@ public void download(String source, String dest)
}

@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);
Expand All @@ -91,10 +89,10 @@ public RemoteResourceFilter getDownloadFilter() {

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:
Expand All @@ -104,8 +102,7 @@ private void download(final TransferListener listener,
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");
Expand Down

0 comments on commit 40f956b

Please sign in to comment.