示例#1
1
 /**
  * Construct a frequency table from an InputStream. This will create a temporary file to copy the
  * InputStream in.
  *
  * @param source the Input Stream
  * @return an InputStream which is a copy of the source Stream, provided to re-Read the same Bytes
  *     for the actual compression process.
  */
 public InputStream constructTable(InputStream source, boolean copy) {
   freq = new int[TABLESIZE];
   try {
     File file = null;
     FileOutputStream fos = null;
     if (copy == true) {
       file = File.createTempFile("huf", "tmp");
       file.deleteOnExit();
       fos = new FileOutputStream(file);
     }
     while (source.available() != 0) {
       int c = source.read();
       freq[c]++;
       if (copy) fos.write(c);
     }
     source.close();
     if (copy) {
       fos.close();
       return new FileInputStream(file);
     }
     return null;
   } catch (Exception ex) {
     ExHandler.handle(ex);
     return null;
   }
 }
示例#2
0
 public Graph(WeightedBVGraph graph, String[] names) {
   org.apache.log4j.Logger logger =
       org.apache.log4j.Logger.getLogger("it.unimi.dsi.webgraph.ImmutableGraph");
   logger.setLevel(org.apache.log4j.Level.FATAL);
   if (names.length != graph.numNodes())
     throw new Error("Problem with the list of names for the nodes in the graph.");
   try {
     File auxFile = File.createTempFile("graph-maps-" + System.currentTimeMillis(), "aux");
     auxFile.deleteOnExit();
     RecordManager recMan = RecordManagerFactory.createRecordManager(auxFile.getAbsolutePath());
     nodes = recMan.hashMap("nodes");
     nodesReverse = recMan.hashMap("nodesReverse");
   } catch (IOException ex) {
     throw new Error(ex);
   }
   nodes.clear();
   nodesReverse.clear();
   Constructor[] cons = WeightedArc.class.getDeclaredConstructors();
   for (int i = 0; i < cons.length; i++) cons[i].setAccessible(true);
   this.graph = graph;
   WeightedArcSet list = new WeightedArcSet();
   ArcLabelledNodeIterator it = graph.nodeIterator();
   while (it.hasNext()) {
     if (commit++ % COMMIT_SIZE == 0) {
       commit();
       list.commit();
     }
     Integer aux1 = it.nextInt();
     Integer aux2 = null;
     ArcLabelledNodeIterator.LabelledArcIterator suc = it.successors();
     while ((aux2 = suc.nextInt()) != null && aux2 >= 0 && (aux2 < graph.numNodes()))
       try {
         WeightedArc arc = (WeightedArc) cons[0].newInstance(aux2, aux1, suc.label().getFloat());
         list.add(arc);
         this.nodes.put(aux1, names[aux1]);
         this.nodes.put(aux2, names[aux2]);
         this.nodesReverse.put(names[aux1], aux1);
         this.nodesReverse.put(names[aux2], aux2);
       } catch (Exception ex) {
         throw new Error(ex);
       }
   }
   reverse = new WeightedBVGraph(list.toArray(new WeightedArc[0]));
   numArcs = list.size();
   iterator = nodeIterator();
   try {
     File auxFile = File.createTempFile("graph" + System.currentTimeMillis(), "aux");
     auxFile.deleteOnExit();
     String basename = auxFile.getAbsolutePath();
     store(basename);
   } catch (IOException ex) {
     throw new Error(ex);
   }
   commit();
 }
  private static File patchConfFile(File conf, String library) throws IOException {
    File tmpConf = File.createTempFile("idea-", "-mvn.conf");
    tmpConf.deleteOnExit();
    patchConfFile(conf, tmpConf, library);

    return tmpConf;
  }
示例#4
0
 public static String createTempImageFile(String origFile, String prefix, String suffix)
     throws Exception {
   File outputFile = createTempFile(prefix, suffix);
   outputFile.deleteOnExit();
   copyFile(outputFile.getAbsolutePath(), origFile);
   return outputFile.getAbsolutePath();
 }
  private static String getArtifactResolverJar(boolean isMaven3) throws IOException {
    Class marker =
        isMaven3 ? MavenArtifactResolvedM3RtMarker.class : MavenArtifactResolvedM2RtMarker.class;

    File classDirOrJar = new File(PathUtil.getJarPathForClass(marker));

    if (!classDirOrJar.isDirectory()) {
      return classDirOrJar.getAbsolutePath(); // it's a jar in IDEA installation.
    }

    // it's a classes directory, we are in development mode.
    File tempFile = FileUtil.createTempFile("idea-", "-artifactResolver.jar");
    tempFile.deleteOnExit();

    ZipOutputStream zipOutput = new ZipOutputStream(new FileOutputStream(tempFile));
    try {
      ZipUtil.addDirToZipRecursively(zipOutput, null, classDirOrJar, "", null, null);

      if (isMaven3) {
        File m2Module = new File(PathUtil.getJarPathForClass(MavenModuleMap.class));

        String commonClassesPath = MavenModuleMap.class.getPackage().getName().replace('.', '/');
        ZipUtil.addDirToZipRecursively(
            zipOutput, null, new File(m2Module, commonClassesPath), commonClassesPath, null, null);
      }
    } finally {
      zipOutput.close();
    }

    return tempFile.getAbsolutePath();
  }
 void put(final URI uri, ArtifactData data) throws Exception {
   reporter.trace("put %s %s", uri, data);
   File tmp = createTempFile(repoDir, "mtp", ".whatever");
   tmp.deleteOnExit();
   try {
     copy(uri.toURL(), tmp);
     byte[] sha = SHA1.digest(tmp).digest();
     reporter.trace("SHA %s %s", uri, Hex.toHexString(sha));
     ArtifactData existing = get(sha);
     if (existing != null) {
       reporter.trace("existing");
       xcopy(existing, data);
       return;
     }
     File meta = new File(repoDir, Hex.toHexString(sha) + ".json");
     File file = new File(repoDir, Hex.toHexString(sha));
     rename(tmp, file);
     reporter.trace("file %s", file);
     data.file = file.getAbsolutePath();
     data.sha = sha;
     data.busy = false;
     CommandData cmddata = parseCommandData(data);
     if (cmddata.bsn != null) {
       data.name = cmddata.bsn + "-" + cmddata.version;
     } else data.name = Strings.display(cmddata.title, cmddata.bsn, cmddata.name, uri);
     codec.enc().to(meta).put(data);
     reporter.trace("TD = " + data);
   } finally {
     tmp.delete();
     reporter.trace("puted %s %s", uri, data);
   }
 }
 @Test
 public void shouldHonorDiskStorageRootOverride() throws IOException, ClassNotFoundException {
   File tmpDir = TestUtil.createTmpDir();
   try {
     String tmpDirPath = tmpDir.getAbsolutePath();
     systemEnv.put(TlbConstants.Server.TLB_DATA_DIR.key, tmpDirPath);
     initializer = new TlbServerInitializer(new SystemEnvironment(systemEnv));
     EntryRepoFactory factory = initializer.repoFactory();
     File file =
         new File(
             tmpDirPath,
             new EntryRepoFactory.VersionedNamespace(LATEST_VERSION, SUBSET_SIZE)
                 .getIdUnder("quux"));
     file.deleteOnExit();
     writeEntriedTo(file);
     SubsetSizeRepo repo = factory.createSubsetRepo("quux", LATEST_VERSION);
     assertThat(
         (List<SubsetSizeEntry>) repo.list(),
         is(
             Arrays.asList(
                 new SubsetSizeEntry(1), new SubsetSizeEntry(2), new SubsetSizeEntry(3))));
   } finally {
     FileUtils.deleteQuietly(tmpDir);
   }
 }
示例#8
0
  /**
   * Take the name of a jar file and extract the plugin.xml file, if possible, to a temporary file.
   *
   * @param f The jar file to extract from.
   * @return a temporary file to which the plugin.xml file has been copied.
   */
  public static File unpackPluginXML(File f) {
    InputStream in = null;
    OutputStream out = null;

    try {
      JarFile jar = new JarFile(f);
      ZipEntry entry = jar.getEntry(PLUGIN_XML_FILE);
      if (entry == null) {
        return null;
      }
      File dest = File.createTempFile("jabref_plugin", ".xml");
      dest.deleteOnExit();

      in = new BufferedInputStream(jar.getInputStream(entry));
      out = new BufferedOutputStream(new FileOutputStream(dest));
      byte[] buffer = new byte[2048];
      for (; ; ) {
        int nBytes = in.read(buffer);
        if (nBytes <= 0) break;
        out.write(buffer, 0, nBytes);
      }
      out.flush();
      return dest;
    } catch (IOException ex) {
      ex.printStackTrace();
      return null;
    } finally {
      try {
        if (out != null) out.close();
        if (in != null) in.close();
      } catch (IOException ex) {
        ex.printStackTrace();
      }
    }
  }
  /**
   * Extract the dependent libs and return the extracted jar files
   *
   * @return the collection of extracted files
   */
  private Collection<File> extractDependentLibs(final File cachedir) throws IOException {
    final Attributes attributes = getMainAttributes();
    if (null == attributes) {
      debug("no manifest attributes");
      return null;
    }

    final ArrayList<File> files = new ArrayList<File>();
    final String libs = attributes.getValue(RUNDECK_PLUGIN_LIBS);
    if (null != libs) {
      debug("jar libs listed: " + libs + " for file: " + pluginJar);
      if (!cachedir.isDirectory()) {
        if (!cachedir.mkdirs()) {
          debug("Failed to create cachedJar dir for dependent libs: " + cachedir);
        }
      }
      final String[] libsarr = libs.split(" ");
      extractJarContents(libsarr, cachedir);
      for (final String s : libsarr) {
        File libFile = new File(cachedir, s);
        libFile.deleteOnExit();
        files.add(libFile);
      }
    } else {
      debug("no jar libs listed in manifest: " + pluginJar);
    }
    return files;
  }
示例#10
0
  @Test
  public void canFollowLogfile() throws IOException {
    File tempFile =
        File.createTempFile("commons-io", "", new File(System.getProperty("java.io.tmpdir")));
    tempFile.deleteOnExit();
    System.out.println("Temp file = " + tempFile.getAbsolutePath());
    PrintStream log = new PrintStream(tempFile);
    LogfileFollower follower = new LogfileFollower(tempFile);
    List<String> lines;

    // Empty file:
    lines = follower.newLines();
    assertEquals(0, lines.size());

    // Write two lines:
    log.println("Line 1");
    log.println("Line 2");
    lines = follower.newLines();
    assertEquals(2, lines.size());
    assertEquals("Line 2", lines.get(1));

    // Write one more line:
    log.println("Line 3");
    lines = follower.newLines();
    assertEquals(1, lines.size());
    assertEquals("Line 3", lines.get(0));

    // Write one and a half line and finish later:
    log.println("Line 4");
    log.print("Line 5 begin");
    lines = follower.newLines();
    assertEquals(1, lines.size());

    // End last line and start a new one:
    log.println(" end");
    log.print("Line 6 begin");
    lines = follower.newLines();
    assertEquals(1, lines.size());
    assertEquals("Line 5 begin end", lines.get(0));

    // End last line:
    log.println(" end");
    lines = follower.newLines();
    assertEquals(1, lines.size());
    assertEquals("Line 6 begin end", lines.get(0));

    // A line only missing a newline:
    log.print("Line 7");
    lines = follower.newLines();
    assertEquals(0, lines.size());
    log.println();
    lines = follower.newLines();
    assertEquals(1, lines.size());
    assertEquals("Line 7", lines.get(0));

    // Delete:
    log.close();
    lines = follower.newLines();
    assertEquals(0, lines.size());
  }
示例#11
0
  protected File createTempFile(JarEntry je) throws IOException {
    byte[] jeBytes = getJarEntryBytes(outerFile, je);

    String tempName = je.getName().replace('/', '_') + ".";
    File tempDir = new File("execwartmp");
    if (tempDir.mkdir()) tempDir.deleteOnExit();
    File file = File.createTempFile("moqui_temp", tempName, tempDir);
    file.deleteOnExit();
    BufferedOutputStream os = null;
    try {
      os = new BufferedOutputStream(new FileOutputStream(file));
      os.write(jeBytes);
    } finally {
      if (os != null) os.close();
    }
    return file;
  }
示例#12
0
 /**
  * Creates a single cached version of the pluginJar located within pluginJarCacheDirectory
  * deleting all existing versions of pluginJar
  *
  * @param jarName
  */
 protected File createCachedJar(final File dir, final String jarName) throws PluginException {
   File cachedJar;
   try {
     cachedJar = new File(dir, jarName);
     cachedJar.deleteOnExit();
     FileUtils.fileCopy(pluginJar, cachedJar, true);
   } catch (IOException e) {
     throw new PluginException(e);
   }
   return cachedJar;
 }
 /**
  * Create a file that contains the specified message
  *
  * @param root a git repository root
  * @param message a message to write
  * @return a file reference
  * @throws IOException if file cannot be created
  */
 private File createMessageFile(VirtualFile root, final String message) throws IOException {
   // filter comment lines
   File file = FileUtil.createTempFile(GIT_COMMIT_MSG_FILE_PREFIX, GIT_COMMIT_MSG_FILE_EXT);
   file.deleteOnExit();
   @NonNls String encoding = GitConfigUtil.getCommitEncoding(myProject, root);
   Writer out = new OutputStreamWriter(new FileOutputStream(file), encoding);
   try {
     out.write(message);
   } finally {
     out.close();
   }
   return file;
 }
    /**
     * Creates a file on the local FS. Pass size as {@link LocalDirAllocator.SIZE_UNKNOWN} if not
     * known apriori. We round-robin over the set of disks (via the configured dirs) and return a
     * file on the first path which has enough space. The file is guaranteed to go away when the JVM
     * exits.
     */
    public File createTmpFileForWrite(String pathStr, long size, Configuration conf)
        throws IOException {

      // find an appropriate directory
      Path path = getLocalPathForWrite(pathStr, size, conf, true);
      File dir = new File(path.getParent().toUri().getPath());
      String prefix = path.getName();

      // create a temp file on this directory
      File result = File.createTempFile(prefix, null, dir);
      result.deleteOnExit();
      return result;
    }
示例#15
0
 private String createTemporaryPreinstallFile() {
   try {
     File tempFile = File.createTempFile("preinstall", null);
     tempFile.deleteOnExit();
     InstallUtil.copyResourceToFile("/gnu/io/installer/resources/macosx/preinstall", tempFile);
     String absPath = tempFile.getAbsolutePath();
     Process p = Runtime.getRuntime().exec(new String[] {"chmod", "a+x", absPath});
     p.waitFor();
     return absPath;
   } catch (Throwable t) {
   }
   return null;
 }
示例#16
0
 private void installAuthKit() {
   boolean usingJWS = false;
   final ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
   if (originalClassLoader.toString().toLowerCase().indexOf("jnlp") != -1) usingJWS = true;
   if (usingJWS) return;
   try {
     File tempFile = new File(jarFolder, "libAuthKit.jnilib");
     tempFile.deleteOnExit();
     InstallUtil.copyResourceToFile(
         "/gnu/io/installer/resources/macosx/lib/libAuthKit.jnilib", tempFile);
   } catch (Throwable t) {
     System.out.println("installAuthKit Throwable " + t);
   }
 }
示例#17
0
文件: Q2.java 项目: rlishtaba/jPOS
  public void deployElement(Element e, String fileName, boolean encrypt, boolean isTransient)
      throws ISOException, IOException, GeneralSecurityException {
    e = ((Element) e.clone());

    XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
    Document doc = new Document();
    doc.setRootElement(e);
    File qbean = new File(deployDir, fileName);
    if (isTransient) {
      e.setAttribute("instance", getInstanceId().toString());
      qbean.deleteOnExit();
    }
    FileWriter writer = new FileWriter(qbean);
    if (encrypt) doc = encrypt(doc);
    out.output(doc, writer);
    writer.close();
  }
 private boolean tryToWriteTestFile() {
   boolean testFileWrite = false;
   File testFile = new File(configDirectory, "tmp" + System.currentTimeMillis());
   try {
     boolean success = testFile.createNewFile();
     if (success) {
       boolean deleted = testFile.delete();
       if (!deleted) {
         testFile.deleteOnExit();
       } else {
         testFileWrite = true;
       }
     }
   } catch (IOException e) {
     ConfigLogImplementation.logMethods.warn(
         "Test file write to config directory failed at " + configDirectory);
   }
   return testFileWrite;
 }
示例#19
0
    public void run() {
      try {
        URL url = new URL(protocol + "://localhost:" + port + "/test1/" + f);
        HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
        if (urlc instanceof HttpsURLConnection) {
          HttpsURLConnection urlcs = (HttpsURLConnection) urlc;
          urlcs.setHostnameVerifier(
              new HostnameVerifier() {
                public boolean verify(String s, SSLSession s1) {
                  return true;
                }
              });
          urlcs.setSSLSocketFactory(ctx.getSocketFactory());
        }
        byte[] buf = new byte[4096];

        if (fixedLen) {
          urlc.setRequestProperty("XFixed", "yes");
        }
        InputStream is = urlc.getInputStream();
        File temp = File.createTempFile("Test1", null);
        temp.deleteOnExit();
        OutputStream fout = new BufferedOutputStream(new FileOutputStream(temp));
        int c, count = 0;
        while ((c = is.read(buf)) != -1) {
          count += c;
          fout.write(buf, 0, c);
        }
        is.close();
        fout.close();

        if (count != size) {
          throw new RuntimeException("wrong amount of data returned");
        }
        String orig = root + "/" + f;
        compare(new File(orig), temp);
        temp.delete();
      } catch (Exception e) {
        e.printStackTrace();
        fail = true;
      }
    }
 // remove temp files, the pipeline.jar operator isn't working.
 public void deleteTempFiles() {
   System.out.println("\nRemoving these temp files:");
   File workingDir = new File(System.getProperty("user.dir"));
   File[] toExamine = workingDir.listFiles();
   for (File f : toExamine) {
     boolean d = false;
     String n = f.getName();
     if (n.startsWith("pipeinstancelog")) d = true;
     else if (n.contains(".DOC.sample")) d = true;
     else if (n.contains("allDepths.")) d = true;
     else if (n.startsWith("nocalls.")) d = true;
     else if (n.startsWith("snpeff.")) d = true;
     else if (n.contains("plice")) d = true;
     if (d) {
       System.out.println("\t" + n);
       f.deleteOnExit();
     }
   }
   // delete the temp uncompressed vcf (required by Pipeline.jar)
   if (deleteTempVcf) System.out.println("\t" + finalVcf.getName());
 }
 private static File jarModulesOutput(
     @NotNull Set<Module> modules,
     @Nullable Manifest manifest,
     final @Nullable String pluginXmlPath)
     throws IOException {
   File jarFile = FileUtil.createTempFile(TEMP_PREFIX, JAR_EXTENSION);
   jarFile.deleteOnExit();
   ZipOutputStream jarPlugin = null;
   try {
     BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(jarFile));
     jarPlugin = manifest != null ? new JarOutputStream(out, manifest) : new JarOutputStream(out);
     final ProgressIndicator progressIndicator =
         ProgressManager.getInstance().getProgressIndicator();
     final Set<String> writtenItemRelativePaths = new HashSet<String>();
     for (Module module : modules) {
       final VirtualFile compilerOutputPath =
           CompilerModuleExtension.getInstance(module).getCompilerOutputPath();
       if (compilerOutputPath == null)
         continue; // pre-condition: output dirs for all modules are up-to-date
       ZipUtil.addDirToZipRecursively(
           jarPlugin,
           jarFile,
           new File(compilerOutputPath.getPath()),
           "",
           createFilter(progressIndicator, FileTypeManager.getInstance()),
           writtenItemRelativePaths);
     }
     if (pluginXmlPath != null) {
       ZipUtil.addFileToZip(
           jarPlugin,
           new File(pluginXmlPath),
           "/META-INF/plugin.xml",
           writtenItemRelativePaths,
           createFilter(progressIndicator, null));
     }
   } finally {
     if (jarPlugin != null) jarPlugin.close();
   }
   return jarFile;
 }
示例#22
0
 @Override
 public void save() {
   NBTOutputStream stream = null;
   try {
     Files.createParentDirs(this.file);
     final File temporaryFile =
         File.createTempFile(this.file.getName(), null, this.file.getParentFile());
     temporaryFile.deleteOnExit();
     stream = new NBTOutputStream(new FileOutputStream(temporaryFile));
     stream.writeTag(new CompoundTag(this.name, this.root));
     stream.close();
     this.file.delete();
     temporaryFile.renameTo(this.file);
     temporaryFile.delete();
   } catch (IOException ex) {
     ex.printStackTrace();
   } finally {
     try {
       stream.close();
     } catch (IOException ex2) {
     }
   }
 }
  private File createDelimitedFile(Set<TestBean> theBeans) {

    File aDelimitedFile = null;

    try {

      // Create a temporary file ...
      aDelimitedFile = createTempFile(this.getClass().getName(), ".csv");
      aDelimitedFile.deleteOnExit();

      // Write the set of beans out to the file ...
      BufferedWriter aWriter = new BufferedWriter(new FileWriter(aDelimitedFile));
      for (TestBean aBean : theBeans) {

        StringBuilder aStringBuilder = new StringBuilder();

        aStringBuilder.append(aBean.getFirstName());
        aStringBuilder.append(DEFAULT_DELIMITER);
        aStringBuilder.append(aBean.getLastName());
        aStringBuilder.append(DEFAULT_DELIMITER);
        aStringBuilder.append(aBean.getFavoriteColor());

        aWriter.append(aStringBuilder);
        aWriter.newLine();
      }

      // File all buffers and close up shop ...
      aWriter.flush();
      aWriter.close();

    } catch (IOException e) {

      fail("Failed to create test delimited file.", e);
    }

    return aDelimitedFile;
  }
 private static void makeAndAddLibraryJar(
     final VirtualFile virtualFile,
     final File zipFile,
     final String pluginName,
     final ZipOutputStream zos,
     final Set<String> usedJarNames,
     final ProgressIndicator progressIndicator,
     final String preferredName)
     throws IOException {
   File libraryJar = FileUtil.createTempFile(TEMP_PREFIX, JAR_EXTENSION);
   libraryJar.deleteOnExit();
   ZipOutputStream jar = null;
   try {
     jar = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(libraryJar)));
     ZipUtil.addFileOrDirRecursively(
         jar,
         libraryJar,
         VfsUtilCore.virtualToIoFile(virtualFile),
         "",
         createFilter(progressIndicator, FileTypeManager.getInstance()),
         null);
   } finally {
     if (jar != null) jar.close();
   }
   final String jarName =
       getLibraryJarName(
           virtualFile.getName() + JAR_EXTENSION,
           usedJarNames,
           preferredName == null ? null : preferredName + JAR_EXTENSION);
   ZipUtil.addFileOrDirRecursively(
       zos,
       zipFile,
       libraryJar,
       getZipPath(pluginName, jarName),
       createFilter(progressIndicator, null),
       null);
 }
示例#25
0
  public StanfordCoreNLPServer(int port) throws IOException {
    serverPort = port;

    defaultProps = new Properties();
    defaultProps.setProperty(
        "annotators", "tokenize, ssplit, pos, lemma, ner, parse, depparse, natlog, openie, dcoref");
    defaultProps.setProperty("inputFormat", "text");
    defaultProps.setProperty("outputFormat", "json");

    // Generate and write a shutdown key
    String tmpDir = System.getProperty("java.io.tmpdir");
    File tmpFile = new File(tmpDir + File.separator + "corenlp.shutdown");
    tmpFile.deleteOnExit();
    if (tmpFile.exists()) {
      if (!tmpFile.delete()) {
        throw new IllegalStateException("Could not delete shutdown key file");
      }
    }
    this.shutdownKey = new BigInteger(130, new Random()).toString(32);
    IOUtils.writeStringToFile(shutdownKey, tmpFile.getPath(), "utf-8");

    // Set the static page handler
    this.staticPageHandle = new FileHandler("edu/stanford/nlp/pipeline/demo/corenlp-brat.html");
  }
示例#26
0
 public Graph neighbourhoodGraph(int nnodes[], int hops) {
   PrimaryHashMap<Integer, String> nodes;
   PrimaryHashMap<String, Integer> nodesReverse;
   try {
     File auxFile = File.createTempFile("graph-maps-" + System.currentTimeMillis(), "aux");
     auxFile.deleteOnExit();
     RecordManager recMan = RecordManagerFactory.createRecordManager(auxFile.getAbsolutePath());
     nodes = recMan.hashMap("nodes");
     nodesReverse = recMan.hashMap("nodesReverse");
   } catch (IOException ex) {
     throw new Error(ex);
   }
   nodes.clear();
   nodesReverse.clear();
   WeightedArcSet list1 = new WeightedArcSet();
   Int2IntAVLTreeMap map = new Int2IntAVLTreeMap();
   IntSet set = new IntLinkedOpenHashSet();
   int numIterators = 100;
   Constructor[] cons = WeightedArc.class.getDeclaredConstructors();
   for (int i = 0; i < cons.length; i++) cons[i].setAccessible(true);
   for (int n : nnodes) map.put(n, 0);
   NodeIterator its[] = new NodeIterator[numIterators];
   int itNum[] = new int[numIterators];
   for (int n = 0; n < its.length; n++) {
     its[n] = nodeIterator();
     itNum[n] = 0;
   }
   while (map.size() != 0) {
     Integer node = 0;
     for (int n = 0; n < its.length; n++) if (itNum[n] <= node) node = itNum[n];
     node = map.tailMap(node).firstKey();
     if (node == null) map.firstKey();
     NodeIterator it = null;
     Integer aux1 = 0;
     int iit = 0;
     for (int n = 0; n < its.length; n++) {
       if (!its[n].hasNext()) {
         its[n] = nodeIterator();
         itNum[n] = 0;
       }
       if (itNum[n] == node) {
         it = its[n];
         aux1 = itNum[n];
         iit = 0;
         break;
       }
       if (itNum[n] < node && itNum[n] >= aux1) {
         it = its[n];
         aux1 = itNum[n];
         iit = n;
       }
     }
     if (it == null) {
       its[0] = nodeIterator();
       itNum[0] = 0;
       it = its[0];
     }
     while (it != null && (aux1 = it.nextInt()) != null && aux1 >= 0 && aux1 < node) {}
     itNum[iit] = aux1 + 1;
     Integer aux2 = null;
     ArcLabelledNodeIterator.LabelledArcIterator suc = it.successors();
     while ((aux2 = suc.nextInt()) != null && aux2 >= 0 && (aux2 < graph.numNodes()))
       try {
         if (commit++ % COMMIT_SIZE == 0) {
           try {
             nodes.getRecordManager().commit();
           } catch (IOException e) {
             throw new Error(e);
           }
           try {
             nodesReverse.getRecordManager().commit();
           } catch (IOException e) {
             throw new Error(e);
           }
         }
         if (!nodesReverse.containsKey(this.nodes.get(aux1))) {
           nodes.put(nodes.size(), this.nodes.get(aux1));
           nodesReverse.put(this.nodes.get(aux1), nodesReverse.size());
         }
         if (!nodesReverse.containsKey(this.nodes.get(aux2))) {
           nodes.put(nodes.size(), this.nodes.get(aux2));
           nodesReverse.put(this.nodes.get(aux2), nodesReverse.size());
         }
         int aaux1 = nodesReverse.get(this.nodes.get(aux1));
         int aaux2 = nodesReverse.get(this.nodes.get(aux2));
         WeightedArc arc1 =
             (WeightedArc) cons[0].newInstance(aaux1, aaux2, suc.label().getFloat());
         list1.add(arc1);
         if (map.get(node) < hops) {
           if (!set.contains(aux1) && (map.get(aux1) == null || map.get(aux1) > map.get(node) + 1))
             map.put(aux1.intValue(), map.get(node) + 1);
           if (!set.contains(aux2) && (map.get(aux2) == null || map.get(aux2) > map.get(node) + 1))
             map.put(aux2.intValue(), map.get(node) + 1);
         }
       } catch (Exception ex) {
         ex.printStackTrace();
         throw new Error(ex);
       }
     ArcLabelledNodeIterator.LabelledArcIterator anc = it.ancestors();
     while ((aux2 = anc.nextInt()) != null && aux2 >= 0 && (aux2 < graph.numNodes()))
       try {
         if (commit++ % COMMIT_SIZE == 0) {
           try {
             nodes.getRecordManager().commit();
           } catch (IOException e) {
             throw new Error(e);
           }
           try {
             nodesReverse.getRecordManager().commit();
           } catch (IOException e) {
             throw new Error(e);
           }
         }
         if (!nodesReverse.containsKey(this.nodes.get(aux1))) {
           nodes.put(nodes.size(), this.nodes.get(aux1));
           nodesReverse.put(this.nodes.get(aux1), nodesReverse.size());
         }
         if (!nodesReverse.containsKey(this.nodes.get(aux2))) {
           nodes.put(nodes.size(), this.nodes.get(aux2));
           nodesReverse.put(this.nodes.get(aux2), nodesReverse.size());
         }
         int aaux1 = nodesReverse.get(this.nodes.get(aux1));
         int aaux2 = nodesReverse.get(this.nodes.get(aux2));
         WeightedArc arc1 =
             (WeightedArc) cons[0].newInstance(aaux2, aaux1, anc.label().getFloat());
         list1.add(arc1);
         if (map.get(node) < hops) {
           if (!set.contains(aux1) && (map.get(aux1) == null || map.get(aux1) > map.get(node) + 1))
             map.put(aux1.intValue(), map.get(node) + 1);
           if (!set.contains(aux2) && (map.get(aux2) == null || map.get(aux2) > map.get(node) + 1))
             map.put(aux2.intValue(), map.get(node) + 1);
         }
       } catch (Exception ex) {
         ex.printStackTrace();
         throw new Error(ex);
       }
     map.remove(node);
     set.add(node);
   }
   Graph newGraph = new Graph(list1.toArray(new WeightedArc[0]));
   newGraph.nodes.clear();
   newGraph.nodesReverse.clear();
   newGraph.nodes = nodes;
   newGraph.nodesReverse = nodesReverse;
   return newGraph;
 }
示例#27
0
 public Graph(String file) throws IOException {
   org.apache.log4j.Logger logger =
       org.apache.log4j.Logger.getLogger("it.unimi.dsi.webgraph.ImmutableGraph");
   logger.setLevel(org.apache.log4j.Level.FATAL);
   try {
     File auxFile = File.createTempFile("graph-maps-" + System.currentTimeMillis(), "aux");
     auxFile.deleteOnExit();
     RecordManager recMan = RecordManagerFactory.createRecordManager(auxFile.getAbsolutePath());
     nodes = recMan.hashMap("nodes");
     nodesReverse = recMan.hashMap("nodesReverse");
   } catch (IOException ex) {
     throw new Error(ex);
   }
   nodes.clear();
   nodesReverse.clear();
   Constructor[] cons = WeightedArc.class.getDeclaredConstructors();
   for (int i = 0; i < cons.length; i++) cons[i].setAccessible(true);
   String aux = null;
   Float weight = (float) 1.0;
   WeightedArcSet list = new WeightedArcSet();
   BufferedReader br;
   try {
     br =
         new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file))));
   } catch (Exception ex) {
     br = new BufferedReader(new FileReader(file));
   }
   while ((aux = br.readLine()) != null)
     try {
       if (commit++ % COMMIT_SIZE == 0) {
         commit();
         list.commit();
       }
       String parts[] = aux.split("\t");
       String l1 = new String(parts[0]);
       String l2 = new String(parts[1]);
       if (!nodesReverse.containsKey(l1)) {
         nodesReverse.put(l1, nodesReverse.size());
         nodes.put(nodes.size(), l1);
       }
       if (!nodesReverse.containsKey(l2)) {
         nodesReverse.put(l2, nodesReverse.size());
         nodes.put(nodes.size(), l2);
       }
       if (parts.length == 3) weight = new Float(parts[2]);
       list.add(
           (WeightedArc) cons[0].newInstance(nodesReverse.get(l1), nodesReverse.get(l2), weight));
     } catch (Exception ex) {
       throw new Error(ex);
     }
   this.graph = new WeightedBVGraph(list.toArray(new WeightedArc[0]));
   br.close();
   list = new WeightedArcSet();
   br = new BufferedReader(new FileReader(file));
   while ((aux = br.readLine()) != null)
     try {
       if (commit++ % COMMIT_SIZE == 0) {
         commit();
         list.commit();
       }
       String parts[] = aux.split("\t");
       String l1 = new String(parts[0]);
       String l2 = new String(parts[1]);
       if (parts.length == 3) weight = new Float(parts[2]);
       list.add(
           (WeightedArc) cons[0].newInstance(nodesReverse.get(l2), nodesReverse.get(l1), weight));
     } catch (Exception ex) {
       throw new Error(ex);
     }
   br.close();
   this.reverse = new WeightedBVGraph(list.toArray(new WeightedArc[0]));
   numArcs = list.size();
   iterator = nodeIterator();
   try {
     File auxFile = File.createTempFile("graph" + System.currentTimeMillis(), "aux");
     auxFile.deleteOnExit();
     String basename = auxFile.getAbsolutePath();
     store(basename);
   } catch (IOException ex) {
     throw new Error(ex);
   }
   commit();
 }
示例#28
0
 public Graph(BVGraph graph) {
   org.apache.log4j.Logger logger =
       org.apache.log4j.Logger.getLogger("it.unimi.dsi.webgraph.ImmutableGraph");
   logger.setLevel(org.apache.log4j.Level.FATAL);
   try {
     File auxFile = File.createTempFile("graph-maps-" + System.currentTimeMillis(), "aux");
     auxFile.deleteOnExit();
     RecordManager recMan = RecordManagerFactory.createRecordManager(auxFile.getAbsolutePath());
     nodes = recMan.hashMap("nodes");
     nodesReverse = recMan.hashMap("nodesReverse");
   } catch (IOException ex) {
     throw new Error(ex);
   }
   nodes.clear();
   nodesReverse.clear();
   Constructor[] cons = WeightedArc.class.getDeclaredConstructors();
   for (int i = 0; i < cons.length; i++) cons[i].setAccessible(true);
   Integer aux1 = null;
   WeightedArcSet list = new WeightedArcSet();
   it.unimi.dsi.webgraph.NodeIterator it = graph.nodeIterator();
   while ((aux1 = it.nextInt()) != null) {
     LazyIntIterator suc = it.successors();
     Integer aux2 = null;
     while ((aux2 = suc.nextInt()) != null && aux2 >= 0 && (aux2 < graph.numNodes()))
       try {
         if (commit++ % COMMIT_SIZE == 0) {
           list.commit();
         }
         list.add((WeightedArc) cons[0].newInstance(aux1, aux2, (float) 1.0));
       } catch (Exception ex) {
         throw new Error(ex);
       }
   }
   this.graph = new WeightedBVGraph(list.toArray(new WeightedArc[0]));
   list = new WeightedArcSet();
   it = graph.nodeIterator();
   while ((aux1 = it.nextInt()) != null) {
     LazyIntIterator suc = it.successors();
     Integer aux2 = null;
     while ((aux2 = suc.nextInt()) != null && aux2 >= 0 && (aux2 < graph.numNodes()))
       try {
         if (commit++ % COMMIT_SIZE == 0) {
           commit();
           list.commit();
         }
         list.add((WeightedArc) cons[0].newInstance(aux2, aux1, (float) 1.0));
         this.nodes.put(aux1, "" + aux1);
         this.nodes.put(aux2, "" + aux2);
         this.nodesReverse.put("" + aux1, aux1);
         this.nodesReverse.put("" + aux2, aux2);
       } catch (Exception ex) {
         throw new Error(ex);
       }
   }
   this.reverse = new WeightedBVGraph(list.toArray(new WeightedArc[0]));
   numArcs = list.size();
   iterator = nodeIterator();
   try {
     File auxFile = File.createTempFile("graph" + System.currentTimeMillis(), "aux");
     auxFile.deleteOnExit();
     String basename = auxFile.getAbsolutePath();
     store(basename);
   } catch (IOException ex) {
     throw new Error(ex);
   }
   commit();
 }
    protected URL writeConfig(ConfigData configuration, String fileName) throws Exception {
      File configFile = new File(configDirectory, fileName);
      File backupFile = new File(configDirectory, fileName + ".bak");
      ConfigLogImplementation.logMethods.info("Writing configuration file at " + configFile);
      checkConfigDirectoryWritable();
      checkConfigFileWritableIfExists(configFile);
      checkConfigFileWritableIfExists(backupFile);

      File tempConfigFile = null;
      FileOutputStream fos = null;
      try {
        ConfigLogImplementation.logMethods.debug("About to create temp file");
        tempConfigFile = File.createTempFile("tempConfig", "." + extension, configDirectory);
        tempConfigFile.deleteOnExit();

        ConfigLogImplementation.logMethods.debug("About to write: " + tempConfigFile);

        try {
          fos = new FileOutputStream(tempConfigFile);
          writeConfigToStream(fos, configuration.getSerializedConfig(), configuration.getVersion());
        } finally {
          if (fos != null) {
            try {
              fos.close();
            } catch (Exception e) {
              ConfigLogImplementation.logMethods.error(
                  "Failed to close out file stream to temp file " + tempConfigFile.getPath(), e);
            }
          }
        }
        ConfigLogImplementation.logMethods.debug("Written: " + tempConfigFile);

        if (backupFile.exists()) {
          ConfigLogImplementation.logMethods.debug("About to delete " + backupFile);
          boolean deleted = backupFile.delete();
          if (deleted) {
            ConfigLogImplementation.logMethods.debug("Deleted " + backupFile);
          } else {
            ConfigLogImplementation.logMethods.warn("Failed to delete " + backupFile);
          }
        }

        if (configFile.exists()) {
          ConfigLogImplementation.logMethods.debug(
              "About to rename old config: " + configFile + " to " + backupFile);
          if (!configFile.renameTo(backupFile)) {
            throw new IOException(
                "Unable to rename old config: " + configFile + " to " + backupFile);
          }
        }

        ConfigLogImplementation.logMethods.debug(
            "About to rename temp config: " + tempConfigFile + " to " + configFile);
        if (!tempConfigFile.renameTo(configFile)) {
          throw new IOException(
              "Unable to rename temp config: " + tempConfigFile + " to new config: " + configFile);
        }
      } catch (IOException e) {
        ConfigLogImplementation.logMethods.error("Unable to save config: " + configFile, e);
        throw new ConfigManagerException("Unable to save config", e);
      }
      return configFile.toURI().toURL();
    }
示例#30
0
  public void toTDF(
      String typeString,
      String ifile,
      String ofile,
      String probeFile,
      String genomeId,
      int maxZoomValue,
      Collection<WindowFunction> windowFunctions,
      String tmpDirName,
      int maxRecords)
      throws IOException, PreprocessingException {

    if (!ifile.endsWith(".affective.csv")) validateIsTilable(typeString);

    System.out.println("toTDF.  File = " + ifile);
    System.out.println("Max zoom = " + maxZoomValue);
    if (probeFile != null && probeFile.trim().length() > 0) {
      System.out.println("Probe file = " + probeFile);
    }
    System.out.print("Window functions: ");
    for (WindowFunction wf : windowFunctions) {
      System.out.print(wf.toString() + " ");
    }
    System.out.println();

    boolean isGCT = isGCT(typeString);
    Genome genome = loadGenome(genomeId, isGCT);
    if (genome == null) {
      throw new PreprocessingException("Genome could not be loaded: " + genomeId);
    }
    File inputFileOrDir = new File(ifile);

    // Estimae the total number of lines to be parsed, for progress updates
    int nLines = estimateLineCount(inputFileOrDir);

    // TODO -- move this block of code out of here, this should be done before calling this method
    // Convert  gct files to igv format first
    File deleteme = null;
    if (isGCT(typeString)) {
      File tmpDir = null;
      if (tmpDirName != null && tmpDirName.length() > 0) {
        tmpDir = new File(tmpDirName);
        if (!tmpDir.exists() || !tmpDir.isDirectory()) {
          throw new PreprocessingException(
              "Specified tmp directory does not exist or is not directory: " + tmpDirName);
        }
      } else {
        tmpDir = new File(System.getProperty("java.io.tmpdir"), System.getProperty("user.name"));
      }
      if (!tmpDir.exists()) {
        tmpDir.mkdir();
      }

      String baseName = (new File(ifile)).getName();
      File igvFile = new File(tmpDir, baseName + ".igv");
      igvFile.deleteOnExit();
      doGCTtoIGV(typeString, ifile, igvFile, probeFile, maxRecords, tmpDirName, genome);

      inputFileOrDir = igvFile;
      deleteme = igvFile;
      typeString = ".igv";
    }

    // Convert to tdf
    File outputFile = new File(ofile);
    try {
      Preprocessor p = new Preprocessor(outputFile, genome, windowFunctions, nLines, null);
      if (inputFileOrDir.isDirectory() || inputFileOrDir.getName().endsWith(".list")) {
        List<File> files = getFilesFromDirOrList(inputFileOrDir);
        for (File f : files) {
          p.preprocess(f, maxZoomValue, typeString);
        }
      } else {
        p.preprocess(inputFileOrDir, maxZoomValue, typeString);
      }
      p.finish();
    } catch (IOException e) {
      e.printStackTrace();
      // Delete output file as its probably corrupt
      if (outputFile.exists()) {
        outputFile.delete();
      }
    } finally {
      if (deleteme != null && deleteme.exists()) {
        deleteme.delete();
      }
    }

    System.out.flush();
  }