Skip to content

Commit

Permalink
Merge pull request #6 from EMCECS/bugfix-single-byte-read
Browse files Browse the repository at this point in the history
Bugfix single byte read
  • Loading branch information
DavidASeibert authored Jan 6, 2017
2 parents 81104e8 + 732cf7b commit cf24c47
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016 EMC Corporation. All Rights Reserved.
* Copyright 2016-2017 EMC Corporation. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
Expand Down Expand Up @@ -218,7 +218,8 @@ public int read() throws IOException {
if (bytesRead == EOF) {
return EOF;
} else {
return b[0];
//byte type in Java is from -128 to +127 and we are supposed to return 0-255
return b[0] & 0xFF;
}
}

Expand Down
22 changes: 14 additions & 8 deletions src/test/java/com/emc/ecs/nfsclient/nfs/io/Test_Streams.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016 EMC Corporation. All Rights Reserved.
* Copyright 2016-2017 EMC Corporation. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
Expand Down Expand Up @@ -47,19 +47,27 @@ public void testReadingAndWriting() throws Exception {
assertTrue(test.exists());
assertTrue(test.canModify());
assertTrue(test.canRead());
byte[] expectedData = "some data".getBytes(RpcRequest.CHARSET);
byte[] expectedData = new byte[] { 1, 2, 3, 127, -1, -128, 0, 1, 32 };
outputStream.write(expectedData);
outputStream.close();

NfsFileInputStream inputStream = new NfsFileInputStream(test);
byte[] buffer = new byte[1000];
assertEquals(expectedData.length, inputStream.available());
int bytesRead = (int) inputStream.skip(expectedData.length);
assertEquals(0, inputStream.available());
inputStream.close();

inputStream = new NfsFileInputStream(test);
buffer = new byte[1000];
for (int i = 0; i < expectedData.length; ++i) {
int nextByte = inputStream.read();
assertNotEquals(NfsFileInputStream.EOF, nextByte);
assertEquals(expectedData[i], (byte) nextByte);
}
assertEquals(NfsFileInputStream.EOF, inputStream.read());
inputStream.close();

inputStream = new NfsFileInputStream(test);
byte[] buffer = new byte[1000];
assertEquals(expectedData.length, inputStream.available());
bytesRead = inputStream.read(buffer);
assertEquals(0, inputStream.available());
Expand Down Expand Up @@ -119,9 +127,8 @@ public void testClosing() throws Exception {

try {
outputStream.close();
fail("This should throw an IOException");
} catch (IOException e) {
// Do nothing, this was expected.
fail("This should not throw an IOException");
}

try {
Expand Down Expand Up @@ -164,9 +171,8 @@ public void testClosing() throws Exception {

try {
inputStream.close();
fail("This should throw an IOException");
} catch (IOException e) {
// Do nothing, this was expected.
fail("This should not throw an IOException");
}

try {
Expand Down

0 comments on commit cf24c47

Please sign in to comment.