Skip to content

Conversation

@dwalend
Copy link
Contributor

@dwalend dwalend commented Dec 30, 2020

No description provided.

byteBuffer.clear();
channel.position(0);
int n = channel.read(byteBuffer);
if (n == -1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this branch should handle the n == 0 case as well. I'm not sure when would this happen, but if it happens then the code below will break (bytes[-1] is invalid).

Copy link
Member

Choose a reason for hiding this comment

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

It is not a bad idea.
I will merge this version and later we will refine with that idea

Copy link
Contributor

Choose a reason for hiding this comment

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

This was already added: c1b7897 (later commit in this PR)

} while (n <= 0);
byteBuffer.clear();
channel.position(0);
int n = channel.read(byteBuffer);
Copy link
Contributor

@JakubVanek JakubVanek Dec 30, 2020

Choose a reason for hiding this comment

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

The position() + read() calls can be merged together:

int n = channel.read(byteBuffer, 0);
With that change, all FileChannel accesses should be thread-safe (I think including close), only the buffer would be unsafe.

EDIT: javadocs: https://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html#read(java.nio.ByteBuffer,%20long)

@jabrena jabrena merged commit 433eaf3 into ev3dev-lang-java:sysfs_perf2 Dec 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants