Ejemplo n.º 1
0
  /** Retourne le stream associé à une URL, d'abord teste le réseau et sinon le cache */
  public InputStream getWithBackup(String url) throws Exception {
    // Cache non accessible, on retourne directement l'appel distant
    if (!isAvailable()) return Util.openStream(url);

    Exception eBackup = null;
    InputStream is = null;

    // Tentative d'accès direct par le réseau
    try {
      is = Util.openStream(url);
      if (is == null) throw new Exception("cache openStream error");
    } catch (Exception e) {
      is = null;
      eBackup = e;
    }

    String id = codage(url);
    File f = new File(dir + Util.FS + id);

    // Ca a marché en direct !
    if (is != null) {
      // Devrais-je tenter de remettre à jour le cache
      if (!f.isFile() || outOfDate(f)) add(url);
      return is;
    }

    // Ca n'a pas marché en direct, peut être présent dans le cache ?
    if (f.isFile() && f.canRead() && f.length() > 0) {
      aladin.trace(3, "[" + url + "] backup from cache !");
      return new FileInputStream(f);
    }

    // Bon, pas d'autre solution
    throw eBackup;
  }
Ejemplo n.º 2
0
  private static List<File> kompilerClasspath(File kotlinHome, CompileContext context) {
    File libs = new File(kotlinHome, "lib");

    if (!libs.exists() || libs.isFile()) {
      context.addMessage(
          ERROR,
          "Broken compiler at '"
              + libs.getAbsolutePath()
              + "'. Make sure plugin is properly installed",
          "",
          -1,
          -1);
      return Collections.emptyList();
    }

    ArrayList<File> answer = new ArrayList<File>();
    File[] jars = libs.listFiles();
    if (jars != null) {
      for (File jar : jars) {
        if (jar.isFile() && jar.getName().endsWith(".jar")) {
          answer.add(jar);
        }
      }
    }

    return answer;
  }
Ejemplo n.º 3
0
  /**
   * load from the specified jar filled with help files in the [language] directory in the jar
   *
   * @param file the jar file
   */
  private void loadFromJar(File file) {
    if (file.getName().toLowerCase().endsWith(".jar") && file.isFile()) {
      try {
        int counter = 0;
        JarInputStream jis;
        JarEntry je;
        counter = 0;
        jis = new JarInputStream(new BufferedInputStream(new FileInputStream(file)));
        je = jis.getNextJarEntry();

        while (je != null) {
          String mnemo = trimEntryName(je);
          if (je.getName().toLowerCase().matches(helproot + "/" + language + "/.*.htm")
              && !exists(mnemo)) {
            addToCache(jis, mnemo);
            counter++;
          }
          je = jis.getNextJarEntry();
        }
        jis.close();

        System.out.println(
            "+ " + String.valueOf(counter) + "\thelp text(s) from:\t" + file.getCanonicalPath());
      } catch (IOException ignored) {
      }
    }
  }
Ejemplo n.º 4
0
 public static void createNgramsFromFolder(
     File input_folder, File output_folder, int ngram_value) {
   Stack<File> stack = new Stack<File>();
   stack.push(input_folder);
   while (!stack.isEmpty()) {
     File child = stack.pop();
     if (child.isDirectory()) {
       for (File f : child.listFiles()) stack.push(f);
     } else if (child.isFile()) {
       try {
         System.out.println("Processing: " + child.getAbsolutePath());
         FileReader fr = new FileReader(child.getAbsolutePath());
         FileWriter outputFile = new FileWriter(output_folder + "/file" + file_no);
         BufferedReader br = new BufferedReader(fr);
         String readline = "";
         while ((readline = br.readLine()) != null) {
           String[] words = readline.split("\\s+");
           for (int i = 0; i < words.length - ngram_value + 1; i++) {
             String ngram = "";
             for (int j = 0; j < ngram_value; j++) ngram = ngram + " " + words[i + j];
             outputFile.write(ngram + "\n");
           }
         }
         file_no++;
         outputFile.close();
         br.close();
         fr.close();
       } catch (Exception e) {
         System.out.println("File not found:" + e);
       }
     }
   }
 }
Ejemplo n.º 5
0
  public boolean load(File f) {

    if (f.exists() && f.isFile()) {
      list = new Vector();
      try {
        FileInputStream fis = new FileInputStream(f);
        BufferedReader br = new BufferedReader(new InputStreamReader(fis));
        System.out.println("loading file:" + f);
        String tmp = br.readLine();
        while (tmp != null) {
          // System.out.println("line" +tmp);
          list.add(new Data(tmp));
          tmp = br.readLine();
        }
        return true;

      } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      return false;
    }
    System.err.println("bad file: " + f.getName());
    return false;
  }
Ejemplo n.º 6
0
  /** @param fn */
  protected final void fileForShow(File fn) {
    if (!fn.exists() || !fn.isFile()) {
      return;
    }

    s_showFile = fn;
  }
Ejemplo n.º 7
0
 /**
  * Expands list of files to process into full list of all files that can be found by recursively
  * descending directories.
  */
 void expand(File dir, String[] files, boolean isUpdate) {
   if (files == null) {
     return;
   }
   for (int i = 0; i < files.length; i++) {
     File f;
     if (dir == null) {
       f = new File(files[i]);
     } else {
       f = new File(dir, files[i]);
     }
     if (f.isFile()) {
       if (entries.add(f)) {
         if (isUpdate) entryMap.put(entryName(f.getPath()), f);
       }
     } else if (f.isDirectory()) {
       if (entries.add(f)) {
         if (isUpdate) {
           String dirPath = f.getPath();
           dirPath = (dirPath.endsWith(File.separator)) ? dirPath : (dirPath + File.separator);
           entryMap.put(entryName(dirPath), f);
         }
         expand(f, f.list(), isUpdate);
       }
     } else {
       error(formatMsg("error.nosuch.fileordir", String.valueOf(f)));
       ok = false;
     }
   }
 }
Ejemplo n.º 8
0
  /**
   * helper: recursively scan given directory.
   *
   * @return list of files found (might be empty)
   */
  private ArrayList<File> scanDirectory(File path) {
    Debug.trace("scanning directory " + path.getAbsolutePath());

    ArrayList<File> scan_result = new ArrayList<File>();

    if (!path.isDirectory()) {
      return scan_result;
    }

    File[] entries = path.listFiles();

    for (File file : entries) {
      if (file == null) {
        continue;
      }

      if (file.isDirectory()) {
        scan_result.addAll(scanDirectory(file));
      } else if ((file.isFile()) && (file.getName().toLowerCase().endsWith(".java"))) {
        scan_result.add(file);
      }
    }

    return scan_result;
  }
Ejemplo n.º 9
0
  public static String[] listFiles(File dir, Boolean includeSubDirs) throws IOException {
    FileFilter fileFilter =
        new FileFilter() {
          public boolean accept(File file) {
            return file.isDirectory();
          }
        };
    File[] subFolders = dir.listFiles(fileFilter);

    List<String> files = new ArrayList<String>();

    List<File> fileArray =
        new ArrayList<File>(
            FileUtils.listFiles(
                dir, TrueFileFilter.INSTANCE, includeSubDirs ? TrueFileFilter.INSTANCE : null));

    for (File file : fileArray) {
      if (file.isFile()) {
        if (includeSubDirs && containsParentFolder(file, subFolders)) {
          files.add(file.getParentFile().getName() + File.separator + file.getName());
        } else {
          files.add(file.getName());
        }
      }
    }

    return (String[]) files.toArray(new String[0]);
  }
Ejemplo n.º 10
0
  private Service getService(File base) throws Exception {
    File dataFile = new File(base, "data");
    if (!dataFile.isFile()) return null;

    ServiceData data = getData(ServiceData.class, dataFile);
    return new Service(this, data);
  }
Ejemplo n.º 11
0
 public boolean modelExists() {
   if (location == null) return false;
   File folder = new File(location);
   if (!folder.isDirectory()) return false;
   File dat = new File(folder, LEVELS_DAT_FILE);
   return dat.isFile();
 }
Ejemplo n.º 12
0
  public static File[] listFileHandles(File dir, Boolean includeSubDirs) throws IOException {

    if (!dir.exists() || !dir.isDirectory()) {
      return new File[0];
    }

    FileFilter fileFilter =
        new FileFilter() {
          public boolean accept(File file) {
            return file.isDirectory();
          }
        };

    File[] subFolders = dir.listFiles(fileFilter);

    List<File> files = new ArrayList<File>();

    List<File> fileArray =
        new ArrayList<File>(
            FileUtils.listFiles(
                dir, TrueFileFilter.INSTANCE, includeSubDirs ? TrueFileFilter.INSTANCE : null));

    for (File file : fileArray) {
      if (file.isFile()) {
        if (includeSubDirs && containsParentFolder(file, subFolders)) {
          files.add(file);
        } else {
          files.add(file);
        }
      }
    }

    return (File[]) files.toArray(new File[0]);
  }
Ejemplo n.º 13
0
  /**
   * Loads test configuration.
   *
   * @throws Exception if configuration is unawailable or broken.
   */
  private void loadTestConfiguration() throws Exception {
    assert TEST_CONFIGURATION_FILE.isFile();

    InputStream in = null;

    Properties p = new Properties();

    try {
      in = new FileInputStream(TEST_CONFIGURATION_FILE);

      p.load(in);
    } finally {
      U.closeQuiet(in);
    }

    clientNodes = Integer.parseInt(p.getProperty("client.nodes.count"));
    srvNodes = Integer.parseInt(p.getProperty("server.nodes.count"));
    threadsPerClient = Integer.parseInt(p.getProperty("threads.per.client"));
    cancelRate = Integer.parseInt(p.getProperty("cancel.rate"));
    submitDelay = Long.parseLong(p.getProperty("submit.delay"));

    taskParams =
        new GridJobLoadTestParams(
            Integer.parseInt(p.getProperty("jobs.count")),
            Integer.parseInt(p.getProperty("jobs.test.duration")),
            Integer.parseInt(p.getProperty("jobs.test.completion.delay")),
            Double.parseDouble(p.getProperty("jobs.failure.probability")));
  }
Ejemplo n.º 14
0
    @Override
    public void actionPerformed(ActionEvent e) {
      Frame frame = getFrame();
      JFileChooser chooser = new JFileChooser();
      int ret = chooser.showOpenDialog(frame);

      if (ret != JFileChooser.APPROVE_OPTION) {
        return;
      }

      File f = chooser.getSelectedFile();
      if (f.isFile() && f.canRead()) {
        Document oldDoc = getEditor().getDocument();
        if (oldDoc != null) {
          oldDoc.removeUndoableEditListener(undoHandler);
        }
        if (elementTreePanel != null) {
          elementTreePanel.setEditor(null);
        }
        getEditor().setDocument(new PlainDocument());
        frame.setTitle(f.getName());
        Thread loader = new FileLoader(f, editor.getDocument());
        loader.start();
      } else {
        JOptionPane.showMessageDialog(
            getFrame(),
            "Could not open file: " + f,
            "Error opening file",
            JOptionPane.ERROR_MESSAGE);
      }
    }
Ejemplo n.º 15
0
  /**
   * Output the entries to the specified output folder on the file system.
   *
   * @param outputFolder The target output folder.
   * @throws java.io.IOException Write failure.
   */
  public void toFileSystem(File outputFolder) throws IOException {
    AssertArgument.isNotNull(outputFolder, "outputFolder");

    if (outputFolder.isFile()) {
      throw new IOException(
          "Cannot write Archive entries to '"
              + outputFolder.getAbsolutePath()
              + "'.  This is a normal file i.e. not a directory.");
    }
    if (!outputFolder.exists()) {
      outputFolder.mkdirs();
    }

    Set<Map.Entry<String, File>> entrySet = entries.entrySet();
    for (Map.Entry<String, File> entry : entrySet) {
      File archEntryFile = entry.getValue();
      byte[] fileBytes;
      File entryFile = new File(outputFolder, entry.getKey());

      if (archEntryFile != null) {
        fileBytes = FileUtils.readFile(archEntryFile);
        entryFile.getParentFile().mkdirs();
        FileUtils.writeFile(fileBytes, entryFile);
      } else {
        entryFile.mkdirs();
      }
    }
  }
Ejemplo n.º 16
0
  private void copy(File workspaceDir, InputStream in, Pattern glob, boolean overwrite)
      throws Exception {

    Jar jar = new Jar("dot", in);
    try {
      for (Entry<String, Resource> e : jar.getResources().entrySet()) {

        String path = e.getKey();
        bnd.trace("path %s", path);

        if (glob != null && !glob.matcher(path).matches()) continue;

        Resource r = e.getValue();
        File dest = Processor.getFile(workspaceDir, path);
        if (overwrite
            || !dest.isFile()
            || dest.lastModified() < r.lastModified()
            || r.lastModified() <= 0) {

          bnd.trace("copy %s to %s", path, dest);

          File dp = dest.getParentFile();
          if (!dp.exists() && !dp.mkdirs()) {
            throw new IOException("Could not create directory " + dp);
          }

          IO.copy(r.openInputStream(), dest);
        }
      }
    } finally {
      jar.close();
    }
  }
  private void copyAllFilesToLogDir(File node, File parent) throws IOException {
    if (!node.getAbsoluteFile().equals(parent.getAbsoluteFile())
        && node.isFile()
        && !node.getParentFile().equals(parent)) {
      String fileNamePrefix = node.getName().substring(0, node.getName().lastIndexOf('.'));
      String fileNameSuffix = node.getName().substring(node.getName().lastIndexOf('.'));
      String newFilePath =
          node.getParentFile().getAbsolutePath()
              + File.separator
              + fileNamePrefix.replace(".", "_")
              + fileNameSuffix;

      File newNode = new File(newFilePath);
      if (node.renameTo(newNode)) {
        FileUtils.copyFileToDirectory(newNode, parent);
      }
    }
    if (node.isDirectory()) {
      String[] subNote = node.list();
      for (String filename : subNote) {
        copyAllFilesToLogDir(new File(node, filename), parent);
      }
      if (!node.equals(parent)) {
        FileUtils.deleteDirectory(node);
      }
    }
  }
Ejemplo n.º 18
0
  protected static long getTorrentDataSizeFromFileOrDirSupport(File file) {
    String name = file.getName();

    if (name.equals(".") || name.equals("..")) {

      return (0);
    }

    if (!file.exists()) {

      return (0);
    }

    if (file.isFile()) {

      return (file.length());

    } else {

      File[] dir_files = file.listFiles();

      long length = 0;

      for (int i = 0; i < dir_files.length; i++) {

        length += getTorrentDataSizeFromFileOrDirSupport(dir_files[i]);
      }

      return (length);
    }
  }
Ejemplo n.º 19
0
 public void load() {
   if (!modelExists()) {
     loadError = "Cannot find " + LEVELS_DAT_FILE + ".";
     return;
   }
   File folder = new File(location);
   File dat = new File(folder, LEVELS_DAT_FILE);
   // File lst = new File(folder, LEVELS_LST_FILE);
   if (!dat.isFile()) return;
   ArrayList levels = new ArrayList();
   try {
     FileInputStream reader = new FileInputStream(dat);
     long fileLength = dat.length();
     int length = model.getField().getSize() + 96;
     int readLength = 0;
     byte[] buffer = new byte[length];
     while (fileLength - readLength >= length) {
       int toRead = length;
       while (toRead > 0) {
         toRead -= reader.read(buffer, length - toRead, toRead);
       }
       readLength += length;
       SupaplexLevel level = new SupaplexLevel();
       level.setModel(model);
       level.loadFromBytes(buffer);
       levels.add(level);
     }
     reader.close();
   } catch (Exception e) {
     e.printStackTrace();
   }
   SupaplexLevel[] ls = (SupaplexLevel[]) levels.toArray(new SupaplexLevel[0]);
   model.setLevels(ls);
 }
Ejemplo n.º 20
0
 /** Load all AIML Maps */
 int addAIMLMaps() {
   int cnt = 0;
   Timer timer = new Timer();
   timer.start();
   try {
     // Directory path here
     String file;
     File folder = new File(maps_path);
     if (folder.exists()) {
       File[] listOfFiles = IOUtils.listFiles(folder);
       if (MagicBooleans.trace_mode)
         System.out.println("Loading AIML Map files from " + maps_path);
       for (File listOfFile : listOfFiles) {
         if (listOfFile.isFile()) {
           file = listOfFile.getName();
           if (file.endsWith(".txt") || file.endsWith(".TXT")) {
             if (MagicBooleans.trace_mode) System.out.println(file);
             String mapName = file.substring(0, file.length() - ".txt".length());
             if (MagicBooleans.trace_mode) System.out.println("Read AIML Map " + mapName);
             AIMLMap aimlMap = new AIMLMap(mapName, this);
             cnt += aimlMap.readAIMLMap(this);
             mapMap.put(mapName, aimlMap);
           }
         }
       }
     } else System.out.println("addAIMLMaps: " + maps_path + " does not exist.");
   } catch (Exception ex) {
     ex.printStackTrace();
   }
   return cnt;
 }
Ejemplo n.º 21
0
 /**
  * Get the test suite specifications from the suite file, apply the options to all, and report any
  * messages to the holder.
  *
  * @param suitePath the String path to the harness suite file
  * @param options the String[] options for the tests - may be null
  * @param holder the IMessageHolder for any messages - may be null
  * @return AjcTest.Suite.Spec test descriptions (non-null but empty if some error)
  */
 public static AjcTest.Suite.Spec getSuiteSpec(
     String suitePath, String[] options, IMessageHolder holder) {
   if (null == suitePath) {
     MessageUtil.fail(holder, "null suitePath");
     return EmptySuite.ME;
   }
   File suiteFile = new File(suitePath);
   if (!suiteFile.canRead() || !suiteFile.isFile()) {
     MessageUtil.fail(holder, "unable to read file " + suitePath);
     return EmptySuite.ME;
   }
   try {
     AjcTest.Suite.Spec tempSpec;
     AbstractRunSpec.RT runtime = new AbstractRunSpec.RT();
     tempSpec = AjcSpecXmlReader.getReader().readAjcSuite(suiteFile);
     tempSpec.setSuiteDirFile(suiteFile.getParentFile());
     if (null == options) {
       options = new String[0];
     }
     runtime.setOptions(options);
     boolean skip = !tempSpec.adoptParentValues(runtime, holder);
     if (skip) {
       tempSpec = EmptySuite.ME;
     }
     return tempSpec;
   } catch (IOException e) {
     MessageUtil.abort(holder, "IOException", e);
     return EmptySuite.ME;
   }
 }
Ejemplo n.º 22
0
 @Override
 public int getIconResourceId() {
   if (!parentItem && !dirSelectItem) {
     return file.isFile() ? R.drawable.ic_file : R.drawable.ic_dir;
   } else {
     return R.drawable.ic_dir;
   }
 }
Ejemplo n.º 23
0
  /**
   * @param sourceFile File to read from
   * @return List of String objects with the shas
   */
  public static FileRequestFileContent readRequestFile(final File sourceFile) {
    if (!sourceFile.isFile() || !(sourceFile.length() > 0)) {
      return null;
    }
    Document d = null;
    try {
      d = XMLTools.parseXmlFile(sourceFile.getPath());
    } catch (final Throwable t) {
      logger.log(Level.SEVERE, "Exception in readRequestFile, during XML parsing", t);
      return null;
    }

    if (d == null) {
      logger.log(Level.SEVERE, "Could'nt parse the request file");
      return null;
    }

    final Element rootNode = d.getDocumentElement();

    if (rootNode.getTagName().equals(TAG_FrostFileRequestFile) == false) {
      logger.severe(
          "Error: xml request file does not contain the root tag '"
              + TAG_FrostFileRequestFile
              + "'");
      return null;
    }

    final String timeStampStr = XMLTools.getChildElementsTextValue(rootNode, TAG_timestamp);
    if (timeStampStr == null) {
      logger.severe("Error: xml file does not contain the tag '" + TAG_timestamp + "'");
      return null;
    }
    final long timestamp = Long.parseLong(timeStampStr);

    final List<Element> nodelist = XMLTools.getChildElementsByTagName(rootNode, TAG_shaList);
    if (nodelist.size() != 1) {
      logger.severe("Error: xml request files must contain only one element '" + TAG_shaList + "'");
      return null;
    }

    final Element rootShaNode = nodelist.get(0);

    final List<String> shaList = new LinkedList<String>();
    final List<Element> xmlKeys = XMLTools.getChildElementsByTagName(rootShaNode, TAG_sha);
    for (final Element el : xmlKeys) {

      final Text txtname = (Text) el.getFirstChild();
      if (txtname == null) {
        continue;
      }

      final String sha = txtname.getData();
      shaList.add(sha);
    }

    final FileRequestFileContent content = new FileRequestFileContent(timestamp, shaList);
    return content;
  }
 private static void recurse(File dir, Set<File> foundFiles) {
   for (File f : dir.listFiles()) {
     if (f.isFile()) {
       foundFiles.add(f);
     } else if (f.isDirectory()) {
       recurse(f, foundFiles);
     }
   }
 }
Ejemplo n.º 25
0
 @GET
 @Path("/files")
 public List<ToDo> files() {
   List<ToDo> response = Lists.newArrayList();
   File[] files = new File("/lib").listFiles();
   if (files != null) {
     for (File file : files) {
       if (file.isFile())
         response.add(
             new ToDo(
                 ++id,
                 file.getName(),
                 file.isFile() ? file.length() : -1,
                 System.currentTimeMillis()));
     }
   }
   return response;
 }
Ejemplo n.º 26
0
  protected TOTorrentCreateImpl(
      File _torrent_base, URL _announce_url, boolean _add_other_hashes, long _piece_length)
      throws TOTorrentException {
    super(_torrent_base.getName(), _announce_url, _torrent_base.isFile());

    torrent_base = _torrent_base;
    piece_length = _piece_length;
    add_other_hashes = _add_other_hashes;
  }
Ejemplo n.º 27
0
 private void visit(Set<String> names, String prefix, File file) {
   for (File child : file.listFiles()) {
     if (child.isFile()) {
       names.add(prefix + child.getName());
     } else if (child.isDirectory()) {
       visit(names, prefix + child.getName() + "/", child);
     }
   }
 }
Ejemplo n.º 28
0
  private List<String> readHistory(BundleContext context) throws IOException {
    File log = context.getDataFile("log.txt");
    List<String> result = new ArrayList<String>();

    if (log.isFile()) {
      read(log, result);
    }

    return result;
  }
Ejemplo n.º 29
0
  protected long getTotalFileSizeSupport(File file) throws TOTorrentException {

    String name = file.getName();

    /////////////////////////////////////////

    /////////////////////////////////////

    if (name.equals(".") || name.equals("..")) {

      return (0);
    }

    if (!file.exists()) {

      throw (new TOTorrentException(
          "TOTorrentCreate: file '" + file.getName() + "' doesn't exist",
          TOTorrentException.RT_FILE_NOT_FOUND));
    }

    if (file.isFile()) {

      if (!ignoreFile(name)) {

        total_file_count++;

        return (file.length());

      } else {

        return (0);
      }
    } else {

      File[] dir_files = file.listFiles();

      if (dir_files == null) {

        throw (new TOTorrentException(
            "TOTorrentCreate: directory '"
                + file.getAbsolutePath()
                + "' returned error when listing files in it",
            TOTorrentException.RT_FILE_NOT_FOUND));
      }

      long length = 0;

      for (int i = 0; i < dir_files.length; i++) {

        length += getTotalFileSizeSupport(dir_files[i]);
      }

      return (length);
    }
  }
Ejemplo n.º 30
0
 /**
  * Copies content of {@code fromDir} to {@code toDir}. It's equivalent to "cp -r fromDir/* toDir"
  * unix command.
  *
  * @param fromDir source directory
  * @param toDir destination directory
  * @throws IOException in case of any IO troubles
  */
 public static void copyDirContent(@NotNull File fromDir, @NotNull File toDir) throws IOException {
   File[] children = ObjectUtils.notNull(fromDir.listFiles(), ArrayUtil.EMPTY_FILE_ARRAY);
   for (File child : children) {
     File target = new File(toDir, child.getName());
     if (child.isFile()) {
       copy(child, target);
     } else {
       copyDir(child, target, true);
     }
   }
 }