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.apache.manifoldcf.connectorcommon.common.CommonsHTTPSender.java

private static String getResponseBodyAsString(BackgroundHTTPThread methodThread)
        throws IOException, InterruptedException, HttpException {
    InputStream is = methodThread.getSafeInputStream();
    if (is != null) {
        try {/*w  w  w  .  ja  v a  2s  .  c  o  m*/
            Charset charSet = methodThread.getCharSet();
            if (charSet == null)
                charSet = StandardCharsets.UTF_8;
            char[] buffer = new char[65536];
            Reader r = new InputStreamReader(is, charSet);
            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 "";
}

From source file:im.vector.util.BugReporter.java

/**
 * Read the file content as String//from   w w w.j  a  v a  2s. c  o m
 *
 * @param fin the input file
 * @return the file content as String
 */
private static String convertStreamToString(File fin) {
    Reader reader = null;

    try {
        Writer writer = new StringWriter();
        InputStream inputStream = new FileInputStream(fin);
        try {
            reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            int n;

            char[] buffer = new char[2048];
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            try {
                if (null != reader) {
                    reader.close();
                }
            } catch (Exception e) {
                Log.e(LOG_TAG, "## convertStreamToString() failed to close inputStream " + e.getMessage());
            }
        }
        return writer.toString();
    } catch (Exception e) {
        Log.e(LOG_TAG, "## convertStreamToString() failed " + e.getMessage());
    } catch (OutOfMemoryError oom) {
        Log.e(LOG_TAG, "## convertStreamToString() failed " + oom.getMessage());
    } finally {
        try {
            if (null != reader) {
                reader.close();
            }
        } catch (Exception e) {
            Log.e(LOG_TAG, "## convertStreamToString() failed to close inputStream " + e.getMessage());
        }
    }

    return "";
}

From source file:com.android.aft.AFNetworkConnection.AFNetworkConnection.java

/**
 * Transform an InputStream into a String
 *
 * @param is InputStream//  w ww. ja  v  a 2s.co m
 * @return String from the InputStream
 * @throws IOException If a problem occurs while reading the InputStream
 */
public static String convertStreamToString(InputStream is, boolean isGzipEnabled, HttpMethod method,
        int contentLength) throws IOException {
    InputStream cleanedIs = is;
    if (isGzipEnabled) {
        cleanedIs = new GZIPInputStream(is);
    }

    try {
        switch (method) {
        case Get:
            final BufferedReader reader = new BufferedReader(new InputStreamReader(cleanedIs));
            final StringBuilder sb = new StringBuilder();

            String line = null;

            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            return sb.toString();

        case Post:
            int i = contentLength;
            if (i < 0) {
                i = 4096;
            }

            final Reader readerPost = new InputStreamReader(cleanedIs);
            final CharArrayBuffer buffer = new CharArrayBuffer(i);
            final char[] tmp = new char[1024];
            int l;
            while ((l = readerPost.read(tmp)) != -1) {
                buffer.append(tmp, 0, l);
            }
            return buffer.toString();

        default:
            return null;
        }
    } finally {
        cleanedIs.close();

        if (isGzipEnabled) {
            is.close();
        }
    }
}

From source file:com.xqdev.jam.MLJAM.java

private static String getBody(HttpServletRequest req) {
    try {/* w  ww .  j  av  a2s .  c  o  m*/
        // Try reading the post body using characters.
        // This might throw an exception if something on the
        // server side already called getInputStream().
        // In that case we'll pull as bytes.
        Reader reader = null;
        try {
            reader = new BufferedReader(req.getReader());
        } catch (IOException e) {
            reader = new BufferedReader(new InputStreamReader(req.getInputStream(), "UTF-8"));
        }

        StringBuffer sbuf = new StringBuffer();
        char[] cbuf = new char[4096];
        int count = 0;
        while ((count = reader.read(cbuf)) != -1) {
            sbuf.append(cbuf, 0, count);
        }
        return sbuf.toString();
    } catch (IOException e2) {
        throw new ServerProblemException("IOException in reading POST body: " + e2.getMessage());
    }
}

From source file:hudson.Util.java

public static void copyStream(Reader in, Writer out) throws IOException {
    char[] buf = new char[8192];
    int len;/*from  w w w  . ja v a  2s  .c o  m*/
    while ((len = in.read(buf)) > 0)
        out.write(buf, 0, len);
}

From source file:it.govpay.web.utils.Utils.java

public static void copy2(Reader in, Writer out) throws IOException {

    // do not allow other threads to read from the
    // input or write to the output while copying is
    // taking place

    synchronized (in) {
        synchronized (out) {

            char[] buffer = new char[256];
            while (true) {
                int bytesRead = in.read(buffer);
                if (bytesRead == -1)
                    break;
                out.write(buffer, 0, bytesRead);
            }//from ww  w. j av a 2  s  .c  om
        }
    }
}

From source file:com.bumptech.glide.disklrucache.DiskLruCacheTest.java

private static String readFile(File file) throws Exception {
    Reader reader = new FileReader(file);
    StringWriter writer = new StringWriter();
    char[] buffer = new char[1024];
    int count;//from   www .j  a  v  a 2s.co  m
    while ((count = reader.read(buffer)) != -1) {
        writer.write(buffer, 0, count);
    }
    reader.close();
    return writer.toString();
}

From source file:com.example.android.networkconnect.DownloadUrl.java

public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
    Reader reader = null;
    reader = new InputStreamReader(stream, "UTF-8");
    char[] buffer = new char[len];
    reader.read(buffer);
    return new String(buffer);
}

From source file:com.eclipsesource.jaxrs.consumer.example.GitHubUserProvider.java

public String convertStreamToString(InputStream is) throws IOException {
    Writer writer = new StringWriter();
    char[] buffer = new char[1024];
    try {//from   www  .  ja  v  a 2 s.c om
        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();
}

From source file:com.morphoss.acal.service.connector.AcalRequestor.java

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