Skip to content

Commit cf24c47

Browse files
Merge pull request EMCECS#6 from EMCECS/bugfix-single-byte-read
Bugfix single byte read
2 parents 81104e8 + 732cf7b commit cf24c47

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/main/java/com/emc/ecs/nfsclient/nfs/io/NfsFileInputStream.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016 EMC Corporation. All Rights Reserved.
2+
* Copyright 2016-2017 EMC Corporation. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License").
55
* You may not use this file except in compliance with the License.
@@ -218,7 +218,8 @@ public int read() throws IOException {
218218
if (bytesRead == EOF) {
219219
return EOF;
220220
} else {
221-
return b[0];
221+
//byte type in Java is from -128 to +127 and we are supposed to return 0-255
222+
return b[0] & 0xFF;
222223
}
223224
}
224225

src/test/java/com/emc/ecs/nfsclient/nfs/io/Test_Streams.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016 EMC Corporation. All Rights Reserved.
2+
* Copyright 2016-2017 EMC Corporation. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License").
55
* You may not use this file except in compliance with the License.
@@ -47,19 +47,27 @@ public void testReadingAndWriting() throws Exception {
4747
assertTrue(test.exists());
4848
assertTrue(test.canModify());
4949
assertTrue(test.canRead());
50-
byte[] expectedData = "some data".getBytes(RpcRequest.CHARSET);
50+
byte[] expectedData = new byte[] { 1, 2, 3, 127, -1, -128, 0, 1, 32 };
5151
outputStream.write(expectedData);
5252
outputStream.close();
5353

5454
NfsFileInputStream inputStream = new NfsFileInputStream(test);
55-
byte[] buffer = new byte[1000];
5655
assertEquals(expectedData.length, inputStream.available());
5756
int bytesRead = (int) inputStream.skip(expectedData.length);
5857
assertEquals(0, inputStream.available());
5958
inputStream.close();
6059

6160
inputStream = new NfsFileInputStream(test);
62-
buffer = new byte[1000];
61+
for (int i = 0; i < expectedData.length; ++i) {
62+
int nextByte = inputStream.read();
63+
assertNotEquals(NfsFileInputStream.EOF, nextByte);
64+
assertEquals(expectedData[i], (byte) nextByte);
65+
}
66+
assertEquals(NfsFileInputStream.EOF, inputStream.read());
67+
inputStream.close();
68+
69+
inputStream = new NfsFileInputStream(test);
70+
byte[] buffer = new byte[1000];
6371
assertEquals(expectedData.length, inputStream.available());
6472
bytesRead = inputStream.read(buffer);
6573
assertEquals(0, inputStream.available());
@@ -119,9 +127,8 @@ public void testClosing() throws Exception {
119127

120128
try {
121129
outputStream.close();
122-
fail("This should throw an IOException");
123130
} catch (IOException e) {
124-
// Do nothing, this was expected.
131+
fail("This should not throw an IOException");
125132
}
126133

127134
try {
@@ -164,9 +171,8 @@ public void testClosing() throws Exception {
164171

165172
try {
166173
inputStream.close();
167-
fail("This should throw an IOException");
168174
} catch (IOException e) {
169-
// Do nothing, this was expected.
175+
fail("This should not throw an IOException");
170176
}
171177

172178
try {

0 commit comments

Comments
 (0)