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(char cbuf[], int off, int len) throws IOException 

Source Link

Document

Reads characters into a portion of an array.

Usage

From source file:com.googlecode.jsfFlex.shared.tasks.AbstractFileManipulatorTaskRunner.java

public synchronized String readFileContent(String fileName) {
    StringBuilder fileContent = new StringBuilder();
    BufferedReader bufferRead = null;

    try {//from  w w  w. j av a 2s.  c  o m
        bufferRead = new BufferedReader(new FileReader(new File(fileName)));

        char[] charBuffer = new char[2048];
        int offSet = 0;

        while ((offSet = bufferRead.read(charBuffer, 0, 2048)) > -1) {
            fileContent.append(charBuffer, 0, offSet);
        }

    } catch (FileNotFoundException fileNotFoundExcept) {
        throw new ComponentBuildException("Failure in finding of " + fileName, fileNotFoundExcept);
    } catch (IOException ioExcept) {
        throw new ComponentBuildException("Failure in reading of " + fileName, ioExcept);
    } finally {

        if (bufferRead != null) {
            try {
                bufferRead.close();
            } catch (IOException closerException) {
                _log.debug("Error while closing the writer within readFileContent", closerException);
            }
        }

    }

    return fileContent.toString();
}

From source file:com.googlecode.jsfFlex.renderkit.html.util.JsfFlexResourceImpl.java

private void readInputWriteOutput(InputStream resourceStream, HttpServletResponse httpResponse) {

    PrintWriter responseWriter = null;

    try {/*from  w w  w .java2 s. co  m*/
        responseWriter = httpResponse.getWriter();

        BufferedReader reader = new BufferedReader(new InputStreamReader(resourceStream));

        char[] charBuffer = new char[2048];
        int offSet = 0;
        while ((offSet = reader.read(charBuffer, 0, 2048)) > -1) {
            responseWriter.write(charBuffer, 0, offSet);
        }

        responseWriter.flush();
    } catch (IOException ioException) {
        _log.debug("IOException while writing the script's content to PrintWriter of HttpServletResponse",
                ioException);
    }

}

From source file:de.dan_nrw.android.util.net.TextFileHttpResponseHandler.java

@Override
protected String handleResponseInternal(HttpResponse response) throws IOException {
    BufferedReader reader = null;

    try {/*from   w  ww .j  a va 2s  .  com*/
        reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        StringBuilder stringBuilder = new StringBuilder();

        char[] buffer = new char[1024];
        int charsRead = 0;

        while ((charsRead = reader.read(buffer, 0, buffer.length)) > 0) {
            stringBuilder.append(buffer, 0, charsRead);
        }

        return stringBuilder.toString();
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
}

From source file:hudson.Util.java

public static String loadFile(File logfile, Charset charset) throws IOException {
    if (!logfile.exists())
        return "";

    StringBuilder str = new StringBuilder((int) logfile.length());

    BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(logfile), charset));
    try {/*from   w w  w  . jav  a2 s  .c om*/
        char[] buf = new char[1024];
        int len;
        while ((len = r.read(buf, 0, buf.length)) > 0)
            str.append(buf, 0, len);
    } finally {
        r.close();
    }

    return str.toString();
}

From source file:com.googlecode.jsfFlex.shared.tasks.AbstractFileManipulatorTaskRunner.java

public synchronized String getComponentTemplate(ClassLoader loader, String template) {
    StringBuilder fileContent = new StringBuilder();
    BufferedReader bufferRead = null;

    try {//w  ww. j a va  2s .co m

        bufferRead = new BufferedReader(new InputStreamReader(loader.getResourceAsStream(template)));
        char[] charBuffer = new char[2048];
        int offSet = 0;

        while ((offSet = bufferRead.read(charBuffer, 0, 2048)) > -1) {
            fileContent.append(charBuffer, 0, offSet);
        }

    } catch (FileNotFoundException fileNotFoundExcept) {
        throw new ComponentBuildException(getErrorMessage("getComponentTemplate", template),
                fileNotFoundExcept);
    } catch (IOException ioExcept) {
        throw new ComponentBuildException(getErrorMessage("getComponentTemplate", template), ioExcept);
    } finally {

        if (bufferRead != null) {
            try {
                bufferRead.close();
            } catch (IOException closerException) {
                _log.debug("Error while closing the writer within getComponentTemplate", closerException);
            }
        }

    }

    return fileContent.toString();
}

From source file:com.github.nlloyd.hornofmongo.action.MongoScriptAction.java

/**
 * Borrowed from Rhino's Parser class./*from  w  w  w.ja  va  2  s .  com*/
 * 
 * @param reader
 * @return
 * @throws IOException
 */
private String readFully(Reader reader) throws IOException {
    BufferedReader in = new BufferedReader(reader);
    try {
        char[] cbuf = new char[1024];
        StringBuilder sb = new StringBuilder(1024);
        int bytes_read;
        while ((bytes_read = in.read(cbuf, 0, 1024)) != -1) {
            sb.append(cbuf, 0, bytes_read);
        }
        return sb.toString();
    } finally {
        in.close();
    }
}

From source file:hudson.Util.java

public static String loadFile(File logfile, Charset charset) throws IOException {
    if (!logfile.exists()) {
        return "";
    }//w  w w . j  a v  a  2  s  .  com

    StringBuilder str = new StringBuilder((int) logfile.length());

    BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(logfile), charset));
    char[] buf = new char[1024];
    int len;
    while ((len = r.read(buf, 0, buf.length)) > 0) {
        str.append(buf, 0, len);
    }
    r.close();

    return str.toString();
}

From source file:MyServlet.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    try {//from w w w  . j a  v  a 2s .c  o  m
        // Get a reader to read the incoming data
        BufferedReader reader = req.getReader();

        // Get a writer to write the data in UTF-8
        res.setContentType("text/html; charset=UTF-8");
        //PrintWriter out = res.getWriter();
        out = new PrintWriter(new OutputStreamWriter(res.getOutputStream(), "UTF8"), true);

        char[] buf = new char[4 * 1024]; // 4Kchar buffer
        int len;
        while ((len = reader.read(buf, 0, buf.length)) != -1) {
            out.write(buf, 0, len);
        }
        out.flush();
    } catch (Exception e) {
        out.println("Problem filtering page to UTF-8");
        getServletContext().log(e, "Problem filtering page to UTF-8");
    }
    out.println("Done");
}

From source file:net.xisberto.phonetodesktop.network.URLOptions.java

/**
 * Loads a url and search for a HTML title. <br>
 * Based on the code found at/*from   ww  w .j ava 2 s .com*/
 * http://www.gotoquiz.com/web-coding/programming/
 * java-programming/how-to-extract-titles-from-web-pages-in-java/
 * 
 * @param url
 *            the url to load
 * @return the HTML title or {@code null} if it's not a HTML page or if no
 *         title was found
 * @throws IOException
 */
private String getPageTitle(String url) throws IOException, NullPointerException {
    HttpClient client = new DefaultHttpClient();
    HttpUriRequest request = new HttpGet(url);
    HttpResponse response = client.execute(request);

    if (isCancelled) {
        return null;
    }

    // Make sure this URL goes to a HTML page
    String headerValue = "";
    for (Header header : response.getAllHeaders()) {
        Utils.log("header: " + header.getName());
        if (header.getName().equals("Content-Type")) {
            headerValue = header.getValue();
            break;
        }
    }

    Utils.log("value: " + headerValue);
    String contentType = "";
    Charset charset = Charset.forName("ISO-8859-1");
    int sep = headerValue.indexOf(";");
    if (sep != -1) {
        contentType = headerValue.substring(0, sep);
        Matcher matcherCharset = CHARSET_HEADER.matcher(headerValue);
        if (matcherCharset.find()) {
            charset = Charset.forName(matcherCharset.group(1));
        }
    } else {
        contentType = headerValue;
    }

    if (contentType.equals("text/html")) {
        // Now we can search for <title>
        InputStream in = response.getEntity().getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in, charset));
        int n = 0, totalRead = 0;
        char[] buffer = new char[1024];
        StringBuilder content = new StringBuilder();

        while (totalRead < 8192 && (n = reader.read(buffer, 0, buffer.length)) != -1) {
            content.append(buffer);
            totalRead += n;
            Matcher matcher = TITLE_TAG.matcher(content);
            if (matcher.find()) {
                reader.close();
                String result = matcher.group(1).replaceAll("[\\s\\<>]+", " ").trim();
                return result;
            }
            if (isCancelled) {
                reader.close();
                return null;
            }
            Utils.log("Will read some more");
        }
    }
    return null;
}

From source file:org.opencastproject.deliver.itunesu.HTTPHelper.java

/**
 * Reads the HTTP response from the connection.
 *
 * @throws IOException//from w w  w .  ja v  a 2 s .  c o m
 * @return the response as string
 */
private String readResponse() throws IOException {
    // read the response
    InputStream inputStream = null;
    StringBuffer stringBuffer = new StringBuffer();

    try {
        inputStream = connection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        char[] buffer = new char[BUFFER_SIZE];
        for (int n = 0; n >= 0;) {
            n = reader.read(buffer, 0, buffer.length);
            if (n > 0) {
                stringBuffer.append(buffer, 0, n);
            }
        }
    } finally {
        IOUtils.closeQuietly(inputStream);
    }

    return stringBuffer.toString();
}