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:Main.java

public static String readInputStream(InputStreamReader in) {
    StringBuilder sb = new StringBuilder();
    try {/*w  w  w.j  ava  2 s. co  m*/
        BufferedReader reader = new BufferedReader(in);
        char[] temp = new char[1024];
        int length;
        while ((length = reader.read(temp)) != -1) {
            if (Thread.currentThread().isInterrupted())
                break;
            sb.append(temp, 0, length);
        }
        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            in.close();
        } catch (IOException e) {
        }
    }
    return (sb.toString());
}

From source file:org.wildfly.test.integration.microprofile.config.smallrye.HttpUtils.java

public static String getContent(HttpResponse response) throws IOException {
    try (InputStream stream = response.getEntity().getContent()) {
        StringBuilder builder = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
        char[] buff = new char[512];
        int read;
        while ((read = reader.read(buff)) != -1) {
            builder.append(buff, 0, read);
        }//from w  ww.  j ava  2  s.c  o m
        return builder.toString();
    }
}

From source file:a.org.fakereplace.integration.jbossas.util.HttpUtils.java

public static String getContent(HttpResponse response) throws IOException {
    InputStream stream = null;/*from  ww w .j a v a2 s. co m*/
    try {
        stream = response.getEntity().getContent();
        StringBuilder builder = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
        char[] buff = new char[512];
        int read = -1;
        while ((read = reader.read(buff)) != -1) {
            builder.append(buff, 0, read);
        }
        return builder.toString();
    } finally {
        if (stream != null) {
            stream.close();
        }
    }
}

From source file:com.mmd.mssp.util.HttpUtil.java

public static String httpGet(String uri) throws IOException {
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(uri);
    HttpResponse response = httpclient.execute(httpget);

    logger.info(httpget.getURI().toString() + " == " + response.getStatusLine());

    HttpEntity entity = response.getEntity();
    Header[] cts = response.getHeaders("Content-Type");
    String charset = "UTF-8";
    if (cts.length > 0) {
        String ContentType = cts[0].getValue();
        if (ContentType.contains("charset")) {
            charset = ContentType.split("charset=")[1];
        }/*from   w  w  w  . j a v a  2  s .c o m*/
    }
    if (entity != null) {
        InputStream instream = entity.getContent();
        try {
            StringBuffer buffer = new StringBuffer();
            char[] chars = new char[BUFFER_SIZE];
            while (true) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(instream, charset));
                int len = reader.read(chars);
                if (len < 0) {
                    break;
                }
                buffer.append(chars, 0, len);
            }
            return buffer.toString();
        } catch (IOException ex) {
            throw ex;
        } catch (RuntimeException ex) {
            httpget.abort();
            throw ex;
        } finally {
            instream.close();
            httpclient.getConnectionManager().shutdown();
        }
    }
    return "";
}

From source file:com.admc.util.IOUtil.java

/**
 * Generates a StringBuilder from specified InputStream,
 * using UTF-8 encoding./*from   w  w w  .j  a  v  a 2s. com*/
 *
 * If the input stream can be initialized by this method, then this method
 * will also close it.
 */
public static StringBuilder toStringBuilder(InputStream inputStream, int bufferChars) throws IOException {
    char[] buffer = new char[bufferChars];
    int i;
    StringBuilder sb = new StringBuilder();
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
    try {
        while ((i = reader.read(buffer)) > -1)
            sb.append(buffer, 0, i);
    } finally {
        try {
            reader.close();
        } catch (IOException ioe) {
            log.error("Failed to close reader", ioe);
        }
        reader = null;
    }
    return sb;
}

From source file:net.orpiske.tcs.utils.compression.Decompressor.java

/**
 * Decompress an array of bytes/*www  .  j  av a2  s  .  c o  m*/
 * @param bytes the array to decompress
 * @return a String object with the text
 * @throws IOException if unable to decompress it for any reason
 */
public static String decompress(final byte[] bytes) throws IOException {
    ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
    InputStream gzipInputStream = new GZIPInputStream(inputStream);

    /**
     * Ok, this should be "smarter". Will fix this, eventually ...
     */
    Reader reader = new InputStreamReader(gzipInputStream, Charset.forName("UTF-8"));

    BufferedReader bufferedReader = new BufferedReader(reader);
    StringBuilder builder = new StringBuilder();

    try {
        char[] buffer = new char[1];

        while (bufferedReader.read(buffer) > 0) {
            builder.append(buffer);
        }

        return builder.toString();
    } finally {
        IOUtils.closeQuietly(gzipInputStream);
        IOUtils.closeQuietly(inputStream);
    }
}

From source file:Main.java

private static String getStreamAsString(InputStream stream, String charset) throws IOException {
    try {/* www.j  ava2 s.  c o  m*/
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream, charset));
        StringWriter writer = new StringWriter();

        char[] chars = new char[256];
        int count = 0;
        while ((count = reader.read(chars)) > 0) {
            writer.write(chars, 0, count);
        }

        return writer.toString();
    } finally {
        if (stream != null) {
            stream.close();
        }
    }
}

From source file:org.fusesource.cloudmix.common.util.FileUtils.java

public static String readFile(File file) throws IOException {

    StringBuilder sb = new StringBuilder();

    BufferedReader reader = new BufferedReader(new FileReader(file));

    char[] chars = new char[BUFFER_SIZE];
    int length;/*from   w  ww.j a v  a 2 s  . com*/

    while ((length = reader.read(chars)) > 0) {
        sb.append(chars, 0, length);
    }

    reader.close();
    return sb.toString();
}

From source file:com.example.android.touroflondon.Util.java

/**
 * Reads the route.json and parses it into a JSONObject.
 *
 * @param context//w w  w  .java 2 s. c o m
 * @return
 * @throws IOException
 * @throws JSONException
 */
public static JSONObject readRoute(Context context) throws IOException, JSONException {

    // Read file into String
    InputStream is = context.getResources().openRawResource(R.raw.tour);
    StringBuilder sb = new StringBuilder();
    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        char[] buffer = new char[1024];
        int length;
        while ((length = br.read(buffer)) > 0) {
            sb.append(buffer, 0, length);
        }
    } finally {
        if (is != null) {
            is.close();
        }
    }

    // Covert String to json
    JSONObject json = new JSONObject(sb.toString());

    return json;
}

From source file:org.kaaproject.kaa.server.common.utils.FileUtils.java

/**
 * Read resource./* w  w w .j  a va 2  s.  c o m*/
 *
 * @param resource the resource
 * @return the string
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static String readResource(String resource) throws IOException {
    String result = null;
    try {
        StringBuffer fileData = new StringBuffer();
        InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
        char[] buf = new char[1024];
        int numRead = 0;
        while ((numRead = reader.read(buf)) != -1) {
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
        }
        reader.close();
        result = fileData.toString();
    } catch (IOException exception) {
        LOG.error("Unable to read from specified resource '" + resource + "'! Error: " + exception.getMessage(),
                exception);
        throw exception;
    }
    return result;
}