Example usage for java.io Reader read

List of usage examples for java.io Reader read

Introduction

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

Prototype

public int read(char cbuf[]) throws IOException 

Source Link

Document

Reads characters into an array.

Usage

From source file:org.cryptsecure.HttpStringRequest.java

/**
 * Convert the content of a HTTP response into a string.
 * //  w  w  w  .ja  v  a2 s  .c om
 * @param response
 *            the response
 * @return the content as string
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
private static String getContentAsString(HttpResponse response) throws IOException {
    String returnString = "";
    HttpEntity httpEntity = response.getEntity();

    InputStream inputStream = httpEntity.getContent();
    InputStreamReader is = new InputStreamReader(inputStream, "ISO-8859-1"); // "UTF-8");

    if (is != null) {
        Writer writer = new StringWriter();

        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(is);
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            is.close();
        }

        returnString = writer.toString().replace("\n", "");
    }

    return returnString;
}

From source file:FileUtils.java

public static String readStream(Reader stream) throws IOException {

    StringBuffer streamString = new StringBuffer();
    char[] readBuffer = new char[256];
    int readCount = 0;

    while ((readCount = stream.read(readBuffer)) != -1) {
        streamString.append(readBuffer, 0, readCount);
    }/*from w w w .  j  a  va2s  .c o  m*/

    return streamString.toString();
}

From source file:com.ebay.cloud.cms.metadata.dataloader.MetadataDataLoader.java

private static String convertStreamToString(InputStream is) throws IOException {
    if (is != null) {
        Writer writer = new StringWriter();
        char[] buffer = new char[1024];
        try {/*from  ww w.  j  a va2  s .  c o m*/
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            is.close();
        }
        return writer.toString();
    } else {
        return "";
    }
}

From source file:CopyUtils.java

/**
 * Copy chars from a <code>Reader</code> to a <code>Writer</code>.
 * @param input the <code>Reader</code> to read from
 * @param output the <code>Writer</code> to write to
 * @return the number of characters copied
 * @throws IOException In case of an I/O problem
 *//*from  ww  w .j  a  v a  2  s .c om*/
public static int copy(Reader input, Writer output) throws IOException {
    char[] buffer = new char[DEFAULT_BUFFER_SIZE];
    int count = 0;
    int n = 0;
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}

From source file:com.bluetooth.activities.WiFiControl.java

/**
 * This function converts an InputStream in a String, this is needed to
 * serve the .htm file to the client.//from  ww w .  j a va2s .  c  o m
 * <p>
 * To convert the InputStream to String we use the Reader.read(char[]
 * buffer) method. We iterate until the Reader return -1 which means there's
 * no more data to read. We use the StringWriter class to produce the
 * string.
 */
protected static String openHTMLString(Context context, int id) {
    InputStream is = context.getResources().openRawResource(id);
    if (is != null) {
        Writer writer = new StringWriter();

        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return writer.toString();
    } else {
        return "";
    }
}

From source file:it.polimi.brusamentoceruti.moviebookrest.boundary.JsonRequest.java

public static JSONObject doQuery(String Url) throws JSONException, IOException {
    String responseBody = null;/*  w w  w  . j  a v a 2s.co m*/
    HttpGet httpget;
    HttpClient httpClient = new DefaultHttpClient();
    try {
        httpget = new HttpGet(Url);
    } catch (IllegalArgumentException iae) {
        return null;
    }

    HttpResponse response = httpClient.execute(httpget);
    InputStream contentStream = null;
    try {
        StatusLine statusLine = response.getStatusLine();
        if (statusLine == null) {
            throw new IOException(String.format("Unable to get a response from server"));
        }
        int statusCode = statusLine.getStatusCode();
        if (statusCode < 200 && statusCode >= 300) {
            throw new IOException(
                    String.format("OWM server responded with status code %d: %s", statusCode, statusLine));
        }
        /* Read the response content */
        HttpEntity responseEntity = response.getEntity();
        contentStream = responseEntity.getContent();
        Reader isReader = new InputStreamReader(contentStream);
        int contentSize = (int) responseEntity.getContentLength();
        if (contentSize < 0)
            contentSize = 8 * 1024;
        StringWriter strWriter = new StringWriter(contentSize);
        char[] buffer = new char[8 * 1024];
        int n = 0;
        while ((n = isReader.read(buffer)) != -1) {
            strWriter.write(buffer, 0, n);
        }
        responseBody = strWriter.toString();
        contentStream.close();
    } catch (IOException e) {
        throw e;
    } catch (RuntimeException re) {
        httpget.abort();
        throw re;
    } finally {
        if (contentStream != null)
            contentStream.close();
    }
    return new JSONObject(responseBody);
}

From source file:com.tri_voltage.md.io.YoutubeVideoParser.java

private static String getStringFromInputStream(String encoding, InputStream instream)
        throws UnsupportedEncodingException, IOException {
    Writer writer = new StringWriter();

    char[] buffer = new char[1024];
    try {//from   w w w  . ja  v a  2s  .  c o m
        Reader reader = new BufferedReader(new InputStreamReader(instream, encoding));
        int n;
        while ((n = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, n);
        }
    } finally {
        instream.close();
    }
    String result = writer.toString();
    return result;
}

From source file:com.android.volley.misc.Utils.java

public static String readFully(Reader reader) throws IOException {
    try {/* ww  w. j a v a  2s .co  m*/
        StringWriter writer = new StringWriter();
        char[] buffer = new char[1024];
        int count;
        while ((count = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, count);
        }
        return writer.toString();
    } finally {
        reader.close();
    }
}

From source file:eu.e43.impeller.Utils.java

static public String readAll(Reader r) throws IOException {
    int nRead;//from  w ww. j  av  a  2s.  c o  m
    char[] buf = new char[16 * 1024];
    StringBuilder bld = new StringBuilder();
    while ((nRead = r.read(buf)) != -1) {
        bld.append(buf, 0, nRead);
    }
    return bld.toString();
}

From source file:org.apache.manifoldcf.authorities.authorities.jira.JiraSession.java

private static String convertToString(HttpResponse httpResponse) throws IOException {
    HttpEntity entity = httpResponse.getEntity();
    if (entity != null) {
        InputStream is = entity.getContent();
        try {/*  ww w.ja v a 2 s .  c o  m*/
            char[] buffer = new char[65536];
            Reader r = new InputStreamReader(is, getCharSet(entity));
            Writer w = new StringWriter();
            try {
                while (true) {
                    int amt = r.read(buffer);
                    if (amt == -1)
                        break;
                    w.write(buffer, 0, amt);
                }
            } finally {
                w.flush();
            }
            return w.toString();
        } finally {
            is.close();
        }
    }
    return "";
}