List of usage examples for org.apache.commons.io IOUtils lineIterator
public static LineIterator lineIterator(Reader reader)
Reader
. From source file:org.jenkinsci.plugins.autozoil.AutozoilSource.java
/** * Splits the source code into three blocks: the line to highlight and the * source code before and after this line. * * @param sourceFile the source code of the whole file as rendered HTML string *///from w w w. ja v a 2s . c o m private void splitSourceFile(final String sourceFile) { StringBuilder output = new StringBuilder(sourceFile.length()); AutozoilFile baseAutozoilFile = autozoilWorkspaceFile.getAutozoilFile(); LineIterator lineIterator = IOUtils.lineIterator(new StringReader(sourceFile)); int lineNumber = 1; //---header while (lineNumber < SOURCE_GENERATOR_OFFSET) { copyLine(output, lineIterator); lineNumber++; } lineNumber = 1; //---iterate before the error line //while (lineNumber < autozoilFile.getLineNumber()) { while (lineIterator.hasNext()) { if (lineNumberToAutozoilFilesMap.keySet().contains(lineNumber)) { output.append("</code>\n"); output.append("</td></tr>\n"); output.append("<tr><td bgcolor=\""); if (lineNumber == baseAutozoilFile.getLineNumber()) { appendRangeColor(output); } else { appendRangeSecondaryColor(output); } output.append("\">\n"); output.append("<div tooltip=\""); outputEscaped(output, conciseHintFromAutozoilFiles(lineNumberToAutozoilFilesMap.get(lineNumber))); output.append("\" nodismiss=\"\">\n"); output.append("<code><b>\n"); copyLineWithHighlightedContext(output, lineIterator, MESSAGE_COLOR, lineNumberToAutozoilFilesMap.get(lineNumber)); lineNumber++; output.append("</b></code>\n"); output.append("</div>\n"); output.append("</td></tr>\n"); output.append("<tr><td>\n"); output.append("<code>\n"); continue; } copyLine(output, lineIterator); lineNumber++; } output.append("</code>\n"); output.append("</td></tr>\n"); sourceCode = output.toString(); }
From source file:org.nd4j.linalg.factory.Nd4j.java
/** * Read line via input streams/* w w w.j av a 2s . co m*/ * * @param ndarray the input stream ndarray * @param sep character, defaults to "," * @return NDArray */ public static INDArray readTxtString(InputStream ndarray, String sep) { /* We could dump an ndarray to a file with the tostring (since that is valid json) and use put/get to parse it as json But here we leverage our information of the tostring method to be more efficient With our current toString format we use tads along dimension (rank-1,rank-2) to write to the array in two dimensional chunks at a time. This is more efficient than setting each value at a time with putScalar. This also means we can read the file one line at a time instead of loading the whole thing into memory Future work involves enhancing the write json method to provide more features to make the load more efficient */ int lineNum = 0; int rowNum = 0; int tensorNum = 0; char theOrder = 'c'; int[] theShape = { 1, 1 }; int rank = 0; double[][] subsetArr = { { 0.0, 0.0 }, { 0.0, 0.0 } }; INDArray newArr = Nd4j.zeros(2, 2); BufferedReader reader = new BufferedReader(new InputStreamReader(ndarray)); LineIterator it = IOUtils.lineIterator(reader); DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.US); format.setParseBigDecimal(true); try { while (it.hasNext()) { String line = it.nextLine(); lineNum++; line = line.replaceAll("\\s", ""); if (line.equals("") || line.equals("}")) continue; // is it from dl4j? if (lineNum == 2) { String[] lineArr = line.split(":"); String fileSource = lineArr[1].replaceAll("\\W", ""); if (!fileSource.equals("dl4j")) return null; } // parse ordering if (lineNum == 3) { String[] lineArr = line.split(":"); theOrder = lineArr[1].replaceAll("\\W", "").charAt(0); continue; } // parse shape if (lineNum == 4) { String[] lineArr = line.split(":"); String dropJsonComma = lineArr[1].split("]")[0]; String[] shapeString = dropJsonComma.replace("[", "").split(","); rank = shapeString.length; theShape = new int[rank]; for (int i = 0; i < rank; i++) { try { theShape[i] = Integer.parseInt(shapeString[i]); } catch (NumberFormatException nfe) { } ; } subsetArr = new double[theShape[rank - 2]][theShape[rank - 1]]; newArr = Nd4j.zeros(theShape, theOrder); continue; } //parse data if (lineNum > 5) { String[] entries = line.replace("\\],", "").replaceAll("\\[", "").replaceAll("\\],", "") .replaceAll("\\]", "").split(sep); for (int i = 0; i < theShape[rank - 1]; i++) { try { BigDecimal number = (BigDecimal) format.parse(entries[i]); subsetArr[rowNum][i] = number.doubleValue(); } catch (ParseException e) { e.printStackTrace(); } } rowNum++; if (rowNum == theShape[rank - 2]) { INDArray subTensor = Nd4j.create(subsetArr); newArr.tensorAlongDimension(tensorNum, rank - 1, rank - 2).addi(subTensor); rowNum = 0; tensorNum++; } } } } finally { LineIterator.closeQuietly(it); } return newArr; }
From source file:org.orbisgis.view.toc.actions.cui.legend.components.ColorScheme.java
/** * Fills the inner collections using the dedicated resource file. *///from w w w. j a v a2 s . c om private static void load() { rangeColorSchemeNames = new ArrayList<>(); discreteColorSchemeNames = new ArrayList<>(); nameToColorsMap = new HashMap<>(); InputStream stream = ColorScheme.class.getResourceAsStream("ColorScheme.txt"); if (stream != null) { InputStreamReader br = new InputStreamReader(stream); LineIterator lineIterator = IOUtils.lineIterator(br); try { while (lineIterator.hasNext()) { String line = lineIterator.nextLine(); add(line); } } finally { LineIterator.closeQuietly(lineIterator); } } }
From source file:org.orbisgis.view.toc.actions.cui.legends.components.ColorScheme.java
/** * Fills the inner collections using the dedicated resource file. *//*from w w w. j av a 2 s . c o m*/ private static void load() { rangeColorSchemeNames = new ArrayList<String>(); discreteColorSchemeNames = new ArrayList<String>(); nameToColorsMap = new HashMap<String, List<Color>>(); InputStream stream = ColorScheme.class.getResourceAsStream("ColorScheme.txt"); InputStreamReader br = new InputStreamReader(stream); LineIterator lineIterator = IOUtils.lineIterator(br); try { while (lineIterator.hasNext()) { String line = lineIterator.next(); add(line); } } finally { LineIterator.closeQuietly(lineIterator); } }
From source file:org.trend.hgraph.mapreduce.pagerank.DriverTest.java
private static void generateGraphDataTest(String dataPath, String tableName, String cq, boolean isDouble) throws IOException { InputStream data = DriverTest.class.getClassLoader().getResourceAsStream(dataPath); LineIterator it = IOUtils.lineIterator(new InputStreamReader(data)); String record = null;// w w w. ja v a2 s . co m Assert.assertEquals(true, it.hasNext()); HTable table = null; try { table = new HTable(TEST_UTIL.getConfiguration(), tableName); table.setAutoFlush(false); Put put = null; String[] values = null; while (it.hasNext()) { record = it.next(); values = record.split("\\|"); Assert.assertNotNull(values); Assert.assertEquals(2, values.length); put = new Put(Bytes.toBytes(values[0])); if (isDouble) { put.add(Bytes.toBytes(HBaseGraphConstants.HBASE_GRAPH_TABLE_COLFAM_PROPERTY_NAME), Bytes.toBytes(cq + HBaseGraphConstants.HBASE_GRAPH_TABLE_COLFAM_PROPERTY_NAME_DELIMITER + "Double"), Bytes.toBytes(Double.parseDouble(values[1]))); } else { put.add(Bytes.toBytes(HBaseGraphConstants.HBASE_GRAPH_TABLE_COLFAM_PROPERTY_NAME), Bytes.toBytes(cq + HBaseGraphConstants.HBASE_GRAPH_TABLE_COLFAM_PROPERTY_NAME_DELIMITER + "String"), Bytes.toBytes(values[1])); } table.put(put); } table.flushCommits(); } catch (IOException e) { System.err.println("generate data for table:" + tableName + " failed"); e.printStackTrace(System.err); throw e; } finally { table.close(); LineIterator.closeQuietly(it); IOUtils.closeQuietly(data); } }
From source file:org.trend.hgraph.mapreduce.pagerank.LocalPrTest.java
private static List<V> generateVs(String vertexData, String edgeData) { InputStream data = DriverTest.class.getClassLoader().getResourceAsStream(vertexData); LineIterator it = IOUtils.lineIterator(new InputStreamReader(data)); // load all Vs List<V> vs = new ArrayList<V>(); String record = null;/*w w w .ja v a2 s.c om*/ String[] values = null; V v = null; while (it.hasNext()) { record = it.next(); values = record.split("\\|"); Assert.assertNotNull(values); Assert.assertEquals(2, values.length); v = new V(); v.key = values[0]; v.pr = Double.parseDouble(values[1]); vs.add(v); } LineIterator.closeQuietly(it); IOUtils.closeQuietly(data); // build adjacency list data = DriverTest.class.getClassLoader().getResourceAsStream(edgeData); it = IOUtils.lineIterator(new InputStreamReader(data)); while (it.hasNext()) { record = it.next(); values = record.split("\\|"); Assert.assertNotNull(values); Assert.assertEquals(2, values.length); values = values[0].split(HBaseGraphConstants.HBASE_GRAPH_TABLE_EDGE_DELIMITER_1); Assert.assertNotNull(values); Assert.assertEquals(3, values.length); for (V tv1 : vs) { if (tv1.key.equals(values[0])) { for (V tv2 : vs) { if (tv2.key.equals(values[2])) { tv1.al.add(tv2); break; } } break; } } } LineIterator.closeQuietly(it); IOUtils.closeQuietly(data); System.out.println("Vs=\n" + vs); return vs; }
From source file:org.trend.hgraph.util.MoveEntities.java
private static void moveEntities(String f, Configuration conf, String st, String dt) throws IOException { Reader fr = null;/*from w w w . ja v a 2 s . c om*/ LineIterator it = null; String key = null; HTable sTable = null; HTable dTable = null; try { fr = new FileReader(f); } catch (FileNotFoundException e) { System.err.println("file:" + f + " does not exist"); e.printStackTrace(System.err); throw e; } try { it = IOUtils.lineIterator(fr); if (it.hasNext()) { sTable = new HTable(conf, st); dTable = new HTable(conf, dt); Get get = null; Put put = null; Result r = null; byte[] bKey = null; byte[] cf = null; byte[] cq = null; byte[] v = null; NavigableMap<byte[], NavigableMap<byte[], byte[]>> cfMap = null; NavigableMap<byte[], byte[]> cqMap = null; Entry<byte[], NavigableMap<byte[], byte[]>> cfEntry = null; Entry<byte[], byte[]> cqEntry = null; long cnt = 0L; while (it.hasNext()) { key = it.next(); bKey = Bytes.toBytes(key); get = new Get(bKey); r = sTable.get(get); if (r.isEmpty()) { String msg = "result is empty for key:" + key + " from table:" + sTable; System.err.println(msg); throw new IllegalStateException(msg); } // dump the values from r to p put = new Put(bKey); cfMap = r.getNoVersionMap(); for (Iterator<Entry<byte[], NavigableMap<byte[], byte[]>>> cfIt = cfMap.entrySet() .iterator(); cfIt.hasNext();) { cfEntry = cfIt.next(); cf = cfEntry.getKey(); cqMap = cfEntry.getValue(); for (Iterator<Entry<byte[], byte[]>> cqIt = cqMap.entrySet().iterator(); cqIt.hasNext();) { cqEntry = cqIt.next(); cq = cqEntry.getKey(); v = cqEntry.getValue(); put.add(cf, cq, v); } } dTable.put(put); cnt++; } System.out.println("put " + cnt + " row(s) into table:" + dt); } } catch (IOException e) { System.err.println("error occurs while doing HBase manipultations !!"); e.printStackTrace(System.err); throw e; } finally { LineIterator.closeQuietly(it); IOUtils.closeQuietly(fr); if (null != sTable) { sTable.close(); } if (null != dTable) { dTable.close(); } } }
From source file:uk.ac.ebi.intact.jami.ontology.iterator.LineOntologyIterator.java
public LineOntologyIterator(Reader reader) { this.reader = reader; lineIterator = IOUtils.lineIterator(reader); processNextLine(); }
From source file:yet.another.hackernews.reader.functions.DownloadText.java
@Override protected String doInBackground(String... urlAdress) { /*/*from w w w .j ava2s .c o m*/ * Core of the class that does the background task. */ id = urlAdress[0].hashCode(); // If we want to use the cache, try and get it first String cacheResults = null; if (useCache) { cacheResults = HardCache.getString(id, context); if (cacheResults != null) { if (!forceUpdate && !Cache.doUpdate(context, id)) { return cacheResults; } } } // Set target url URL url; try { url = new URL(urlAdress[0]); } catch (MalformedURLException e) { // If listener is attached, report error. if (listener != null) { handler.post(new Runnable() { @Override public void run() { listener.onError(MALFORMED_URL_ERROR); } }); } return cacheResults; } // Stream to URL adress BufferedReader bufferedReader = null; LineIterator line = null; // Results, size set to 100 chars final StringBuffer textResults = new StringBuffer(0); try { // Open stream bufferedReader = new BufferedReader(new InputStreamReader(url.openStream(), charset), bufferSize); // Download text line = IOUtils.lineIterator(bufferedReader); while (line.hasNext()) { if (this.isCancelled()) { break; } textResults.append(line.nextLine()); } } catch (IOException e) { if (textResults.length() > 0) { textResults.delete(0, textResults.length()); } if (cacheResults != null) { textResults.append(cacheResults); } if (listener != null) { handler.post(new Runnable() { @Override public void run() { listener.onError(IOEXCEPTION_ERROR); } }); } } finally { // Close reader LineIterator.closeQuietly(line); IOUtils.closeQuietly(bufferedReader); } if (textResults.length() <= 0) { return null; } // If use cache, put it in cache if (useCache && (!textResults.toString().equals(cacheResults))) { HardCache.putString(id, textResults.toString(), context); Cache.saveUpdate(context, id); } return textResults.toString(); }