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:no.digipost.android.utilities.JSONUtilities.java

public static String getJsonStringFromInputStream(final InputStream inputStream) {
    String content = "";

    if (inputStream != null) {
        Writer writer = new StringWriter();
        int buffer_size = 1024;
        char[] buffer = new char[buffer_size];

        try {/*from   ww w  .  ja v  a 2  s .c  o  m*/
            Reader reader = new BufferedReader(new InputStreamReader(inputStream, ApiConstants.ENCODING),
                    buffer_size);
            int n;

            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }

            inputStream.close();
            reader.close();
            writer.close();
        } catch (Exception e) {
        }
        content = writer.toString();
    }
    return content;
}

From source file:circleplus.app.http.AbstractHttpApi.java

private static String readStream(InputStream is, int len) throws UnsupportedEncodingException, IOException {
    Reader reader = new InputStreamReader(is, "UTF-8");
    try {/*from ww  w.j av  a 2 s .  c om*/
        char[] buffer = new char[len];
        reader.read(buffer);
        return new String(buffer);
    } finally {
        reader.close();
    }
}

From source file:nl.igorski.lib.utils.network.ResponseParser.java

private static String _getResponseBody(final HttpEntity entity) throws IOException, ParseException {
    if (entity == null)
        throw new IllegalArgumentException("HTTP entity may not be null");

    InputStream instream = entity.getContent();

    if (instream == null)
        return "";

    if (entity.getContentLength() > Integer.MAX_VALUE)
        throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");

    String charset = getContentCharSet(entity);

    if (charset == null)
        charset = HTTP.DEFAULT_CONTENT_CHARSET;

    Reader reader = new InputStreamReader(instream, charset);
    StringBuilder buffer = new StringBuilder();

    try {/*  w  w  w.  j  a  v  a2 s.  c om*/
        char[] tmp = new char[1024];
        int l;
        while ((l = reader.read(tmp)) != -1) {
            buffer.append(tmp, 0, l);
        }
    } finally {
        reader.close();
    }
    return buffer.toString();
}

From source file:function.Functions.java

private static int copy(Reader input, Writer output) throws IOException {
    char[] buffer = new char[1024];
    int count = 0;
    int n = 0;//from  w w  w .j  a va2  s.  co m
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}

From source file:fr.itinerennes.utils.IOUtils.java

/**
 * Reads the given input stream and returns it as a string.
 * //from w  ww. j  av  a  2s .  com
 * @param in
 *            an input stream to read
 * @return a string with the content provided by the given input stream
 */
public static String read(final InputStream in) {

    final Reader reader = new InputStreamReader(in);
    final char[] buffer = new char[CHAR_BUF_SIZE];
    final StringBuilder script = new StringBuilder();
    int len = 0;
    try {
        while ((len = reader.read(buffer)) != -1) {
            script.append(buffer, 0, len);
        }
    } catch (final IOException e) {
        LOGGER.error("unable to read the input stream", e);
    }

    return script.toString();
}

From source file:Base64.java

private static char[] readChars(File file) {
    CharArrayWriter caw = new CharArrayWriter();
    try {/*from w ww. ja  va 2  s . c  o m*/
        Reader fr = new FileReader(file);
        Reader in = new BufferedReader(fr);
        int count = 0;
        char[] buf = new char[16384];
        while ((count = in.read(buf)) != -1) {
            if (count > 0)
                caw.write(buf, 0, count);
        }
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return caw.toCharArray();
}

From source file:Main.java

/**
 * Write the entire contents of the supplied string to the given writer. This method always flushes and closes the writer when
 * finished.//from www.  j a  va 2 s  . c o m
 * 
 * @param input the content to write to the writer; may be null
 * @param writer the writer to which the content is to be written
 * @throws IOException
 * @throws IllegalArgumentException if the writer is null
 */
public static void write(Reader input, Writer writer) throws IOException {
    boolean error = false;
    try {
        if (input != null) {
            char[] buffer = new char[1024];
            try {
                int numRead = 0;
                while ((numRead = input.read(buffer)) > -1) {
                    writer.write(buffer, 0, numRead);
                }
            } finally {
                input.close();
            }
        }
    } catch (IOException e) {
        error = true; // this error should be thrown, even if there is an error flushing/closing writer
        throw e;
    } catch (RuntimeException e) {
        error = true; // this error should be thrown, even if there is an error flushing/closing writer
        throw e;
    } finally {
        try {
            writer.flush();
        } catch (IOException e) {
            if (!error)
                throw e;
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                if (!error)
                    throw e;
            }
        }
    }
}

From source file:Main.java

/**
 * Read and return the entire contents of the supplied {@link Reader}. This method always closes the reader when finished
 * reading.//  ww w  . ja  va2 s  .  c  o m
 * 
 * @param reader the reader of the contents; may be null
 * @return the contents, or an empty string if the supplied reader is null
 * @throws IOException if there is an error reading the content
 */
public static String read(Reader reader) throws IOException {
    if (reader == null)
        return "";
    StringBuilder sb = new StringBuilder();
    boolean error = false;
    try {
        int numRead = 0;
        char[] buffer = new char[1024];
        while ((numRead = reader.read(buffer)) > -1) {
            sb.append(buffer, 0, numRead);
        }
    } catch (IOException e) {
        error = true; // this error should be thrown, even if there is an error closing reader
        throw e;
    } catch (RuntimeException e) {
        error = true; // this error should be thrown, even if there is an error closing reader
        throw e;
    } finally {
        try {
            reader.close();
        } catch (IOException e) {
            if (!error)
                throw e;
        }
    }
    return sb.toString();
}

From source file:Main.java

public static String getDtdAsString(String uri) throws IOException {
    Reader in = null;
    if ((uri.startsWith("http")) || (uri.startsWith("ftp")) || (uri.startsWith("file:")))
        in = new InputStreamReader(new URL(uri).openStream());
    else {//  w w w.j a  va2  s .c  o  m
        in = new FileReader(uri);
    }
    StringWriter out = new StringWriter();
    char[] buffer = new char[4096];
    for (int count = in.read(buffer); count != -1; count = in.read(buffer)) {
        out.write(buffer, 0, count);
    }
    return out.getBuffer().toString();
}

From source file:Main.java

public final static String getAssetAsString(Context context, String asset) {
    Reader reader = null;
    StringBuilder sb = new StringBuilder(1024);
    try {//from  w  w  w .  j av  a 2s . c  o m
        reader = new InputStreamReader(context.getAssets().open(asset));
        char[] buf = new char[1024];
        int length;
        while ((length = reader.read(buf)) >= 0)
            sb.append(buf, 0, length);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeQuietly(reader);
    }
    String string = sb.toString();
    return string;
}