Skip to content
Merged
Show file tree
Hide file tree
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
71 changes: 71 additions & 0 deletions src/main/java/ev3dev/utils/DataChannelRereader.java
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);

Copy link
Copy Markdown
Contributor

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:

int old = channel.position();
channel.position(0);
int rval = channel.read(buffer);
channel.position(old);
return rval;

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();
}
}
61 changes: 4 additions & 57 deletions src/main/java/ev3dev/utils/Sysfs2.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -67,38 +62,24 @@ public static boolean writeInteger(final String filePath, final int value) {
* @param filePath path
* @return value from attribute
*/
public static String readStringOld(final String filePath) {
public static String readString(final String filePath) {
if (log.isTraceEnabled()) {
log.trace("cat " + filePath);
}
try {
final Path path = Paths.get(filePath);
if (existFile(path) && Files.isReadable(path)) {
final String result = Files.readAllLines(path, Charset.forName("UTF-8")).get(0);
try (DataChannelRereader rereader = new DataChannelRereader(filePath)) {
String result = rereader.readString();
if (log.isTraceEnabled()) {
log.trace("value: {}", result);
}
return result;
}
throw new IOException("Problem reading path: " + filePath);
} catch (IOException e) {
log.error(e.getLocalizedMessage(), e);
throw new RuntimeException("Problem reading path: " + filePath, e);
}
}

public static String readString(final String filePath) {
if (log.isTraceEnabled()) {
log.trace("cat " + filePath);
}
setPathString(filePath);
String result = readStringCustomChannel();
if (log.isTraceEnabled()) {
log.trace("value: {}", result);
}
return result;
}

/**
* Read an Attribute in the Sysfs with containing Integer values
*
Expand All @@ -109,9 +90,7 @@ public static int readInteger(final String filePath) {
return Integer.parseInt(readString(filePath));
}

public static float readFloat(final String filePath) {
return Float.parseFloat(readString(filePath));
}
public static float readFloat(final String filePath) { return Float.parseFloat(readString(filePath)); }

/**
* @param filePath path
Expand Down Expand Up @@ -159,36 +138,4 @@ public static boolean writeBytes(final String path, final byte[] value) {
}
return true;
}

//Skipping much of the boilerplate between InputStream and Channels. 12 ms, but 6 ms with the static path!
//static float fetchSampleCustomChannel() {
// return Float.parseFloat(readStringCustomChannel());
//}

//Static Setter to test the idea from @dwalend
public static void setPathString(String pathString) {
Sysfs2.pathString = pathString;
}

public static String pathString = "";

private static final byte[] buffer = new byte[8];
static final Path staticPath = Paths.get(pathString);
public static String readStringCustomChannel() {
try {
try (InputStream in = customInputStream(staticPath)) {
int n = in.read(buffer);
//todo throw IOException for n = -1
return new String(buffer, StandardCharsets.US_ASCII);
}
} catch (IOException e) {
throw new RuntimeException("Problem reading path: " + pathString, e);
}
}

static InputStream customInputStream(final Path path) throws IOException {
ReadableByteChannel rbc = Files.newByteChannel(path);
return Channels.newInputStream(rbc);
}

}