Skip to content

Commit

Permalink
Adding exception to reads with error value on return and changing byt…
Browse files Browse the repository at this point in the history
…eBuffer to byteArray for better readability
  • Loading branch information
ricardobna committed May 18, 2020
1 parent 85ee94a commit f1e5b04
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions TotalCrossVM/src/nm/nio/channels/FileChannelImpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,50 @@ TC_API void jncFCI_read(NMParams p) {
int32 fd = FileChannelImpl_nfd(fileChannel);
int32 buffer;
int32 ret = read(fd, &buffer, 4);
if(ret < 0) {
throwExceptionNamed(p->currentContext, "java.io.IOException", strerror(errno));
return;
}
p->retI = buffer;
}

TC_API void jncFCI_read_b(NMParams p) {
TCObject fileChannel = p->obj[0];
TCObject byteBuffer = p->obj[1];
if(byteBuffer == NULL) {
throwNullArgumentException(p->currentContext, "b");
return;
}
int32 fd = FileChannelImpl_nfd(fileChannel);
TCObject byteBufferArray = ByteBuffer_array(byteBuffer);
int32 byteBufferOffset = ByteBuffer_offset(byteBuffer);
int32 byteBufferLength = ByteBuffer_length(byteBuffer);
void* arrayStart = ARRAYOBJ_START(byteBufferArray);
int32 ret = read(fd, arrayStart, byteBufferLength);
if(ret < 0) {
throwExceptionNamed(p->currentContext, "java.io.IOException", strerror(errno));
return;
}
p->retI = ret;
}

TC_API void jncFCI_read_Bii(NMParams p) {
TCObject fileChannel = p->obj[0];
TCObject byteBuffer = p->obj[1];
TCObject byteArray = p->obj[1];
int32 offset = p->i32[0];
int32 length = p->i32[1];
int32 fd = FileChannelImpl_nfd(fileChannel);
void* arrayStart;
int32 ret;
if(byteBuffer == NULL) {
if(byteArray == NULL) {
throwNullArgumentException(p->currentContext, "b");
return;
}
if(offset < 0 || length < 0 || length > (ARRAYOBJ_LEN(byteBuffer) + offset)) {
if(offset < 0 || length < 0 || length > (ARRAYOBJ_LEN(byteArray) + offset)) {
throwException(p->currentContext, IndexOutOfBoundsException, NULL);
return;
}
arrayStart = ARRAYOBJ_START(byteBuffer) + offset;
arrayStart = ARRAYOBJ_START(byteArray) + offset;
ret = read(fd, arrayStart, length);
if(ret < 0) {
throwExceptionNamed(p->currentContext, "java.io.IOException", strerror(errno));
Expand All @@ -54,21 +66,21 @@ TC_API void jncFCI_read_Bii(NMParams p) {

TC_API void jncFCI_write_Bii(NMParams p) {
TCObject fileChannel = p->obj[0];
TCObject byteBuffer = p->obj[1];
TCObject byteArray = p->obj[1];
int32 offset = p->i32[0];
int32 length = p->i32[1];
int32 fd = FileChannelImpl_nfd(fileChannel);
void* arrayStart;
int32 ret;
if(byteBuffer == NULL) {
if(byteArray == NULL) {
throwNullArgumentException(p->currentContext, "b");
return;
}
if(offset < 0 || length < 0 || length > (ARRAYOBJ_LEN(byteBuffer) + offset)) {
if(offset < 0 || length < 0 || length > (ARRAYOBJ_LEN(byteArray) + offset)) {
throwException(p->currentContext, IndexOutOfBoundsException, NULL);
return;
}
arrayStart = ARRAYOBJ_START(byteBuffer) + offset;
arrayStart = ARRAYOBJ_START(byteArray) + offset;
ret = write(fd, arrayStart, length);
if(ret < 0) {
throwExceptionNamed(p->currentContext, "java.io.IOException", strerror(errno));
Expand Down

0 comments on commit f1e5b04

Please sign in to comment.