Skip to content
Closed
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
5 changes: 1 addition & 4 deletions src/main/java/ev3dev/hardware/EV3DevDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,7 @@ protected int getIntegerAttribute(final String attribute){
* @param value value
*/
protected void setStringAttribute(final String attribute, final String value){
final boolean result = Sysfs.writeString(this.PATH_DEVICE + "/" + attribute, value);
if(!result){
throw new RuntimeException("Operation not executed: " + this.PATH_DEVICE + "/" + attribute + " with value " + value);
}
Sysfs.writeString(this.PATH_DEVICE + "/" + attribute, value);
}

/**
Expand Down
73 changes: 29 additions & 44 deletions src/main/java/ev3dev/utils/Sysfs.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import org.slf4j.Logger;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.*;
import java.nio.ByteBuffer;
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 All @@ -29,33 +29,23 @@ public class Sysfs {
*
* @param filePath File path
* @param value value to write
* @return A boolean value if the operation was written or not.
*/
public static boolean writeString(final String filePath, final String value) {
if(log.isTraceEnabled())
log.trace("echo " + value + " > " + filePath);
try {
final File file = new File(filePath);
if(file.canWrite()) {
//TODO Review if it possible to improve
PrintWriter out = new PrintWriter(file);
out.println(value);
out.flush();
out.close();
//TODO Review
}else {
log.error("File: '{}' without write permissions.", filePath);
return false;
}
public static void writeString(final String filePath, final String value) {
log.trace("echo {} > {}", value, filePath);

final ByteBuffer data = StandardCharsets.UTF_8.encode(value);
try (final FileOutputStream fd = new FileOutputStream(filePath, false)) {
fd.write(data.array());

} catch (IOException e) {
log.error(e.getLocalizedMessage(), e);
return false;
String msg = String.format("Sysfs write failed: '%s' with value '%s'", filePath, value);
throw new RuntimeException(msg, e);
}
return true;
}

public static boolean writeInteger(final String filePath, final int value) {
return writeString(filePath, new StringBuilder().append(value).toString());
public static void writeInteger(final String filePath, final int value) {
writeString(filePath, Integer.toString(value));
}

/**
Expand All @@ -64,20 +54,15 @@ public static boolean writeInteger(final String filePath, final int value) {
* @return value from attribute
*/
public static String readString(final String filePath) {
if(log.isTraceEnabled())
log.trace("cat " + filePath);
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);
if(log.isTraceEnabled())
log.trace("value: {}", result);
return result;
}
throw new IOException("Problem reading path: " + filePath);
final String result = Files.readAllLines(Paths.get(filePath), StandardCharsets.UTF_8).get(0);
log.trace("value: {}", result);
return result;
} catch (IOException e) {
log.error(e.getLocalizedMessage(), e);
throw new RuntimeException("Problem reading path: " + filePath, e);
String msg = String.format("Sysfs read failed: '%s'", filePath);
throw new RuntimeException(msg, e);
}
}

Expand All @@ -101,11 +86,14 @@ public static float readFloat(final String filePath) {
*/
public static List<File> getElements(final String filePath) {
final File f = new File(filePath);
if(existPath(filePath) && (f.listFiles().length > 0)) {
return new ArrayList<>(Arrays.asList(f.listFiles()));
}else {
throw new RuntimeException("The path doesn't exist: " + filePath);
if (!existPath(filePath)) {
throw new RuntimeException("Not a directory: " + filePath);
}
final File[] list = f.listFiles();
if (list == null){
throw new RuntimeException("listFiles() returned null: " + filePath);
}
return new ArrayList<>(Arrays.asList(list));
}

/**
Expand All @@ -115,8 +103,7 @@ public static List<File> getElements(final String filePath) {
* @return boolean
*/
public static boolean existPath(final String filePath){
if(log.isTraceEnabled())
log.trace("ls " + filePath);
log.trace("ls {}", filePath);
final File f = new File(filePath);
return f.exists() && f.isDirectory();
}
Expand All @@ -125,13 +112,11 @@ public static boolean existFile(Path pathToFind) {
return Files.exists(pathToFind);
}

public static boolean writeBytes(final String path, final byte[] value) {
public static void writeBytes(final String path, final byte[] value) {
try {
Files.write(Paths.get(path), value, StandardOpenOption.WRITE);
} catch (IOException e) {
throw new RuntimeException("Unable to draw the LCD", e);
}
return true;
}

}