All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.silverspoon.bulldog.linux.sysfs.SysFs Maven / Gradle / Ivy

There is a newer version: 0.3.0
Show newest version
package io.silverspoon.bulldog.linux.sysfs;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileWriter;

import io.silverspoon.bulldog.core.util.BulldogUtil;

@Deprecated
public class SysFs {

   private static final int WAIT_TIMEOUT_MS = 5000;

   public SysFs() {

   }

   public File[] getFilesInPath(String path, final String namePattern) {
      File root = new File(path);
      File[] files = root.listFiles(new FileFilter() {
         public boolean accept(File pathname) {
            return pathname.getName().startsWith(namePattern);
         }
      });

      return files;
   }

   public void echo(String path, Object value) {
      echo(path, String.valueOf(value));
   }

   public void echo(String path, String value) {
      try {
         BufferedWriter writer = new BufferedWriter(new FileWriter(path));
         writer.write(value);
         writer.close();
      } catch (Exception e) {
         throw new RuntimeException(e);
      }
   }

   public void echoAndWait(String path, Object value, int waitMs) {
      echoAndWait(path, String.valueOf(value), waitMs);
   }

   public void echoAndWait(String path, String value, int waitMs) {
      try {
         waitForFileCreation(path, WAIT_TIMEOUT_MS);
         BufferedWriter writer = new BufferedWriter(new FileWriter(path));
         writer.write(value);
         BulldogUtil.sleepMs(waitMs);
         writer.close();
      } catch (Exception e) {
         throw new RuntimeException(e);
      }
   }

   public void waitForFileCreation(String filePath, long waitMillis) {
      long startWaitingTime = System.currentTimeMillis();
      File file = new File(filePath);
      while (!file.exists()) {
         BulldogUtil.sleepMs(10);

         long millisecondsInWait = System.currentTimeMillis() - startWaitingTime;
         if (millisecondsInWait >= waitMillis) {
            throw new RuntimeException("Could not find file " + filePath + " within " + waitMillis + " milliseconds. Aborting.");
         }
      }
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy