Example usage for org.apache.commons.io IOUtils readLines

List of usage examples for org.apache.commons.io IOUtils readLines

Introduction

In this page you can find the example usage for org.apache.commons.io IOUtils readLines.

Prototype

public static List readLines(Reader input) throws IOException 

Source Link

Document

Get the contents of a Reader as a list of Strings, one entry per line.

Usage

From source file:de.cismet.cids.custom.sudplan.wupp.geocpm.ie.ImportExportTest.java

private void testDyna(final int interval, final List<Double> rainValues, final String referenceFile)
        throws Exception {
    this.exporter = new GeoCPMExport(1, this.testOutFile, DB_USER, DB_PWD, CON.getMetaData().getURL());
    this.exporter.generateDYNA(interval, rainValues);

    final String parent = this.testOutFile.getParent();
    final File exported = new File(parent, GeoCPMExport.DYNA_FILE);

    exported.deleteOnExit();//w w w  . j av  a 2  s .  co  m

    final List<String> exportedData = IOUtils
            .readLines(ImportExportTest.class.getResourceAsStream(referenceFile));
    final List<String> referenceData = IOUtils.readLines(new FileInputStream(exported));

    assertEquals("Number of DYNA records is different", exportedData.size(), referenceData.size());

    final int size = referenceData.size();
    for (int i = 0; i < size; i++) {
        // Sometimes, there is a field delimiter at the end of a record in in the INPUT file.
        // For now, we ignore it in our tests
        assertEquals(exportedData.get(i).trim(), referenceData.get(i).trim());
    }

    exported.delete();
}

From source file:com.izforge.izpack.compiler.packager.impl.PackagerBase.java

/**
 * Write manifest in the install jar./* ww  w .ja v  a2s. co m*/
 *
 * @throws IOException for any I/O error
 */
protected void writeManifest() throws IOException {
    // Add splash screen configuration
    List<String> lines = IOUtils.readLines(getClass().getResourceAsStream("MANIFEST.MF"));
    if (splashScreenImage != null) {
        String destination = String.format("META-INF/%s", splashScreenImage.getName());
        mergeManager.addResourceToMerge(splashScreenImage.getAbsolutePath(), destination);
        lines.add(String.format("SplashScreen-Image: %s", destination));
    }
    lines.add("");
    File tempManifest = com.izforge.izpack.util.file.FileUtils.createTempFile("MANIFEST", ".MF");
    FileUtils.writeLines(tempManifest, lines);
    mergeManager.addResourceToMerge(tempManifest.getAbsolutePath(), "META-INF/MANIFEST.MF");
}

From source file:net.pms.dlna.RootFolder.java

/**
 * Creates, populates and returns a virtual folder mirroring the
 * contents of the system's iPhoto folder.
 * Mac OS X only.//from  ww w  . j  a  v  a 2s.  c  o  m
 *
 * @return iPhotoVirtualFolder the populated <code>VirtualFolder</code>, or null if one couldn't be created.
 */
private DLNAResource getiPhotoFolder() {
    VirtualFolder iPhotoVirtualFolder = null;

    if (Platform.isMac()) {
        logger.debug("Adding iPhoto folder");
        InputStream inputStream = null;

        try {
            // This command will show the XML files for recently opened iPhoto databases
            Process process = Runtime.getRuntime().exec("defaults read com.apple.iApps iPhotoRecentDatabases");
            inputStream = process.getInputStream();
            List<String> lines = IOUtils.readLines(inputStream);
            logger.debug("iPhotoRecentDatabases: {}", lines);

            if (lines.size() >= 2) {
                // we want the 2nd line
                String line = lines.get(1);

                // Remove extra spaces
                line = line.trim();

                // Remove quotes
                line = line.substring(1, line.length() - 1);

                URI uri = new URI(line);
                URL url = uri.toURL();
                File file = FileUtils.toFile(url);
                logger.debug("Resolved URL to file: {} -> {}", url, file.getAbsolutePath());

                // Load the properties XML file.
                Map<String, Object> iPhotoLib = Plist.load(file);

                // The list of all photos
                Map<?, ?> photoList = (Map<?, ?>) iPhotoLib.get("Master Image List");

                // The list of events (rolls)
                List<Map<?, ?>> listOfRolls = (List<Map<?, ?>>) iPhotoLib.get("List of Rolls");

                iPhotoVirtualFolder = new VirtualFolder("iPhoto Library", null);

                for (Map<?, ?> roll : listOfRolls) {
                    Object rollName = roll.get("RollName");

                    if (rollName != null) {
                        VirtualFolder virtualFolder = new VirtualFolder(rollName.toString(), null);

                        // List of photos in an event (roll)
                        List<?> rollPhotos = (List<?>) roll.get("KeyList");

                        for (Object photo : rollPhotos) {
                            Map<?, ?> photoProperties = (Map<?, ?>) photoList.get(photo);

                            if (photoProperties != null) {
                                Object imagePath = photoProperties.get("ImagePath");

                                if (imagePath != null) {
                                    RealFile realFile = new RealFile(new File(imagePath.toString()));
                                    virtualFolder.addChild(realFile);
                                }
                            }
                        }

                        iPhotoVirtualFolder.addChild(virtualFolder);
                    }
                }
            } else {
                logger.info("iPhoto folder not found");
            }
        } catch (XmlParseException e) {
            logger.error("Something went wrong with the iPhoto Library scan: ", e);
        } catch (URISyntaxException e) {
            logger.error("Something went wrong with the iPhoto Library scan: ", e);
        } catch (IOException e) {
            logger.error("Something went wrong with the iPhoto Library scan: ", e);
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
    }

    return iPhotoVirtualFolder;
}

From source file:com.unister.semweb.drums.bucket.hashfunction.RangeHashFunction.java

@Override
public void load(InputStream in) throws IOException {
    List<String> readData = IOUtils.readLines(in);

    maxRangeValues = new byte[readData.size() - 1][];
    filenames = new String[readData.size() - 1];

    // analyze header
    String[] header = readData.get(0).split("\t");
    int keySize = 0;
    keyComposition = new int[header.length - 1];
    for (int i = 0; i < keyComposition.length; i++) {
        int e = stringToByteCount(header[i]);
        if (e == 0) {
            throw new IOException("Header could not be read. Could not decode " + header[i]);
        }/*  ww w  .  j  a  v a 2 s.c  om*/
        keyComposition[i] = e;
        keySize += e;
    }
    for (int i = 0; i < readData.size() - 1; i++) {
        String[] Aline = readData.get(i + 1).split("\t");
        // TODO: format exception
        maxRangeValues[i] = new byte[keySize];
        // we need an offset for the current part of the key
        int keyPartOffset = -1;
        for (int k = 0; k < keyComposition.length; k++) {
            long tmp = Long.parseLong(Aline[k]);
            // set the offset on the last byte of the current part of the key
            keyPartOffset += keyComposition[k];
            // start from the lowest bits of the read long value and use them for the last byte (= lowest byte) of
            // the current part of the key. Than take the next bits and the second lowest byte
            for (int b = 0; b < keyComposition[k]; b++) {
                maxRangeValues[i][keyPartOffset - b] = (byte) tmp;
                tmp = tmp >> 8;
            }
        }
        filenames[i] = Aline[keyComposition.length];
    }
    this.sort();
    generateBucketIds();

}

From source file:gov.nih.nci.nbia.StandaloneDMV3.java

private List<String> connectAndReadFromURL(URL url, List<String> seriesList, String userId, String passWd) {
    List<String> data = null;
    DefaultHttpClient httpClient = null;
    TrustStrategy easyStrategy = new TrustStrategy() {
        @Override/*from ww w . j  av a  2s . c om*/
        public boolean isTrusted(X509Certificate[] certificate, String authType) throws CertificateException {
            return true;
        }
    };
    try {
        // SSLContext sslContext = SSLContext.getInstance("SSL");
        // set up a TrustManager that trusts everything
        // sslContext.init(null, new TrustManager[] { new
        // EasyX509TrustManager(null)}, null);

        SSLSocketFactory sslsf = new SSLSocketFactory(easyStrategy,
                SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        Scheme httpsScheme = new Scheme("https", 443, sslsf);
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(httpsScheme);
        schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
        ClientConnectionManager ccm = new ThreadSafeClientConnManager(schemeRegistry);

        HttpParams httpParams = new BasicHttpParams();
        // HttpConnectionParams.setConnectionTimeout(httpParams, 50000);
        // HttpConnectionParams.setSoTimeout(httpParams, new
        // Integer(12000));
        HttpConnectionParams.setConnectionTimeout(httpParams, 500000);
        HttpConnectionParams.setSoTimeout(httpParams, new Integer(120000));
        httpClient = new DefaultHttpClient(ccm, httpParams);
        httpClient.setRoutePlanner(new ProxySelectorRoutePlanner(schemeRegistry, ProxySelector.getDefault()));
        // // Additions by lrt for tcia -
        // // attempt to reduce errors going through a Coyote Point
        // Equalizer load balance switch
        // httpClient.getParams().setParameter("http.socket.timeout", new
        // Integer(12000));
        httpClient.getParams().setParameter("http.socket.timeout", new Integer(120000));
        httpClient.getParams().setParameter("http.socket.receivebuffer", new Integer(16384));
        httpClient.getParams().setParameter("http.tcp.nodelay", true);
        httpClient.getParams().setParameter("http.connection.stalecheck", false);
        // // end lrt additions

        HttpPost httpPostMethod = new HttpPost(url.toString());

        List<BasicNameValuePair> postParams = new ArrayList<BasicNameValuePair>();

        if (userId != null && passWd != null) {
            postParams.add(new BasicNameValuePair("userId", userId));
            httpPostMethod.addHeader("password", passWd);
        }
        postParams.add(new BasicNameValuePair("numberOfSeries", Integer.toString(seriesList.size())));
        int i = 0;
        for (String s : seriesList) {
            postParams.add(new BasicNameValuePair("series" + Integer.toString(++i), s));
        }

        UrlEncodedFormEntity query = new UrlEncodedFormEntity(postParams);
        httpPostMethod.setEntity(query);
        HttpResponse response = httpClient.execute(httpPostMethod);
        int responseCode = response.getStatusLine().getStatusCode();

        if (responseCode != HttpURLConnection.HTTP_OK) {
            returnStatus = responseCode;
            return null;
        } else {
            InputStream inputStream = response.getEntity().getContent();
            data = IOUtils.readLines(inputStream);
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (KeyStoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnrecoverableKeyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if (httpClient != null) {
            httpClient.getConnectionManager().shutdown();
        }
    }

    return data;
}

From source file:com.grepcurl.random.BaseGenerator.java

private void _loadDictionary() {
    _dictionary = new HashSet<>();
    try {//from   w ww  . ja va  2 s . com
        List<String> words = IOUtils.readLines(
                Thread.currentThread().getContextClassLoader().getResourceAsStream(getDictionaryFileName()));
        words.forEach(_dictionary::add);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.hipu.bdb.util.FileUtils.java

/**
 * Retrieve a number of lines from the file around the given 
 * position, as when paging forward or backward through a file. 
 * /*from w w  w.j ava  2s . c  om*/
 * @param file File to retrieve lines
 * @param position offset to anchor lines
 * @param signedDesiredLineCount lines requested; if negative, 
 *        want this number of lines ending with a line containing
 *        the position; if positive, want this number of lines,
 *        all starting at or after position. 
 * @param lines List<String> to insert found lines
 * @param lineEstimate int estimate of line size, 0 means use default
 *        of 128
 * @return LongRange indicating the file offsets corresponding to 
 *         the beginning of the first line returned, and the point
 *         after the end of the last line returned
 * @throws IOException
 */
@SuppressWarnings("unchecked")
public static LongRange pagedLines(File file, long position, int signedDesiredLineCount, List<String> lines,
        int lineEstimate) throws IOException {
    // consider negative positions as from end of file; -1 = last byte
    if (position < 0) {
        position = file.length() + position;
    }

    // calculate a reasonably sized chunk likely to have all desired lines
    if (lineEstimate == 0) {
        lineEstimate = 128;
    }
    int desiredLineCount = Math.abs(signedDesiredLineCount);
    long startPosition;
    long fileEnd = file.length();
    int bufferSize = (desiredLineCount + 5) * lineEstimate;
    if (signedDesiredLineCount > 0) {
        // reading forward; include previous char in case line-end
        startPosition = position - 1;
    } else {
        // reading backward
        startPosition = position - bufferSize + (2 * lineEstimate);
    }
    if (startPosition < 0) {
        startPosition = 0;
    }
    if (startPosition + bufferSize > fileEnd) {
        bufferSize = (int) (fileEnd - startPosition);
    }

    // read that reasonable chunk
    FileInputStream fis = new FileInputStream(file);
    fis.getChannel().position(startPosition);
    byte[] buf = new byte[bufferSize];
    IOUtils.closeQuietly(fis);

    // find all line starts fully in buffer
    // (positions after a line-end, per line-end definition in 
    // BufferedReader.readLine)
    LinkedList<Integer> lineStarts = new LinkedList<Integer>();
    if (startPosition == 0) {
        lineStarts.add(0);
    }
    boolean atLineEnd = false;
    boolean eatLF = false;
    int i;
    for (i = 0; i < bufferSize; i++) {
        if ((char) buf[i] == '\n' && eatLF) {
            eatLF = false;
            continue;
        }
        if (atLineEnd) {
            atLineEnd = false;
            lineStarts.add(i);
            if (signedDesiredLineCount < 0 && startPosition + i > position) {
                // reached next line past position, read no more
                break;
            }
        }
        if ((char) buf[i] == '\r') {
            atLineEnd = true;
            eatLF = true;
            continue;
        }
        if ((char) buf[i] == '\n') {
            atLineEnd = true;
        }
    }
    if (startPosition + i == fileEnd) {
        // add phantom lineStart after end
        lineStarts.add(bufferSize);
    }
    int foundFullLines = lineStarts.size() - 1;

    // if found no lines
    if (foundFullLines < 1) {
        if (signedDesiredLineCount > 0) {
            if (startPosition + bufferSize == fileEnd) {
                // nothing more to read: return nothing
                return new LongRange(fileEnd, fileEnd);
            } else {
                // retry with larger lineEstimate
                return pagedLines(file, position, signedDesiredLineCount, lines,
                        Math.max(bufferSize, lineEstimate));
            }

        } else {
            // try again with much larger line estimate
            // TODO: fail gracefully before growing to multi-MB buffers
            return pagedLines(file, position, signedDesiredLineCount, lines, bufferSize);
        }
    }

    // trim unneeded lines
    while (signedDesiredLineCount > 0 && startPosition + lineStarts.getFirst() < position) {
        // discard lines starting before desired position
        lineStarts.removeFirst();
    }
    while (lineStarts.size() > desiredLineCount + 1) {
        if (signedDesiredLineCount < 0 && (startPosition + lineStarts.get(1) <= position)) {
            // discard from front until reach line containing target position
            lineStarts.removeFirst();
        } else {
            lineStarts.removeLast();
        }
    }
    int firstLine = lineStarts.getFirst();
    int partialLine = lineStarts.getLast();
    LongRange range = new LongRange(startPosition + firstLine, startPosition + partialLine);
    List<String> foundLines = IOUtils
            .readLines(new ByteArrayInputStream(buf, firstLine, partialLine - firstLine));

    if (foundFullLines < desiredLineCount && signedDesiredLineCount < 0 && startPosition > 0) {
        // if needed and reading backward, read more lines from earlier
        range = expandRange(range, pagedLines(file, range.getMinimumLong() - 1,
                signedDesiredLineCount + foundFullLines, lines, bufferSize / foundFullLines));

    }

    lines.addAll(foundLines);

    if (signedDesiredLineCount < 0 && range.getMaximumLong() < position) {
        // did not get line containining start position
        range = expandRange(range, pagedLines(file, partialLine, 1, lines, bufferSize / foundFullLines));
    }

    if (signedDesiredLineCount > 0 && foundFullLines < desiredLineCount && range.getMaximumLong() < fileEnd) {
        // need more forward lines
        range = expandRange(range, pagedLines(file, range.getMaximumLong(), desiredLineCount - foundFullLines,
                lines, bufferSize / foundFullLines));
    }

    return range;
}

From source file:edu.ucsd.crbs.cws.cluster.submission.TestJobCmdScriptCreatorImpl.java

@Test
public void testCreateAndRunScriptWithFakeKeplerThatFailsAndNoEmailsSet() throws Exception {
    assumeTrue(SystemUtils.IS_OS_UNIX);/* www.j  a v a 2  s  . c  om*/
    File baseDirectory = Folder.newFolder();
    File tempDirectory = new File(baseDirectory + File.separator + "subdir");
    File outputsDir = new File(tempDirectory + File.separator + Constants.OUTPUTS_DIR_NAME);
    assertTrue(outputsDir.mkdirs());

    JobEmailNotificationData emailNotifyData = createJobEmailNotificationData();

    JobBinaries jb = new JobBinaries();
    jb.setKeplerScript(getAndCheckForFalseBinaryFile().getAbsolutePath());
    jb.setRegisterUpdateJar("register.jar");
    jb.setJavaCommand("/bin/echo");
    jb.setRetryCount(1);

    JobCmdScriptCreatorImpl scriptCreator = new JobCmdScriptCreatorImpl("/workflowsdir", jb, emailNotifyData);

    File checkForTrue = getAndCheckForTrueBinaryFile();

    scriptCreator.setJavaBinaryPath(checkForTrue.getAbsolutePath());

    Job j = new Job();
    Workflow w = new Workflow();
    w.setId(new Long(5));
    j.setWorkflow(w);

    String jobCmd = scriptCreator.create(tempDirectory.getAbsolutePath(), j, new Long(10));

    assertTrue(jobCmd != null);
    assertTrue(
            jobCmd.equals(outputsDir.getAbsolutePath() + File.separator + JobCmdScriptCreatorImpl.JOB_CMD_SH));
    File checkCmdFile = new File(jobCmd);
    assertTrue(checkCmdFile.canExecute());

    RunCommandLineProcessImpl rclpi = new RunCommandLineProcessImpl();
    rclpi.setWorkingDirectory(tempDirectory.getAbsolutePath());

    try {
        String result = rclpi.runCommandLineProcess(jobCmd);
    } catch (Exception ex) {
        assertTrue(ex.getMessage().startsWith("Non zero exit code (1)"));
    }

    String logFile = baseDirectory.getAbsoluteFile() + File.separator + "job...log";
    File checkLogFile = new File(logFile);
    assertTrue(logFile + " and we ran " + jobCmd, checkLogFile.exists());
    List<String> lines = IOUtils.readLines(new FileReader(logFile));
    for (String line : lines) {
        if (line.startsWith("exitcode: ")) {
            assertTrue(line, line.equals("exitcode: 1"));
        }
    }

}

From source file:edworld.pdfreader4humans.PDFReader.java

protected String indent(String output, int indentLevel) {
    try {//ww w  .j  a v  a 2 s  . co  m
        char[] tabs = new char[indentLevel];
        Arrays.fill(tabs, '\t');
        StringBuilder buffer = new StringBuilder();
        for (String line : IOUtils.readLines(new StringReader(output)))
            if (line.equals("${content}"))
                buffer.append(line);
            else
                buffer.append(tabs).append(line).append(LINE_BREAK);
        return buffer.toString();
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:com.norconex.importer.handler.splitter.impl.TranslatorSplitter.java

private ImporterMetadata translateFields(SplittableDocument doc, Translator translator, String sourceLang,
        String targetLang) throws Exception {
    ImporterMetadata childMeta = new ImporterMetadata();
    if (ignoreNonTranslatedFields) {
        if (fieldsToTranslate == null) {
            return childMeta;
        }/*from w w w  . j ava  2 s .c om*/
        for (String key : fieldsToTranslate) {
            List<String> values = doc.getMetadata().get(key);
            if (values != null) {
                childMeta.put(key, values);
            }
        }
    } else {
        childMeta.load(doc.getMetadata());
        if (fieldsToTranslate == null) {
            return childMeta;
        }
    }

    StringBuilder b = new StringBuilder();
    for (int i = 0; i < fieldsToTranslate.length; i++) {
        List<String> values = doc.getMetadata().get(fieldsToTranslate[i]);
        for (String value : values) {
            b.append("[" + value.replaceAll("[\n\\[\\]]", " ") + "]");
        }
        b.append("\n");
    }
    if (b.length() == 0) {
        return childMeta;
    }

    String txt = translator.translate(b.toString(), sourceLang, targetLang);
    List<String> lines = IOUtils.readLines(new StringReader(txt));
    int index = 0;
    for (String line : lines) {
        line = StringUtils.removeStart(line, "[");
        line = StringUtils.removeEnd(line, "]");
        String[] values = StringUtils.splitByWholeSeparator(line, "][");
        childMeta.setString(fieldsToTranslate[index], values);
        index++;
    }
    return childMeta;
}