Example usage for org.apache.commons.io LineIterator hasNext

List of usage examples for org.apache.commons.io LineIterator hasNext

Introduction

In this page you can find the example usage for org.apache.commons.io LineIterator hasNext.

Prototype

public boolean hasNext() 

Source Link

Document

Indicates whether the Reader has more lines.

Usage

From source file:org.mskcc.cbio.importer.io.internal.FileUtilsImpl.java

/**
 * Helper function to create DataMatrix.
 *
 * @param data InputStream//  ww w  .  j ava 2  s  .co  m
 * @return DataMatrix
 */
private DataMatrix getDataMatrix(InputStream data) throws Exception {

    // iterate over all lines in byte[]
    List<String> columnNames = null;
    List<LinkedList<String>> rowData = null;
    LineIterator it = IOUtils.lineIterator(data, null);
    try {
        int count = -1;
        while (it.hasNext()) {
            // first row is our column heading, create column vector
            if (++count == 0) {
                columnNames = new LinkedList(Arrays.asList(it.nextLine().split(Converter.VALUE_DELIMITER, -1)));
            }
            // all other rows are rows in the table
            else {
                rowData = (rowData == null) ? new LinkedList<LinkedList<String>>() : rowData;
                rowData.add(new LinkedList(Arrays.asList(it.nextLine().split(Converter.VALUE_DELIMITER, -1))));
            }
        }
    } finally {
        LineIterator.closeQuietly(it);
    }

    // problem reading from data?
    if (columnNames == null || rowData == null) {
        if (LOG.isInfoEnabled()) {
            LOG.info(
                    "getDataMatrix(), problem creating DataMatrix from file, data file probably missing data, returning null");
        }
        return null;
    }

    // made it here, we can create DataMatrix
    if (LOG.isInfoEnabled()) {
        LOG.info("creating new DataMatrix(), from file data");
    }

    // outta here
    return new DataMatrix(rowData, columnNames);
}

From source file:org.mskcc.cbio.portal.util.IGVLinking.java

private static String getFileContents(File file) throws Exception {
    StringBuilder sb = new StringBuilder();
    LineIterator it = null;

    try {//from w w w.  j a v a 2  s. c  om
        it = FileUtils.lineIterator(file, "UTF-8");
        while (it.hasNext()) {
            sb.append(it.nextLine());
        }
    } finally {
        if (it != null)
            it.close();
    }

    return sb.toString();
}

From source file:org.nd4j.linalg.factory.Nd4j.java

/**
 * Read line via input streams//from  w ww .j  a v  a 2s  . c  om
 *
 * @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.neo4art.sentiment.service.DictionaryBasicService.java

/**
 * @see org.neo4art.sentiment.service.DictionaryService#saveDictionary()
 *///from   w w w .  j a  va  2  s .co m
@Override
public void saveDictionary() throws IOException {
    String alphabet[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
            "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

    for (int i = 0; i < alphabet.length; i++) {
        String dictionaryFile = "dictionary" + File.separator + this.locale.getLanguage() + File.separator
                + alphabet[i] + " Words.txt";

        logger.info("Importing dictionary file: " + dictionaryFile);

        LineIterator dictionaryFileIterator = IOUtils.lineIterator(
                getClass().getClassLoader().getResourceAsStream(dictionaryFile), Charset.defaultCharset());

        while (dictionaryFileIterator.hasNext()) {
            this.mergeWordWithoutFlushing(dictionaryFileIterator.next());
        }
    }

    Neo4ArtBatchInserterSingleton.flushLegacyNodeIndex(NLPLegacyIndex.WORD_LEGACY_INDEX);
}

From source file:org.neo4art.sentiment.service.DictionaryBasicService.java

/**
 * /*from  ww w  .  j  a v a  2 s  .  co m*/
 * @param file
 * @param polarity
 * @throws IOException
 */
private void addPolarity(String file, int polarity) throws IOException {
    logger.info("Adding polarity from file: " + file);

    LineIterator polarityFile = IOUtils.lineIterator(getClass().getClassLoader().getResourceAsStream(file),
            Charset.defaultCharset());

    while (polarityFile.hasNext()) {
        String word = polarityFile.nextLine();

        if (StringUtils.isNotEmpty(word) && !StringUtils.startsWith(word, ";")) {
            Long wordNodeId = this.mergeWordWithoutFlushing(DictionaryUtils.escapeWordForLuceneSearch(word));

            Neo4ArtBatchInserterSingleton.setNodeProperty(wordNodeId, Word.POLARITY_PROPERTY_NAME, polarity);
        }
    }

    Neo4ArtBatchInserterSingleton.flushLegacyNodeIndex(NLPLegacyIndex.WORD_LEGACY_INDEX);
}

From source file:org.nomt.agent.nmon.job.ParserJob.java

@Override
public void perform() {
    logger.info("ParserJob.perform()");
    String filename = NmonDataFiles.getNextFile();
    File nmonDataFile = new File(filename);
    logger.debug("Found nmon file {}, start to parser it.", filename);
    try {//from   w  ww .j a v  a 2  s . co m
        LineIterator lineIterator = FileUtils.lineIterator(nmonDataFile, "utf-8");
        Aaa aaa = new Aaa();
        CpuX86 cpuX86 = new CpuX86();
        CpuAll cpuAll = new CpuAll();
        Mem mem = new Mem();
        Net net = new Net();
        NetPacket netPacket = new NetPacket();
        DiskBusy diskBusy = new DiskBusy();
        DiskBsize diskBsize = new DiskBsize();
        DiskRead diskRead = new DiskRead();
        DiskWrite diskWrite = new DiskWrite();
        DiskXfer diskXfer = new DiskXfer();
        JfsInode jfsInode = new JfsInode();
        Proc proc = new Proc();

        String nextLine;
        String[] lineParts;
        String part0, part1;
        String[] netTitleLineParts = null;
        String[] netPacketTitleLineParts = null;
        String[] jfsLineParts = null;
        String[] diskBusyLineParts = null;
        String[] diskReadLineParts = null;
        String[] diskWriteLineParts = null;
        String[] diskBsizeLineParts = null;
        String[] diskXferLineParts = null;

        while (lineIterator.hasNext()) {
            nextLine = lineIterator.next();
            lineParts = nextLine.split(",");
            part0 = lineParts[0];
            part1 = lineParts[1];
            if ("AAA".equalsIgnoreCase(part0)) {
                if (lineParts.length == 3) {
                    NmonLineParser.parseAaa(aaa, lineParts);
                } else if (lineParts.length == 4) {
                    NmonLineParser.parseCpuX86(cpuX86, lineParts);
                }
            } else if ("T0001".equalsIgnoreCase(part1)) {
                if ("CPU_ALL".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseCpuAll(cpuAll, lineParts);
                } else if ("MEM".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseMem(mem, lineParts);
                } else if ("NET".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseNet(net, netTitleLineParts, lineParts);
                } else if ("NETPACKET".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseNetPacket(netPacket, netPacketTitleLineParts, lineParts);
                } else if ("JFSFILE".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseJfsInode(jfsInode, jfsLineParts, lineParts);
                } else if ("DISKBUSY".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseDiskBusy(diskBusy, diskBusyLineParts, lineParts);
                } else if ("DISKREAD".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseDiskRead(diskRead, diskReadLineParts, lineParts);
                } else if ("DISKWRITE".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseDiskWrite(diskWrite, diskWriteLineParts, lineParts);
                } else if ("DISKXFER".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseDiskXfer(diskXfer, diskXferLineParts, lineParts);
                } else if ("DISKBSIZE".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseDiskBsize(diskBsize, diskBsizeLineParts, lineParts);
                } else if ("PROC".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseProc(proc, lineParts);
                }
            } else if ("NET".equalsIgnoreCase(part0)) {
                netTitleLineParts = lineParts;
            } else if ("NETPACKET".equalsIgnoreCase(part0)) {
                netPacketTitleLineParts = lineParts;
            } else if ("JFSFILE".equalsIgnoreCase(part0)) {
                jfsLineParts = lineParts;
            } else if ("DISKBUSY".equalsIgnoreCase(part0)) {
                diskBusyLineParts = lineParts;
            } else if ("DISKREAD".equalsIgnoreCase(part0)) {
                diskReadLineParts = lineParts;
            } else if ("DISKWRITE".equalsIgnoreCase(part0)) {
                diskWriteLineParts = lineParts;
            } else if ("DISKXFER".equalsIgnoreCase(part0)) {
                diskXferLineParts = lineParts;
            } else if ("DISKBSIZE".equalsIgnoreCase(part0)) {
                diskBsizeLineParts = lineParts;
            }

        }

        NmonFileInfo nmonFileInfo = new NmonFileInfo();
        nmonFileInfo.setAaa(aaa);
        nmonFileInfo.setX86(cpuX86);
        nmonFileInfo.setCpuAll(cpuAll);
        nmonFileInfo.setNet(net);
        nmonFileInfo.setNetPacket(netPacket);
        nmonFileInfo.setMem(mem);
        nmonFileInfo.setJfsInode(jfsInode);
        nmonFileInfo.setDiskBusy(diskBusy);
        nmonFileInfo.setDiskRead(diskRead);
        nmonFileInfo.setDiskWrite(diskWrite);
        nmonFileInfo.setDiskXfer(diskXfer);
        nmonFileInfo.setBsize(diskBsize);
        NmonFileInfoRepo.setContent(nmonFileInfo);
        logger.debug("Parse Nmon File {} successfully.", filename);
    } catch (IOException e) {
        logger.info("IOException while parsering {}.", filename);
    }

    FileUtils.deleteQuietly(nmonDataFile);
    logger.debug("After parsering, delete file {}.", filename);
}

From source file:org.objectstyle.woproject.maven2.wobootstrap.utils.WebObjectsUtils.java

/**
 * Retrieves the version of installed WebObjects. It uses a
 * <code>WebObjectsLocator</code> to find the WebObjects version file.
 * /*from   www.ja  v  a 2s .c  o m*/
 * @param locator
 *            The WebObjects locator
 * @return Returns the WebObjects version or <code>null</code> if cannot
 *         discover the WebObjects version
 */
public static String getWebObjectsVersion(WebObjectsLocator locator) {
    if (locator == null) {
        return null;
    }

    File versionFile = locator.getWebObjectsVersionFile();

    if (versionFile == null || !versionFile.exists()) {
        return null;
    }

    String version = null;

    LineIterator iterator = null;

    try {
        iterator = FileUtils.lineIterator(versionFile, null);

        while (iterator.hasNext()) {
            String line = iterator.nextLine();

            if ("<key>CFBundleShortVersionString</key>".equals(line.trim())) {
                String versionLine = iterator.nextLine();

                version = versionLine.trim().replaceAll("</?string>", "");

                break;
            }
        }

    } catch (IOException exception) {
        // TODO: hprange, write an info to log instead
        exception.printStackTrace();
    } finally {
        if (iterator != null) {
            LineIterator.closeQuietly(iterator);
        }
    }

    return version;
}

From source file:org.occiware.clouddesigner.occi.docker.connector.dockermachine.util.DockerUtil.java

public static String asString(final InputStream response) {
    final StringWriter logwriter = new StringWriter();
    try {/*w w  w  .  j  a v  a  2  s  .com*/
        final LineIterator itr = IOUtils.lineIterator(response, "UTF-8");
        while (itr.hasNext()) {
            {
                String line = itr.next();
                boolean _hasNext = itr.hasNext();
                if (_hasNext) {
                    logwriter.write((line + "\n"));
                } else {
                    logwriter.write((line + ""));
                }
            }
        }
        response.close();
        return logwriter.toString();
    } catch (final Throwable _t) {
        if (_t instanceof IOException) {
            final IOException e = (IOException) _t;
            throw new RuntimeException(e);
        } else {
            throw Exceptions.sneakyThrow(_t);
        }
    } finally {
        IOUtils.closeQuietly(response);
    }
}

From source file:org.occiware.clouddesigner.occi.hypervisor.connector.libvirt.util.DomainMarshaller.java

public String asString(final String uuid) {
    try {/*from  w w w . jav  a 2  s.co  m*/
        final String filename = (("/" + uuid) + ".xml");
        final StringWriter logwriter = new StringWriter();
        final File tmpDir = this.createTempDir(this.xmlDirectory);
        String _plus = (tmpDir + "/");
        String _plus_1 = (_plus + filename);
        InputStream response = new FileInputStream(_plus_1);
        try {
            final LineIterator itr = IOUtils.lineIterator(response, "UTF-8");
            while (itr.hasNext()) {
                {
                    String line = itr.next();
                    boolean _hasNext = itr.hasNext();
                    if (_hasNext) {
                        logwriter.write((line + "\n"));
                    } else {
                        logwriter.write((line + ""));
                    }
                }
            }
            response.close();
            return logwriter.toString();
        } catch (final Throwable _t) {
            if (_t instanceof IOException) {
                final IOException e = (IOException) _t;
                throw new RuntimeException(e);
            } else {
                throw Exceptions.sneakyThrow(_t);
            }
        } finally {
            IOUtils.closeQuietly(response);
        }
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}

From source file:org.onesec.raven.ivr.impl.ExternalTextToSpeechEngineNodeTest.java

private void printStream(InputStream stream) throws IOException {
    LineIterator it = IOUtils.lineIterator(stream, "utf-8");
    while (it.hasNext())
        System.out.println("LINE: " + it.nextLine());
}