Repository navigation
Channel reset #756
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
Merged
Merged
Channel reset #756
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8a9a9cf
Changed the all-java optimization in Sysfs2.java to not be hard-coded…
dwalend 31079af
Reading the short is definitely faster. Not sure about how to interpr…
dwalend 3340b73
All Sysfs2.java unit tests pass for the minimal java read string opti…
dwalend 57403cb
stray import removed
dwalend 51c5c4e
There's potentially 27 bytes to read. Increasing buffer size to 32.
dwalend 95505c8
Added the reusable DataChannelRereader and used it as filling for get…
dwalend File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package ev3dev.utils; | ||
|
|
||
| import java.io.Closeable; | ||
| import java.io.IOException; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.channels.FileChannel; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
|
|
||
| /** | ||
| * Reader of streams that can reread the same channel for structured data of | ||
| * known length. The focus of this class is on performance. | ||
| * | ||
| * @author David Walend | ||
| */ | ||
| public class DataChannelRereader implements Closeable { | ||
|
|
||
| private final Path path; | ||
| private final ByteBuffer byteBuffer; | ||
| private final FileChannel channel; | ||
|
|
||
| /** | ||
| * Create a DataChannelRereader for path with a bufferLength byte buffer | ||
| * | ||
| * @param path path to the file to reread | ||
| * @param bufferLength length of the buffer to hold the structure | ||
| * @throws IOException when things go wrong | ||
| */ | ||
| public DataChannelRereader(Path path, int bufferLength) throws IOException { | ||
| this.path = path; | ||
| this.byteBuffer = ByteBuffer.allocate(bufferLength); | ||
| this.channel = FileChannel.open(path); | ||
| } | ||
|
|
||
| /** | ||
| * Create a DataChannelRereader for pathString with the default 32-byte buffer. | ||
| * | ||
| * @param pathString Path to the file to reread | ||
| * @throws IOException when things go wrong | ||
| */ | ||
| public DataChannelRereader(String pathString) throws IOException { | ||
| this(Paths.get(pathString),32); | ||
| } | ||
|
|
||
| /** | ||
| * @return a string made from the bytes in the file; | ||
| */ | ||
| public String readString() { | ||
| try { | ||
| int n; | ||
| do { | ||
| byteBuffer.clear(); | ||
| channel.position(0); | ||
| n = channel.read(byteBuffer); | ||
| if (n == -1) throw new IOException("Premature end of file "); | ||
| } while (n <= 0); | ||
|
|
||
| byte[] bytes = byteBuffer.array(); | ||
| if (bytes[n-1] == '\n') return new String(bytes, 0, n-1, StandardCharsets.UTF_8); | ||
| else return new String(bytes, 0, n, StandardCharsets.UTF_8); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException("Problem reading path: "+path, e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| channel.close(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could be simplified to a
channel.read(byteBuffer, 0)call. However, it's entirely possible that under the hood the two-arg read is just the following, which could be slower: