Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I/O Streams implementation #19

Merged
merged 15 commits into from
Aug 26, 2016
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@ protected int getChunk(byte[] chunk) throws IOException {

@Override
public int bytesLeft() {
int i = 0;
try {
i = is.available();
return is.available();
} catch (IOException e) {
new RuntimeException(e);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw it?

}
return i;
return 0;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to return if you throw the Exception

}

@Override
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/hierynomus/smbj/share/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ public void write(ByteChunkProvider provider) throws IOException, SMBApiExceptio
write(provider, null);
}

public void read(OutputStream destStream) throws IOException,
SMBApiException {
public void read(OutputStream destStream) throws IOException, SMBApiException {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SMBApiException is a RuntimeException, no need to mention.

read(destStream, null);
}

Expand Down
73 changes: 53 additions & 20 deletions src/main/java/com/hierynomus/smbj/share/FileOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ public class FileOutputStream extends OutputStream {
private Connection connection;
private int maxWriteSize;
private ProgressListener progressListener;
private byte[] buf;
private int offset = 0;
private int currentSize = 0;
private int readPosition = 0;
private boolean isClosed = false;
private ByteArrayProvider provider;

Expand All @@ -57,19 +53,18 @@ public FileOutputStream(SMB2FileId fileId, TreeConnect treeConnect, ProgressList
this.connection = session.getConnection();
this.progressListener = progressListener;
this.maxWriteSize = connection.getNegotiatedProtocol().getMaxWriteSize();
this.buf = new byte[maxWriteSize];
this.provider = new ByteArrayProvider();
this.provider = new ByteArrayProvider(this.maxWriteSize);
}

@Override
public void write(int b) throws IOException {
verifyConnectionNotClosed();

if (currentSize < maxWriteSize) {
buf[currentSize] = (byte) b;
++currentSize;
if (provider.getCurrentSize() < maxWriteSize) {
provider.getBuf()[provider.getCurrentSize()] = (byte) b;
provider.incCurrentSize();
}
if (currentSize == maxWriteSize) flush();
if (provider.getCurrentSize() == maxWriteSize) flush();
}

@Override
Expand All @@ -80,11 +75,11 @@ public void write(byte b[]) throws IOException {
@Override
public void write(byte b[], int off, int len) throws IOException {
verifyConnectionNotClosed();
if (currentSize < maxWriteSize) {
System.arraycopy(b, off, buf, currentSize, len);
currentSize = currentSize + len;
if (provider.getCurrentSize() < maxWriteSize) {
System.arraycopy(b, off, provider.getBuf(), provider.getCurrentSize(), len);
provider.incCurrentSize(len);
}
if (currentSize == maxWriteSize) flush();
if (provider.getCurrentSize() == maxWriteSize) flush();
}

@Override
Expand All @@ -99,9 +94,8 @@ public void flush() throws IOException {
if (wresp.getHeader().getStatus() != NtStatus.STATUS_SUCCESS) {
throw new SMBApiException(wresp.getHeader().getStatus(), "Write failed for " + this);
}
offset += currentSize;
currentSize = 0;
readPosition = 0;
provider.resetCurrentSize();
provider.resetReadPosition();
if (progressListener != null)
progressListener.onProgressChanged(wresp.getBytesWritten(), provider.getOffset());
}
Expand All @@ -111,18 +105,27 @@ public void flush() throws IOException {
public void close() throws IOException {
flush();
isClosed = true;
buf = null;
provider.clean();
treeConnect = null;
session = null;
connection = null;
logger.debug("EOF, {} bytes written", offset);
logger.debug("EOF, {} bytes written", provider.getOffset());
}

private void verifyConnectionNotClosed() throws IOException {
if (isClosed) throw new IOException("Stream is closed");
}

private class ByteArrayProvider extends ByteChunkProvider {
private static class ByteArrayProvider extends ByteChunkProvider {

private byte[] buf;
private int maxWriteSize;
private int currentSize;
private int readPosition;

private ByteArrayProvider(int maxWriteSize) {
this.maxWriteSize = maxWriteSize;
}

@Override
public boolean isAvailable() {
Expand All @@ -141,5 +144,35 @@ protected int getChunk(byte[] chunk) throws IOException {
public int bytesLeft() {
return currentSize - readPosition;
}

private byte[] getBuf() {
if (buf == null)
buf = new byte[maxWriteSize];
return buf;
}

private void clean() {
buf = null;
}

private int getCurrentSize() {
return currentSize;
}

private void incCurrentSize() {
incCurrentSize(1);
}

private void incCurrentSize(int i) {
currentSize += i;
}

private void resetCurrentSize() {
currentSize = 0;
}

private void resetReadPosition() {
readPosition = 0;
}
}
}