Example usage for java.io LineNumberReader readLine

List of usage examples for java.io LineNumberReader readLine

Introduction

In this page you can find the example usage for java.io LineNumberReader readLine.

Prototype

public String readLine() throws IOException 

Source Link

Document

Read a line of text.

Usage

From source file:com.anyi.gp.license.RegisterTools.java

public static List getMacAddresses() {
    List result = new ArrayList();
    try {/*www  .  ja va  2  s  . c o  m*/
        Properties props = System.getProperties();
        String command = "ipconfig -a";
        if (props.getProperty("os.name").toLowerCase().startsWith("windows"))
            command = "ipconfig /all";

        Process process = Runtime.getRuntime().exec(command);
        InputStreamReader ir = new InputStreamReader(process.getInputStream());
        LineNumberReader input = new LineNumberReader(ir);
        String line;
        while ((line = input.readLine()) != null)
            if (line.indexOf("Physical Address") > 0) {
                String MACAddr = line.substring(line.indexOf("-") - 2);
                // System.out.println("MAC address = [" + MACAddr + "]");
                result.add(MACAddr);
            }
    } catch (java.io.IOException e) {
        e.printStackTrace();
        // System.err.println("IOException " + e.getMessage());
    }
    return result;
}

From source file:org.kse.utilities.pem.PemUtil.java

/**
 * Decode the PEM included in the supplied input stream.
 *
 * @param is//from  w  ww.  ja v  a2s  .  c om
 *            Input stream
 * @return PEM information or null if stream does not contain PEM
 * @throws IOException
 *             If an I/O problem occurs
 */
public static PemInfo decode(InputStream is) throws IOException {
    byte[] streamContents = ReadUtil.readFully(is);

    LineNumberReader lnr = null;

    try {
        lnr = new LineNumberReader(new InputStreamReader(new ByteArrayInputStream(streamContents)));

        String line = lnr.readLine();
        StringBuffer sbBase64 = new StringBuffer();

        if (line != null) {
            line = line.trim();
            String headerType = getTypeFromHeader(line);

            if (headerType != null) {
                line = lnr.readLine();

                PemAttributes attributes = null;

                // Read any header attributes
                if (line != null && line.contains(": ")) {
                    line = line.trim();

                    attributes = new PemAttributes();

                    while (line != null) {
                        line = line.trim();

                        // Empty line - end of attributes
                        if (line.equals("")) {
                            line = lnr.readLine();
                            break;
                        }

                        // Run out of attributes before blank line - not PEM
                        if (!line.contains(": ")) {
                            return null;
                        }

                        // Parse attribute from line
                        int separator = line.indexOf(':');

                        String attributeName = line.substring(0, separator);
                        String attributeValue = line.substring(separator + 2);

                        attributes.add(new PemAttribute(attributeName, attributeValue));

                        line = lnr.readLine();
                    }
                }

                // Read content
                while (line != null) {
                    line = line.trim();
                    String footerType = getTypeFromFooter(line);

                    if (footerType == null) {
                        sbBase64.append(line);
                    } else {
                        // Header and footer types do not match - not PEM
                        if (!headerType.equals(footerType)) {
                            return null;
                        } else {
                            // Decode base 64 content
                            byte[] content = Base64.decode(sbBase64.toString());

                            return new PemInfo(headerType, attributes, content);
                        }
                    }

                    line = lnr.readLine();
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(lnr);
    }

    return null; // Not PEM
}

From source file:net.sf.keystore_explorer.utilities.pem.PemUtil.java

/**
 * Decode the PEM included in the supplied input stream.
 *
 * @param is/*from   ww w .  j av a 2s .  c  o  m*/
 *            Input stream
 * @return PEM information or null if stream does not contain PEM
 * @throws IOException
 *             If an I/O problem occurs
 */
public static PemInfo decode(InputStream is) throws IOException {
    byte[] streamContents = ReadUtil.readFully(is);

    LineNumberReader lnr = null;

    try {
        lnr = new LineNumberReader(new InputStreamReader(new ByteArrayInputStream(streamContents)));

        String line = lnr.readLine();
        StringBuffer sbBase64 = new StringBuffer();

        if (line != null) {
            line = line.trim();
            String headerType = getTypeFromHeader(line);

            if (headerType != null) {
                line = lnr.readLine();

                PemAttributes attributes = null;

                // Read any header attributes
                if (line != null && line.contains(": ")) {
                    line = line.trim();

                    attributes = new PemAttributes();

                    attributesDone: while (line != null) {
                        line = line.trim();

                        // Empty line - end of attributes
                        if (line.equals("")) {
                            line = lnr.readLine();
                            break;
                        }

                        // Run out of attributes before blank line - not PEM
                        if (!line.contains(": ")) {
                            return null;
                        }

                        // Parse attribute from line
                        int separator = line.indexOf(':');

                        String attributeName = line.substring(0, separator);
                        String attributeValue = line.substring(separator + 2);

                        attributes.add(new PemAttribute(attributeName, attributeValue));

                        line = lnr.readLine();
                    }
                }

                // Read content
                while (line != null) {
                    line = line.trim();
                    String footerType = getTypeFromFooter(line);

                    if (footerType == null) {
                        sbBase64.append(line);
                    } else {
                        // Header and footer types do not match - not PEM
                        if (!headerType.equals(footerType)) {
                            return null;
                        } else {
                            // Decode base 64 content
                            byte[] content = Base64.decode(sbBase64.toString());

                            return new PemInfo(headerType, attributes, content);
                        }
                    }

                    line = lnr.readLine();
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(lnr);
    }

    return null; // Not PEM
}

From source file:mitm.common.fetchmail.FetchmailConfigBuilder.java

/**
 * Writes the new config to targetConfig. Lines between START_TOKEN and END_TOKEN are replaced with the 
 * new config from FetchmailConfig.//from   w w  w .j  av a  2 s  .c  om
 */
public static void createConfig(FetchmailConfig config, InputStream sourceConfig, OutputStream targetConfig)
        throws IOException {
    LineNumberReader reader = new LineNumberReader(new InputStreamReader(sourceConfig, "US-ASCII"));

    StrBuilder outputBuilder = new StrBuilder();

    boolean inBlock = false;
    boolean blockInjected = false;

    String line;

    do {
        line = reader.readLine();

        if (line != null) {
            if (inBlock) {
                if (line.startsWith(END_TOKEN)) {
                    inBlock = false;
                    blockInjected = true;

                    injectConfig(config, outputBuilder);

                    outputBuilder.appendln(line);
                }
            } else {
                if (!blockInjected && line.startsWith(START_TOKEN)) {
                    inBlock = true;
                }

                outputBuilder.appendln(line);
            }
        }
    } while (line != null);

    targetConfig.write(MiscStringUtils.toAsciiBytes(outputBuilder.toString()));
}

From source file:edu.stanford.muse.index.DatedDocument.java

public static StringBuilder formatStringForMaxCharsPerLine(String contents, int CHARS_PER_LINE) {
    StringBuilder sb = new StringBuilder();

    // should be in one time setup
    final String punctuationChars = "., \t!)]";
    final Set<Character> punctuationCharSet = new LinkedHashSet<Character>();
    for (int i = 0; i < punctuationChars.length(); i++)
        punctuationCharSet.add(punctuationChars.charAt(i));

    LineNumberReader lnr = new LineNumberReader(new StringReader(contents));
    while (true) {
        String line = null;/*from w w w .j a  v a2 s  . c  o m*/
        try {
            line = lnr.readLine();
        } catch (IOException ioe) {
            sb.append("Unexpected error while formatting line");
            log.warn("Unexpected io exception while formatting max chars per line: " + Util.stackTrace(ioe));
        }
        if (line == null)
            break;

        if (line.length() < CHARS_PER_LINE) {
            // common case, short line, nothing more to be done.
            sb.append(line);
            sb.append("\n");
            //   log.info ("short line: \"" + line + "\"");
            continue;
        }

        // line is longer than CHARS_PER_LINE. try to break it up.
        String remainingLine = line;
        while (true) {
            if (remainingLine.length() <= CHARS_PER_LINE) {
                sb.append(remainingLine);
                break;
            }

            // line is too long, try to break it up, look backwards from CHARS_PER_LINE to see if there's some punctuation
            int i = CHARS_PER_LINE - 1;
            for (; i >= 0; i--) {
                if (punctuationCharSet.contains(remainingLine.charAt(i)))
                    break;
            }

            // i is the index of the punct. char, or -1 if no punct.
            // emit the line, including the punct.
            if (i == -1) {
                // no break found, emit chars as is
                sb.append(remainingLine.substring(0, CHARS_PER_LINE));
                //      log.info ("no break: \"" + remainingLine.substring(0, CHARS_PER_LINE) + "\"");

                remainingLine = remainingLine.substring(CHARS_PER_LINE);
            } else {
                // break found
                sb.append(remainingLine.substring(0, i + 1));
                sb.append("\n");
                //   log.info ("found line: \"" + remainingLine.substring(0, i+1) + "\"");
                if (remainingLine.length() <= (i + 1))
                    break; // nothing remains

                remainingLine = remainingLine.substring(i + 1);
            }
        }
        sb.append("\n");
        //   log.info ("appended newline");
    }
    return sb;
}

From source file:eu.fbk.utils.lsa.util.Anvur.java

static List<String[]> readText(File f) throws IOException {
    logger.info("reading text: " + f + "...");
    List<String[]> list = new ArrayList<String[]>();
    //LineNumberReader lnr = new LineNumberReader(new FileReader(f));
    LineNumberReader lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));

    String line = null;//w  w w.j  a  v  a2s  .  c  o m
    int count = 1;
    while ((line = lnr.readLine()) != null) {
        String[] s = StringEscapeUtils.unescapeHtml(line).split("\t");
        list.add(s);
    } // end while

    logger.info(list.size() + " lines read from in " + f);
    lnr.close();
    return list;
}

From source file:eu.fbk.utils.lsa.util.AnvurDev.java

static List<String[]> readText(File f) throws IOException {
    logger.debug("reading text: " + f + "...");
    List<String[]> list = new ArrayList<String[]>();
    //LineNumberReader lnr = new LineNumberReader(new FileReader(f));
    LineNumberReader lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));

    String line = null;/*from ww  w . j a  va 2s  .com*/
    int count = 1;
    while ((line = lnr.readLine()) != null) {
        String[] s = StringEscapeUtils.unescapeHtml(line).split("\t");
        list.add(s);
    } // end while

    logger.debug(list.size() + " lines read from in " + f);
    lnr.close();
    return list;
}

From source file:nl.nn.adapterframework.webcontrol.FileViewerServlet.java

public static void showReaderContents(Reader reader, String filename, String type, HttpServletResponse response,
        String title) throws DomBuilderException, TransformerException, IOException {
    PrintWriter out = response.getWriter();
    if (type == null) {
        response.setContentType("text/html");
        out.println("resultType not specified");
        return;/*from   ww  w  . j  a  v  a 2 s. c om*/
    }

    if (type.equalsIgnoreCase("html")) {
        response.setContentType("text/html");

        out.println("<html>");
        out.println("<head>");
        out.println("<title>" + AppConstants.getInstance().getResolvedProperty("instance.name.lc") + "@"
                + Misc.getHostname() + " - " + title + "</title>");
        out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\""
                + AppConstants.getInstance().getProperty(fvConfigKey + ".css") + "\">");
        out.println("</head>");
        out.println("<body>");

        LineNumberReader lnr = new LineNumberReader(reader);
        String line;
        while ((line = lnr.readLine()) != null) {
            out.println(makeConfiguredReplacements(XmlUtils.encodeChars(line)) + "<br/>");
        }

        out.println("</body>");
        out.println("</html>");
    }
    if (type.equalsIgnoreCase("text")) {
        response.setContentType("text/plain");
        String lastPart;
        try {
            File f = new File(filename);
            lastPart = f.getName();
        } catch (Throwable t) {
            lastPart = filename;
        }
        response.setHeader("Content-Disposition", "attachment; filename=\"" + lastPart + "\"");
        Misc.readerToWriter(reader, out);
    }
    if (type.equalsIgnoreCase("xml")) {
        response.setContentType("application/xml");
        String lastPart;
        try {
            File f = new File(filename);
            lastPart = f.getName();
        } catch (Throwable t) {
            lastPart = filename;
        }
        response.setHeader("Content-Disposition", "inline; filename=\"" + lastPart + "\"");
        LineNumberReader lnr;
        if (filename.indexOf("_xml.log") >= 0) {
            Reader fileReader = new EncapsulatingReader(reader, log4j_prefix, log4j_postfix, true);
            lnr = new LineNumberReader(fileReader);
        } else {
            if (filename.indexOf("-stats_") >= 0) {
                Reader fileReader = new EncapsulatingReader(reader, stats_prefix, stats_postfix, true);
                lnr = new LineNumberReader(fileReader);
            } else {
                lnr = new LineNumberReader(reader);
            }
        }
        String line;
        while ((line = lnr.readLine()) != null) {
            out.println(line + "\n");
        }
    }
    out.close();
}

From source file:gpl.pierrick.brihaye.aramorph.InMemoryDictionaryHandler.java

/** Loads a compatibility table into a <CODE>Set</CODE>.
 * @param set The set//from www .  j a  va  2s  .c  om
 * @param name A human-readable name
 * @param is The stream
 * @throws RuntimeException If a problem occurs when reading the compatibility table
 */
private static void loadCompatibilityTable(Set set, String name, InputStream is) throws RuntimeException {
    System.out.print("Loading compatibility table : " + name + " ");
    try {
        LineNumberReader IN = new LineNumberReader(new InputStreamReader(is, "ISO8859_1"));
        String line = null;
        while ((line = IN.readLine()) != null) {
            if ((IN.getLineNumber() % 1000) == 1)
                System.out.print(".");
            if (!line.startsWith(";")) { //Ignore comments
                line = line.trim();
                line = line.replaceAll("\\s+", " ");
                set.add(line);
            }
        }
        IN.close();
        System.out.println();
        System.out.println(set.size() + " entries");
    } catch (IOException e) {
        throw new RuntimeException("Can not open : " + name);
    }
}

From source file:net.sf.keystore_explorer.crypto.signing.MidletSigner.java

/**
 * Read the attributes of the supplied JAD file as properties.
 *
 * @param jadFile//from  w w  w  .  j av  a  2 s. c o  m
 *            JAD file
 * @return JAD file's attributes as properties
 * @throws IOException
 *             If an I/O problem occurred or supplied file is not a JAD file
 */
public static Properties readJadFile(File jadFile) throws IOException {
    LineNumberReader lnr = null;

    try {
        Properties jadProperties = new Properties();

        lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(jadFile)));

        String line = null;
        while ((line = lnr.readLine()) != null) {
            int index = line.indexOf(": ");

            if (index == -1) {
                throw new IOException(res.getString("NoReadJadCorrupt.exception.message"));
            }

            String name = line.substring(0, index);
            String value = line.substring(index + 2);
            jadProperties.setProperty(name, value);
        }

        return jadProperties;
    } finally {
        IOUtils.closeQuietly(lnr);
    }
}