Example usage for java.io BufferedReader read

List of usage examples for java.io BufferedReader read

Introduction

In this page you can find the example usage for java.io BufferedReader read.

Prototype

public int read() throws IOException 

Source Link

Document

Reads a single character.

Usage

From source file:com.googlecode.jsonrpc4j.JsonRpcHttpClient.java

private static String readString(InputStream stream) {
    if (stream == null)
        return "null";
    try {//  w  w  w . j a  va2 s  .  c o  m
        StringBuilder buf = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
        for (int ch = reader.read(); ch >= 0; ch = reader.read()) {
            buf.append((char) ch);
        }
        return buf.toString();
    } catch (UnsupportedEncodingException e) {
        return e.getMessage();
    } catch (IOException e) {
        return e.getMessage();
    } finally {
        try {
            stream.close();
        } catch (IOException e) {
        }
    }
}

From source file:com.evolveum.midpoint.testing.model.client.sample.RunScript.java

private static String determineEncoding(String filename) throws IOException {
    BufferedReader br = new BufferedReader(new FileReader(filename));
    StringBuilder sb = new StringBuilder();
    for (;;) {/*from   w ww.j  ava  2s  .co m*/
        int c = br.read();
        if (c == -1) {
            throw new IllegalStateException("No XML declaration found in file " + filename);
        }
        sb.append((char) c);
        if (c == '>') {
            break;
        }
    }
    br.close();
    String xmldecl = sb.toString();
    if (!xmldecl.startsWith("<?xml")) {
        throw new IllegalStateException("No XML declaration found in file " + filename);
    }
    int encodingPos = xmldecl.indexOf("encoding");
    if (encodingPos == -1) {
        return System.getProperty("file.encoding");
    }

    int from1 = xmldecl.indexOf("\'", encodingPos);
    int from2 = xmldecl.indexOf("\"", encodingPos);
    if (from1 == -1 && from2 == -1) {
        throw new IllegalStateException("Incorrect encoding information in XML declaration: " + xmldecl);
    }
    int from, to;
    if (from1 != -1 && from1 < from2) {
        from = from1;
        to = xmldecl.indexOf("\'", from1 + 1);
    } else {
        from = from2;
        to = xmldecl.indexOf("\"", from2 + 1);
    }
    if (to == -1) {
        throw new IllegalStateException("Incorrect encoding information in XML declaration: " + xmldecl);
    }
    return xmldecl.substring(from + 1, to);
}

From source file:net.antidot.sql.model.core.SQLConnector.java

/**
 * Update a database, connected with c, with requests in SQL file.
 * /*from  w  ww .  j  av a  2s  .c om*/
 * @param c
 * @param pathToSQLFile
 * @throws SQLException
 */
public static void updateDatabase(Connection c, String pathToSQLFile) throws SQLException {
    log.debug("[SQLConnector:updateDatabase] pathToSQLFile = " + pathToSQLFile);
    StringBuilder sb = new StringBuilder();
    try {
        FileReader fr = new FileReader(new File(pathToSQLFile));
        // be sure to not have line starting with "--" or "/*" or any other
        // non aplhabetical character
        BufferedReader br = new BufferedReader(fr);

        int s = -1;
        while ((s = br.read()) != -1) {
            sb.appendCodePoint(s);
        }
        br.close();
        // here is our splitter ! We use ";" as a delimiter for each request
        // then we are sure to have well formed statements
        String[] inst = sb.toString().split(";");
        Statement st = c.createStatement();

        for (int i = 0; i < inst.length; i++) {
            // we ensure that there is no spaces before or after the request
            // string
            // in order to not execute empty statements
            if (!inst[i].trim().equals("")) {
                log.debug("[SQLConnector:updateDatabase] >> " + inst[i]);
                st.executeUpdate(inst[i]);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.openanzo.client.cli.CommandLineInterface.java

/**
 * If the PAUSE flag is set then the user must press enter before the program will exit
 * //  w ww  .j ava  2  s . c o m
 * @param errCode
 *            The errCode is the exit code for the program, 0 is normal and 1 is abnormal
 * @param pauseOnExit
 *            If this is true then the user must press enter before the program will exit
 */
private static void exitOnError(int errCode, boolean pauseOnExit) {
    if (pauseOnExit) {
        try {
            System.err.println("Press enter to exit");
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            in.read();
            in.close();
        } catch (IOException e) {
            System.err.println(e.getMessage());
            System.exit(errCode);
        }
    }
    System.exit(errCode);
}

From source file:com.gamerking195.dev.lametric.RestfulWriter.java

private static String readFrom(String url) throws IOException {
    try (InputStream is = new URL(url).openStream()) {
        BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));

        StringBuilder sb = new StringBuilder();
        int cp;//from  ww w.ja v  a  2  s. c  om
        while ((cp = rd.read()) != -1) {
            sb.append((char) cp);
        }
        return sb.toString();
    }
}

From source file:org.addhen.smssync.net.BaseHttpClient.java

public static String convertStreamToString(InputStream is) {

    BufferedReader reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
    StringBuilder sb = new StringBuilder();

    int cp;//from w ww.ja v a  2  s. c  o m
    try {
        while ((cp = reader.read()) != -1) {
            sb.append((char) cp);
        }
    } catch (IOException e) {
        debug(e);
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            debug(e);
        }
    }
    return sb.toString();
}

From source file:json.JsonUI.java

private static String readUrl(String urlString) throws Exception {
    BufferedReader reader = null;
    try {/*ww w  .  ja va 2s  .  c o m*/
        URL url = new URL(urlString);
        reader = new BufferedReader(new InputStreamReader(url.openStream()));
        StringBuilder buffer = new StringBuilder();
        int read;
        while ((read = reader.read()) != -1) {
            buffer.append((char) read);
        }
        return buffer.toString();
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
}

From source file:ebay.dts.client.FileTransferActions.java

private static String compressFileToGzip(String inFilename) throws EbayConnectorException {

    // compress the xml file into gz file in the save folder
    String outFilename = null;//from w  w w. ja va 2s.  c  o  m
    String usingPath = inFilename.substring(0, inFilename.lastIndexOf(File.separator) + 1);
    String fileName = inFilename.substring(inFilename.lastIndexOf(File.separator) + 1);
    outFilename = usingPath + fileName + ".gz";

    try {

        BufferedReader in = new BufferedReader(new FileReader(inFilename));
        BufferedOutputStream out = new BufferedOutputStream(
                new GZIPOutputStream(new FileOutputStream(outFilename)));
        logger.trace("Writing gz file to: " + Utility.getFileLabel(outFilename));

        int c;
        while ((c = in.read()) != -1) {
            out.write(c);
        }
        in.close();
        out.close();

    } catch (FileNotFoundException e) {
        logger.error("Cannot gzip file: " + inFilename);
        throw new EbayConnectorException("Cannot gzip file: " + inFilename, e);
    } catch (IOException e) {
        logger.error("Cannot gzip file: " + inFilename);
        throw new EbayConnectorException("Cannot gzip file: " + inFilename, e);
    }

    logger.info("The compressed file has been saved to " + outFilename);
    return outFilename;
}

From source file:com.sun.faces.generate.HtmlComponentGenerator.java

/**
 * <p>Load the copyright text for the top of each Java source file.</p>
 *
 * @param path Pathname of the copyright file
 */// w w w  .  j av  a2s . c  o m
private static void copyright(String path) throws Exception {

    if (log.isDebugEnabled()) {
        log.debug("Loading copyright text from '" + path + "'");
    }
    StringBuffer sb = new StringBuffer();
    BufferedReader reader = new BufferedReader(new FileReader(path));
    int ch;
    while ((ch = reader.read()) >= 0) {
        sb.append((char) ch);
    }
    reader.close();
    if (log.isDebugEnabled()) {
        log.debug("  Copyright text contains " + sb.length() + " characters");
    }
    copyright = sb.toString();

}

From source file:org.apache.tika.parser.ocr.TesseractOCRParser.java

private static boolean hasPython() {
    // check if python is installed and if the rotation program path has been specified correctly

    boolean hasPython = false;

    try {//from w  ww . j a va 2s . c  om
        Process proc = Runtime.getRuntime().exec("python -h");
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream(), "UTF-8"));
        if (stdInput.read() != -1) {
            hasPython = true;
        }
    } catch (IOException e) {

    }

    return hasPython;
}