Reformatted

This commit is contained in:
Jeroen van Erp
2015-06-17 12:36:31 +02:00
parent 6a476858d1
commit 9529c30105

View File

@@ -1,12 +1,12 @@
/** /**
* Copyright 2009 sshj contributors * Copyright 2009 sshj contributors
* * <p/>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* * <p/>
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* * <p/>
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -34,37 +34,31 @@ public class RemoteFile
super(requester, path, handle); super(requester, path, handle);
} }
public FileAttributes fetchAttributes() public FileAttributes fetchAttributes() throws IOException {
throws IOException {
return requester.request(newRequest(PacketType.FSTAT)) return requester.request(newRequest(PacketType.FSTAT))
.retrieve(requester.getTimeoutMs(), TimeUnit.MILLISECONDS) .retrieve(requester.getTimeoutMs(), TimeUnit.MILLISECONDS)
.ensurePacketTypeIs(PacketType.ATTRS) .ensurePacketTypeIs(PacketType.ATTRS)
.readFileAttributes(); .readFileAttributes();
} }
public long length() public long length() throws IOException {
throws IOException {
return fetchAttributes().getSize(); return fetchAttributes().getSize();
} }
public void setLength(long len) public void setLength(long len) throws IOException {
throws IOException {
setAttributes(new FileAttributes.Builder().withSize(len).build()); setAttributes(new FileAttributes.Builder().withSize(len).build());
} }
public int read(long fileOffset, byte[] to, int offset, int len) public int read(long fileOffset, byte[] to, int offset, int len) throws IOException {
throws IOException {
final Response res = asyncRead(fileOffset, len).retrieve(requester.getTimeoutMs(), TimeUnit.MILLISECONDS); final Response res = asyncRead(fileOffset, len).retrieve(requester.getTimeoutMs(), TimeUnit.MILLISECONDS);
return checkReadResponse(res, to, offset); return checkReadResponse(res, to, offset);
} }
protected Promise<Response, SFTPException> asyncRead(long fileOffset, int len) protected Promise<Response, SFTPException> asyncRead(long fileOffset, int len) throws IOException {
throws IOException {
return requester.request(newRequest(PacketType.READ).putUInt64(fileOffset).putUInt32(len)); return requester.request(newRequest(PacketType.READ).putUInt64(fileOffset).putUInt32(len));
} }
protected int checkReadResponse(Response res, byte[] to, int offset) protected int checkReadResponse(Response res, byte[] to, int offset) throws Buffer.BufferException, SFTPException {
throws Buffer.BufferException, SFTPException {
switch (res.getType()) { switch (res.getType()) {
case DATA: case DATA:
int recvLen = res.readUInt32AsInt(); int recvLen = res.readUInt32AsInt();
@@ -80,8 +74,7 @@ public class RemoteFile
} }
} }
public void write(long fileOffset, byte[] data, int off, int len) public void write(long fileOffset, byte[] data, int off, int len) throws IOException {
throws IOException {
checkWriteResponse(asyncWrite(fileOffset, data, off, len)); checkWriteResponse(asyncWrite(fileOffset, data, off, len));
} }
@@ -95,13 +88,11 @@ public class RemoteFile
); );
} }
private void checkWriteResponse(Promise<Response, SFTPException> responsePromise) private void checkWriteResponse(Promise<Response, SFTPException> responsePromise) throws SFTPException {
throws SFTPException {
responsePromise.retrieve(requester.getTimeoutMs(), TimeUnit.MILLISECONDS).ensureStatusPacketIsOK(); responsePromise.retrieve(requester.getTimeoutMs(), TimeUnit.MILLISECONDS).ensureStatusPacketIsOK();
} }
public void setAttributes(FileAttributes attrs) public void setAttributes(FileAttributes attrs) throws IOException {
throws IOException {
requester.request(newRequest(PacketType.FSETSTAT).putFileAttributes(attrs)) requester.request(newRequest(PacketType.FSETSTAT).putFileAttributes(attrs))
.retrieve(requester.getTimeoutMs(), TimeUnit.MILLISECONDS).ensureStatusPacketIsOK(); .retrieve(requester.getTimeoutMs(), TimeUnit.MILLISECONDS).ensureStatusPacketIsOK();
} }
@@ -141,15 +132,13 @@ public class RemoteFile
} }
@Override @Override
public void write(int w) public void write(int w) throws IOException {
throws IOException {
b[0] = (byte) w; b[0] = (byte) w;
write(b, 0, 1); write(b, 0, 1);
} }
@Override @Override
public void write(byte[] buf, int off, int len) public void write(byte[] buf, int off, int len) throws IOException {
throws IOException {
if (unconfirmedWrites.size() > maxUnconfirmedWrites) { if (unconfirmedWrites.size() > maxUnconfirmedWrites) {
checkWriteResponse(unconfirmedWrites.remove()); checkWriteResponse(unconfirmedWrites.remove());
} }
@@ -158,23 +147,20 @@ public class RemoteFile
} }
@Override @Override
public void flush() public void flush() throws IOException {
throws IOException {
while (!unconfirmedWrites.isEmpty()) { while (!unconfirmedWrites.isEmpty()) {
checkWriteResponse(unconfirmedWrites.remove()); checkWriteResponse(unconfirmedWrites.remove());
} }
} }
@Override @Override
public void close() public void close() throws IOException {
throws IOException {
flush(); flush();
} }
} }
public class RemoteFileInputStream public class RemoteFileInputStream extends InputStream {
extends InputStream {
private final byte[] b = new byte[1]; private final byte[] b = new byte[1];
@@ -202,32 +188,30 @@ public class RemoteFile
} }
@Override @Override
public void reset() public void reset() throws IOException {
throws IOException {
fileOffset = markPos; fileOffset = markPos;
} }
@Override @Override
public long skip(long n) public long skip(long n) throws IOException {
throws IOException {
return (this.fileOffset = Math.min(fileOffset + n, length())); return (this.fileOffset = Math.min(fileOffset + n, length()));
} }
@Override @Override
public int read() public int read() throws IOException {
throws IOException {
return read(b, 0, 1) == -1 ? -1 : b[0] & 0xff; return read(b, 0, 1) == -1 ? -1 : b[0] & 0xff;
} }
@Override @Override
public int read(byte[] into, int off, int len) public int read(byte[] into, int off, int len) throws IOException {
throws IOException {
int read = RemoteFile.this.read(fileOffset, into, off, len); int read = RemoteFile.this.read(fileOffset, into, off, len);
if (read != -1) { if (read != -1) {
fileOffset += read; fileOffset += read;
if (markPos != 0 && read > readLimit) // Invalidate mark position if (markPos != 0 && read > readLimit) {
// Invalidate mark position
markPos = 0; markPos = 0;
} }
}
return read; return read;
} }
@@ -263,19 +247,19 @@ public class RemoteFile
private ByteArrayInputStream pending = new ByteArrayInputStream(new byte[0]); private ByteArrayInputStream pending = new ByteArrayInputStream(new byte[0]);
private boolean retrieveUnconfirmedRead(boolean blocking) throws IOException { private boolean retrieveUnconfirmedRead(boolean blocking) throws IOException {
if (unconfirmedReads.size() <= 0) {
if (unconfirmedReads.size() <= 0)
return false; return false;
}
if (!blocking && !unconfirmedReads.peek().isDelivered()) if (!blocking && !unconfirmedReads.peek().isDelivered()) {
return false; return false;
}
unconfirmedReadOffsets.remove(); unconfirmedReadOffsets.remove();
final Response res = unconfirmedReads.remove().retrieve(requester.getTimeoutMs(), TimeUnit.MILLISECONDS); final Response res = unconfirmedReads.remove().retrieve(requester.getTimeoutMs(), TimeUnit.MILLISECONDS);
switch (res.getType()) { switch (res.getType()) {
case DATA: case DATA:
int recvLen = res.readUInt32AsInt(); int recvLen = res.readUInt32AsInt();
responseOffset += recvLen; responseOffset += recvLen;
pending = new ByteArrayInputStream(res.array(), res.rpos(), recvLen); pending = new ByteArrayInputStream(res.array(), res.rpos(), recvLen);
break; break;
@@ -331,14 +315,14 @@ public class RemoteFile
return -1; return -1;
} }
if (0 == recvLen) // avoid infinite loops if (0 == recvLen) {
// avoid infinite loops
throw new SFTPException("Unexpected response size (0), bailing out"); throw new SFTPException("Unexpected response size (0), bailing out");
}
responseOffset += recvLen; responseOffset += recvLen;
pending = new ByteArrayInputStream(buf, 0, recvLen); pending = new ByteArrayInputStream(buf, 0, recvLen);
} } else if (!retrieveUnconfirmedRead(true /*blocking*/)) {
else
if (!retrieveUnconfirmedRead( true /*blocking*/ )) {
// this may happen if we change prefetch strategy // this may happen if we change prefetch strategy
// currently, we should never get here... // currently, we should never get here...
@@ -352,8 +336,10 @@ public class RemoteFile
@Override @Override
public int available() throws IOException { public int available() throws IOException {
while (!eof && (pending.available() <= 0) && retrieveUnconfirmedRead( false /*blocking*/ )) boolean lastRead = true;
/*loop*/; while (!eof && (pending.available() <= 0) && lastRead) {
lastRead = retrieveUnconfirmedRead(false /*blocking*/);
}
return pending.available(); return pending.available();
} }
} }