Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/main/java/ev3dev/utils/DataChannelRereader.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,15 @@ public DataChannelRereader(String pathString) {
/**
* @return a string made from the bytes in the file;
*/
public String readString() {
public synchronized 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);
byteBuffer.clear();
int n = channel.read(byteBuffer,0);
if ((n == -1)||(n == 0)) {
return "";
} else if (n < -1) {
throw new RuntimeException("Unexpected read byte count of " + n + " while reading " + path);
}

byte[] bytes = byteBuffer.array();
if (bytes[n - 1] == '\n') {
Expand All @@ -71,8 +69,12 @@ public String readString() {
}
}

public Path getPath() {
return path;
}

@Override
public void close() throws IOException {
public synchronized void close() throws IOException {
channel.close();
}
}