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

static public String convertStreamToString(InputStream is) throws IOException {
    if (is != null) {
        Writer writer = new StringWriter();
        char[] buffer = new char[1024];
        try {/* w  w  w. j a va 2s .  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:com.ln.methods.Getcalendar.java

public static void Main() {
    //TODO store file locally and check if file changed == redownload
    try {//from www .j a  v  a 2  s  . c om

        //Get source
        // System.setProperty("http.agent", "lnrev2");
        URL calendar = new URL(Calendarurl);
        HttpURLConnection getsource = (HttpURLConnection) calendar.openConnection();
        InputStream in = new BufferedInputStream(getsource.getInputStream());
        //Write source
        Writer sw = new StringWriter();
        char[] b = new char[1024];
        Reader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
        int n;
        while ((n = reader.read(b)) != -1) {
            sw.write(b, 0, n);
        }
        //Source == String && Send source for parsing
        Parser.source = sw.toString();
        Betaparser.source = sw.toString();

        //String lol = sw.toString();
        //lol = StringUtils.substringBetween(lol, "<month year=\"2012\" num=\"2\">", "</month>");
        //Parser.parse();

        Betaparser.parse();

    } catch (MalformedURLException e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(null, e.getStackTrace(), "Error", JOptionPane.WARNING_MESSAGE);
    } catch (IOException e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(null, e.getStackTrace(), "Error", JOptionPane.WARNING_MESSAGE);
    }
}

From source file:Main.java

public static String readIt(InputStream stream, int maxLen) throws IOException {
    Reader reader = null;
    reader = new InputStreamReader(stream, "UTF-8");
    char[] buffer = new char[maxLen];
    Integer num = reader.read(buffer);
    String response = new String(buffer);
    if (num < 0)
        num = 0;/* ww  w  .  j a va  2s  .  co  m*/
    return response.substring(0, num);
}

From source file:Main.java

public static String streamToString(InputStream is) throws IOException {
    if (is != null) {
        Writer writer = new StringWriter();

        char[] buffer = new char[1024];
        try {//  w  w w .  j  av  a 2  s .co  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:Main.java

public static void transfer(Reader reader, Writer writer) throws IOException {

    char[] buf = new char[8192];
    int count = 0;

    while ((count = reader.read(buf)) >= 0) {

        writer.write(buf, 0, count);/*from  ww  w.  ja  v  a2  s  .c  o  m*/
    }
    writer.flush();
}

From source file:Main.java

/**
 * Slurps a Reader into a String.//from ww  w. j av  a2  s .co m
 * @param in
 * @throws IOException
 */
public static String readerToString(final Reader in) throws IOException {
    StringBuffer out = new StringBuffer();
    char[] b = new char[4096];
    for (int n; (n = in.read(b)) != -1;) {
        out.append(new String(b, 0, n));
    }
    return out.toString();
}

From source file:Main.java

public static String getDefaultCurrencies(InputStream is) {

    Writer writer = new StringWriter();
    char[] buffer = new char[1024];
    try {/*ww w  .j  a v  a  2s. 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);
        }
    } catch (UnsupportedEncodingException e) {
        Log.e(TAG, "loadCurrencies " + "Encoding error");
    } catch (IOException e) {
        Log.e(TAG, "loadCurrencies " + "IOException");
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            Log.e(TAG, "loadCurrencies " + "IOException - close");
        }
    }
    return writer.toString();
}

From source file:Main.java

public static String convertStreamToString(InputStream inputStream) throws IOException {
    if (inputStream != null) {
        Writer writer = new StringWriter();

        char[] buffer = new char[1024];
        try {/*from  ww w.j a  v a  2 s. co  m*/
            Reader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 1024);
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            inputStream.close();
        }
        return writer.toString();
    } else {
        return "";
    }
}

From source file:Main.java

public static void copyCompletely(Reader input, Writer output) throws IOException {
    char[] buf = new char[8192];
    while (true) {
        int length = input.read(buf);
        if (length < 0)
            break;
        output.write(buf, 0, length);//from ww w .java  2 s  . co  m
    }

    try {
        input.close();
    } catch (IOException ignore) {
    }
    try {
        output.close();
    } catch (IOException ignore) {
    }
}

From source file:Main.java

public static long copyLarge(Reader input, Writer output) throws IOException {
    char[] buffer = new char[DEFAULT_BUFFER_SIZE];
    long count = 0;
    int n = 0;//from  w ww  .  jav a 2 s .c  o m
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}