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(); }
/** * Returns the system temporary folder, e.g. /tmp */ public static File tmp() { try { return File.createTempFile("h2o", null).getParentFile(); } catch( IOException e ) { throw new RuntimeException(e); } }
public static File writeFile(String content) { try { return writeFile(File.createTempFile("h2o", null), content); } catch( IOException e ) { throw Log.errRTExcept(e); } }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { String filePickerResult = ""; if (data != null && resultCode == RESULT_OK) { try { ContentResolver cr = getContentResolver(); Uri uri = data.getData(); Cursor cursor = GeckoApp.mAppContext .getContentResolver() .query(uri, new String[] {OpenableColumns.DISPLAY_NAME}, null, null, null); String name = null; if (cursor != null) { try { if (cursor.moveToNext()) { name = cursor.getString(0); } } finally { cursor.close(); } } String fileName = "tmp_"; String fileExt = null; int period; if (name == null || (period = name.lastIndexOf('.')) == -1) { String mimeType = cr.getType(uri); fileExt = "." + GeckoAppShell.getExtensionFromMimeType(mimeType); } else { fileExt = name.substring(period); fileName = name.substring(0, period); } File file = File.createTempFile(fileName, fileExt, sGREDir); FileOutputStream fos = new FileOutputStream(file); InputStream is = cr.openInputStream(uri); byte[] buf = new byte[4096]; int len = is.read(buf); while (len != -1) { fos.write(buf, 0, len); len = is.read(buf); } fos.close(); filePickerResult = file.getAbsolutePath(); } catch (Exception e) { Log.e(LOG_FILE_NAME, "showing file picker", e); } } try { mFilePickerResult.put(filePickerResult); } catch (InterruptedException e) { Log.i(LOG_FILE_NAME, "error returning file picker result", e); } }
@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) { } } }
/** * Build and return a temporary file in the same directory and with the same extension as the * specified file. */ protected File buildTempFile(File file) throws IOException { int i = file.getName().lastIndexOf('.'); String ext = ((i == -1) ? ".zip" : file.getName().substring(i)); File tempFile = File.createTempFile("temp", ext, file.getParentFile()); return tempFile; }
/** * This method handles PUT requests from the client. PUT requests will come from the applet * portion of this application and are the way that images and other files can be posted to the * server. * * @param request the HTTP request object * @param response the HTTP response object * @exception ServletException * @exception IOException */ public void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); /* * The Scan applet will zip all the files together to create a * faster upload and to use just one server connection. */ ZipInputStream in = new ZipInputStream(request.getInputStream()); /* * This will write all the files to a directory on the server. */ try { try { File file = new File("scan"); Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(file)); document.open(); String fileSize = null; while (true) { ZipEntry entry = in.getNextEntry(); if (entry == null) { break; } File f = File.createTempFile("translate", entry.getName()); FileOutputStream out = new FileOutputStream(f); FileInputStream inStream = null; try { int read; byte[] buf = new byte[2024]; while ((read = in.read(buf)) > 0) { out.write(buf, 0, read); } out.close(); inStream = new FileInputStream(f); System.out.println(entry.getSize()); byte[] b = new byte[inStream.available()]; inStream.read(b); System.out.println(b.length); com.itextpdf.text.Image image1 = com.itextpdf.text.Image.getInstance(b); image1.scalePercent(30); image1.setCompressionLevel(9); document.add(image1); } finally { } } document.close(); // fileSize = CommonUtils.getFileSize(file); // DocumentStoreLogDAO documentStoreLogDAO = new DocumentStoreLogDAO(); // DocumentStoreLog documentStoreLog = null; // DocumentStoreLog documentStore = // (DocumentStoreLog)session.getAttribute(CommonConstants.DOCUMENT_STORE_LOG); // if(null != documentStore) { // documentStore.setFileSize(fileSize); // } // Transaction tx = documentStoreLogDAO.getSession().getTransaction(); // tx.begin(); // documentStoreLogDAO.save(documentStore); // tx.commit(); } catch (ZipException ze) { /* * We want to catch each sip exception separately because * there is a possibility that we can read more files from * the archive even if one of them is corrupted. */ ze.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } finally { in.close(); } /* * Now that we have finished uploading the files * we will send a reponse to the server indicating * our success. */ response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); out.println("<html><head><title>ImageSrv</title></head></html>"); out.flush(); out.close(); response.setStatus(HttpServletResponse.SC_OK); }
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; }
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(); }
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(); }
/** * Creates a new empty temporary file in the same directory as the specified file. A variant of * File.createTempFile. */ private static File createTempFileInSameDirectoryAs(File file) throws IOException { File dir = file.getParentFile(); if (dir == null) dir = new File("."); return File.createTempFile("jartmp", null, dir); }