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(java.nio.CharBuffer target) throws IOException 

Source Link

Document

Attempts to read characters into the specified character buffer.

Usage

From source file:net.xy.jcms.controller.configurations.parser.FragmentXMLParser.java

/**
 * reads an input stream as texdata and converts to an string object
 * // w  w  w  .j a  v a 2  s. c om
 * @param stream
 * @return
 */
private static String getStringFromStream(final InputStream stream) {
    final StringBuilder writer = new StringBuilder();
    final BufferedReader reader = new BufferedReader(new InputStreamReader(stream), 1024);
    final char[] buffer = new char[1024];
    try {
        int n;
        while ((n = reader.read(buffer)) != -1) {
            writer.append(buffer, 0, n);
        }
    } catch (final IOException e) {
    }
    return writer.toString();
}

From source file:com.villemos.ispace.httpcrawler.HttpClientConfigurer.java

public static String readFully(InputStream input) throws IOException {

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(input));
    StringBuffer result = new StringBuffer();
    char[] buffer = new char[4 * 1024];
    int charsRead;
    while ((charsRead = bufferedReader.read(buffer)) != -1) {
        result.append(buffer, 0, charsRead);
    }/*  w w  w.  j  a  v  a 2s .  com*/
    input.close();
    bufferedReader.close();

    return result.toString();
}

From source file:org.apache.forrest.core.document.DocumentFactory.java

/**
 * Read a string from an InputStream. Each time a chunk of the file is read
 * we try and work out what type of document we have. Once we know what it
 * is we return an IDocument instance that contains the data read so far and
 * the reader used to read the rest of the data.
 * //  w  w  w. j a  v  a 2  s .  c  o  m
 * @param is
 * @throws ProcessingException 
 */
private static AbstractSourceDocument readFile(final URL sourceURL, final URI requestURI, final InputStream is)
        throws java.io.IOException, ProcessingException {
    AbstractSourceDocument doc = null;
    final StringBuffer fileData = new StringBuffer(1024);
    final BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    char[] buf = new char[1000];
    int numRead = 0;
    String type = null;
    while (type == null && (numRead = reader.read(buf)) != -1) {
        final String readData = String.valueOf(buf, 0, numRead);
        fileData.append(readData);
        buf = new char[1024];
        if (fileData.toString().contains("<?xml")) {
            type = getXMLDocumentType(fileData.toString());
            if (type != null) {
                doc = new XMLSourceDocument(requestURI, fileData.toString(), reader, type);
            }
        }
    }
    if (type == null) {
        if (fileData.toString().contains("<?xml")) {
            doc = new XMLSourceDocument(requestURI, fileData.toString(), reader, "application/xml");
        } else if (fileData.toString().toLowerCase().contains("<html>")) {
            doc = new DefaultSourceDocument(requestURI, fileData.toString(), reader, "html");
        } else {
            String file = sourceURL.getFile();
            String ext = file.substring(file.lastIndexOf(".") + 1);
            log.warn("We don't know the document type, so using source file extension: " + ext);
            doc = new DefaultSourceDocument(requestURI, fileData.toString(), reader, ext);
        }
    }
    return doc;
}

From source file:com.newrelic.agent.android.harvest.HarvestConnection.java

public static String readResponse(HttpResponse httpResponse) throws IOException {
    char[] cArr = new char[AccessibilityNodeInfoCompat.ACTION_SCROLL_BACKWARD];
    StringBuilder stringBuilder = new StringBuilder();
    InputStream content = httpResponse.getEntity().getContent();
    try {//  w  w  w .jav a 2s .co m
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(content));
        while (true) {
            int read = bufferedReader.read(cArr);
            if (read < 0) {
                break;
            }
            stringBuilder.append(cArr, 0, read);
        }
        return stringBuilder.toString();
    } finally {
        content.close();
    }
}

From source file:org.hbird.business.celestrack.http.CelestrackReader.java

/**
 * Helper method to read a HTML byte stream returned through
 * //from  w ww . ja v a  2  s .  c  o  m
 * @param input The input stream
 * @return A string of the complete data
 * @throws IOException
 */
protected static String readFully(InputStream input) throws IOException {

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(input));
    StringBuffer result = new StringBuffer();
    char[] buffer = new char[4 * 1024];
    int charsRead;
    while ((charsRead = bufferedReader.read(buffer)) != -1) {
        result.append(buffer, 0, charsRead);
    }
    input.close();
    bufferedReader.close();

    return result.toString();
}

From source file:org.muckebox.android.net.ApiHelper.java

public static String getResponseAsString(HttpResponse response) throws IOException, AuthenticationException {
    StatusLine statusLine = response.getStatusLine();

    Log.d(LOG_TAG, "HTTP Response " + statusLine.getStatusCode() + " " + statusLine.getReasonPhrase());

    switch (statusLine.getStatusCode()) {
    case 401://from   ww w . j  a va  2  s  .  c  o  m
        throw new AuthenticationException(statusLine.getReasonPhrase());
    case 200:
        break;
    default:
        throw new IOException();
    }

    BufferedReader reader = new BufferedReader(
            new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
    StringBuilder str = new StringBuilder();
    char[] charBuf = new char[1024];

    int bytes_read;

    while ((bytes_read = reader.read(charBuf)) != -1)
        str.append(charBuf, 0, bytes_read);

    return str.toString();
}

From source file:org.hbird.estcube.mibreader.Parser.java

protected static String readFully(InputStream input) throws IOException {

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(input));
    StringBuffer result = new StringBuffer();
    char[] buffer = new char[4 * 1024];
    int charsRead;
    while ((charsRead = bufferedReader.read(buffer)) != -1) {
        result.append(buffer, 0, charsRead);
    }/*from  w w w . ja va 2  s  .  c o  m*/
    input.close();
    bufferedReader.close();

    return result.toString();
}

From source file:com.jts.main.helper.Http.java

public static String get(String urlString) {
    BufferedReader reader = null;
    try {/*from   w w  w. j a v a 2 s  .  c o m*/
        URL url = new URL(urlString);
        reader = new BufferedReader(new InputStreamReader(url.openStream()));
        StringBuilder buffer = new StringBuilder();
        int read;
        char[] chars = new char[1024];
        while ((read = reader.read(chars)) != -1) {
            buffer.append(chars, 0, read);
        }
        return buffer.toString();
    } catch (MalformedURLException ex) {
        errMsg = ex.getMessage();
    } catch (IOException ex) {
        errMsg = ex.getMessage();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ex) {
                errMsg = ex.getMessage();
            }
        }
    }
    return "";
}

From source file:Main.java

/**
 * @param r Will be read and closed/*from   www .  j  av a2  s .  co  m*/
 * @return The contents of input
 */
public static String read(Reader r) {
    try {
        BufferedReader reader = r instanceof BufferedReader ? (BufferedReader) r : new BufferedReader(r);
        final int bufSize = 8192; // this is the default BufferredReader
        // buffer size
        StringBuilder sb = new StringBuilder(bufSize);
        char[] cbuf = new char[bufSize];
        while (true) {
            int chars = reader.read(cbuf);
            if (chars == -1)
                break;
            sb.append(cbuf, 0, chars);
        }
        return sb.toString();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            r.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:org.openadaptor.legacy.convertor.dataobjects.OrderedMapToDataObjectConvertor.java

/**
 * Read complete contents of a text file into a String.
 * @param filePath  file to be read/*from   w  ww. j a v  a2  s. c  o  m*/
 * @return String containing contents of the file
 * @throws IOException if any I/O problems are encountered.
 */
private static String readFile(String filePath) throws IOException {
    StringBuffer fileData = new StringBuffer();
    BufferedReader reader = new BufferedReader(new FileReader(filePath));
    char[] buf = new char[2048];
    int read = 0;
    while ((read = reader.read(buf)) != -1) {
        String s = String.valueOf(buf, 0, read);
        fileData.append(s);
    }
    reader.close();
    return fileData.toString();
}