Skip to content
Closed

#653 #654

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
72 changes: 34 additions & 38 deletions src/main/java/ev3dev/utils/Sysfs.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import org.slf4j.Logger;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
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 @@ -32,30 +32,23 @@ public class Sysfs {
* @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;
}
log.trace("echo {} > {}", value, filePath);

final byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
try (final FileOutputStream fd = new FileOutputStream(filePath, false)) {
fd.write(bytes);

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

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

/**
Expand All @@ -64,20 +57,17 @@ 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 List<String> lines = Files.readAllLines(Paths.get(filePath), StandardCharsets.UTF_8);
final String result = lines.size() == 0 ? "" : lines.get(0);
log.trace("value: {}", result);
return result;
} catch (IOException e) {
log.error(e.getLocalizedMessage(), e);
throw new RuntimeException("Problem reading path: " + filePath, e);
final String msg = String.format("Sysfs read failed: '%s': %s",
filePath, e.getLocalizedMessage());
log.error(msg, e);
throw new RuntimeException(msg, e);
}
}

Expand All @@ -100,12 +90,19 @@ public static float readFloat(final String filePath) {
* @return an List with options from a path
*/
public static List<File> getElements(final String filePath) {
log.trace("ls {}", 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)) {
log.error("existPath() failed for {}", filePath);
throw new RuntimeException("Not a directory: " + filePath);
}
final File[] list = f.listFiles();
if (list == null) {
log.error("listFiles() returned null for {}", filePath);
throw new RuntimeException("listFiles() returned null: " + filePath);
}
return new ArrayList<>(Arrays.asList(list));
}

/**
Expand All @@ -115,8 +112,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 Down