Example usage for java.io Reader close

List of usage examples for java.io Reader close

Introduction

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

Prototype

public abstract void close() throws IOException;

Source Link

Document

Closes the stream and releases any system resources associated with it.

Usage

From source file:Main.java

private static String readAsString(URLConnection connection) throws IOException {
    InputStream inputStream = connection.getInputStream();
    Reader reader = new InputStreamReader(inputStream, "UTF-8");
    try {/*from w  ww  . j a  v a2  s.  c  o  m*/
        StringBuilder sb = new StringBuilder();
        {
            char[] buffer = new char[2048];
            while (true) {
                int read = reader.read(buffer);
                if (read < 0)
                    break;
                sb.append(buffer, 0, read);
            }
        }

        return sb.toString();
    } finally {
        try {
            reader.close();
        } catch (IOException ignored) {
        }
    }
}

From source file:com.moss.appsnap.keeper.freedesktop.FreeDesktopAppHandler.java

private static StringBuilder read(Reader reader) throws IOException {
    StringBuilder appsMenu = new StringBuilder();
    final char[] buf = new char[1024];
    for (int x = reader.read(buf); x != -1; x = reader.read(buf)) {
        appsMenu.append(buf, 0, x);/*from w  w  w . j  a  v a2  s  . c o  m*/
    }
    reader.close();

    return appsMenu;
}

From source file:Base64.java

private static char[] readChars(final File file) {
    final CharArrayWriter caw = new CharArrayWriter();
    try {//from www . j av  a 2  s  .  com
        final Reader fr = new FileReader(file);
        final Reader in = new BufferedReader(fr);
        int count;
        final 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:com.cloudant.tests.UnicodeTest.java

/**
 * Outputs a plain-text entity to a character accumulator.
 *
 * @param destination An Appendable (such as a StringBuilder, a Writer, or a PrintStream).
 * @throws RuntimeException if there is an exception reading the entity
 * @throws IOException      if there is an exception writing to the destination
 *///  ww  w.jav  a 2 s  .c o m
private static void pipePlainTextEntity(Appendable destination, HttpConnection connection, URI uri)
        throws IOException {
    Charset charset = getPlainTextEntityCharset(connection);
    InputStream stream;
    try {
        stream = connection.responseAsInputStream();
    } catch (IOException e) {
        throw new RuntimeException("Error starting to read from " + uri, e);
    }
    Reader reader = new InputStreamReader(new BufferedInputStream(stream), charset);
    try {
        pipeEntityContentAsChars(destination, reader, uri);
    } finally {
        try {
            reader.close();
        } catch (IOException e) {
            // Ignore errors. We were only reading from a socket.
        }
    }
}

From source file:net.sf.cb2xml.convert.MainframeToXml.java

private static String stripNullChars(String in) {
    try {/*  w ww.  j a  v  a2  s .c  o m*/
        Reader reader = new BufferedReader(new StringReader(in));
        StringBuffer buffer = new StringBuffer();
        int ch;
        while ((ch = reader.read()) > -1) {
            if (ch != 0) {
                buffer.append((char) ch);
            } else {
                buffer.append(' ');
            }
        }
        reader.close();
        return buffer.toString();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.lightboxtechnologies.spectrum.PythonJob.java

static void configPyTask(Job job, PyEngine py, String task, String script) throws Exception {
    final Configuration conf = job.getConfiguration();
    conf.set("com.lbt.scriptName." + task, script);
    conf.set("com.lbt.script." + task, Base64.encodeFromFile(script));

    Reader in = null;
    try {//from   ww w. j av  a2  s .c om
        in = new BufferedReader(new FileReader(script));
        py.eval(in, script);
        in.close();
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:liveplugin.toolwindow.addplugin.git.jetbrains.plugins.github.api.GithubApiUtil.java

@NotNull
private static JsonElement parseResponse(@NotNull InputStream githubResponse) throws IOException {
    Reader reader = new InputStreamReader(githubResponse, "UTF-8");
    try {/*from   w ww.  j  av a  2 s.c o  m*/
        return new JsonParser().parse(reader);
    } catch (JsonSyntaxException jse) {
        throw new GithubJsonException("Couldn't parse GitHub response", jse);
    } finally {
        reader.close();
    }
}

From source file:jo.alexa.sim.ui.logic.RuntimeLogic.java

public static void loadHistory(RuntimeBean runtime, File source) throws IOException {
    InputStream is = new FileInputStream(source);
    Reader rdr = new InputStreamReader(is, "utf-8");
    JSONArray jtranss;//w  w w.  j  a  v a2  s  . co m
    try {
        jtranss = (JSONArray) (mParser.parse(rdr));
    } catch (ParseException e) {
        throw new IOException(e);
    }
    rdr.close();
    List<TransactionBean> transs = FromJSONLogic.fromJSONTransactions(jtranss, runtime.getApp());
    runtime.setHistory(transs);
    runtime.firePropertyChange("history", null, runtime.getHistory());
    setProp("app.history", source.toString());
}

From source file:org.wso2.cdm.agent.utils.HTTPConnectorUtils.java

public static String readResponseBody(final HttpEntity entity) throws IOException, ParseException {

    if (entity == null) {
        throw new IllegalArgumentException("HTTP entity may not be null");
    }/*  w  ww .jav  a  2 s .  c o  m*/

    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 {

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

public static void formatXML(Reader xml, Reader xsl, URIResolver resolver, Writer output) throws Exception {
    try {/*  w w w  . j ava  2 s . c o  m*/
        try {
            try {
                Source xmlSource = new SAXSource(new InputSource(xml));
                Source xslSource = new SAXSource(new InputSource(xsl));
                Result result = new StreamResult(output);
                formatXML(xmlSource, xslSource, resolver, result);
            } finally {
                output.close();
            }
        } finally {
            xsl.close();
        }
    } finally {
        xml.close();
    }
}