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:com.taveloper.http.test.ParseBenchmark.java

private static String resourceToString(String path) throws Exception {
    InputStream in = ParseBenchmark.class.getResourceAsStream(path);
    if (in == null) {
        throw new IllegalArgumentException("No such file: " + path);
    }//from ww w. ja  v a  2  s.co m

    Reader reader = new InputStreamReader(in, "UTF-8");
    char[] buffer = new char[8192];
    StringWriter writer = new StringWriter();
    int count;
    while ((count = reader.read(buffer)) != -1) {
        writer.write(buffer, 0, count);
    }
    reader.close();
    return writer.toString();
}

From source file:FileUtil.java

/**
 *  Makes a new temporary file and writes content into it. The temporary
 *  file is created using <code>File.createTempFile()</code>, and the usual
 *  semantics apply.  The files are not deleted automatically in exit.
 *
 *  @param content Initial content of the temporary file.
 *  @param encoding Encoding to use.// w  ww  .j a  va  2  s  .  co m
 *  @return The handle to the new temporary file
 *  @throws IOException If the content creation failed.
 *  @see java.io.File#createTempFile(String,String,File)
 */
public static File newTmpFile(String content, String encoding) throws IOException {
    Writer out = null;
    Reader in = null;
    File f = null;

    try {
        f = File.createTempFile("jspwiki", null);

        in = new StringReader(content);

        out = new OutputStreamWriter(new FileOutputStream(f), encoding);

        copyContents(in, out);
    } finally {
        if (in != null)
            in.close();
        if (out != null)
            out.close();
    }

    return f;
}

From source file:com.freedomotic.plugin.purl.Purl.java

private static String readPage(URL url) throws Exception {

    DefaultHttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(url.toURI());
    HttpResponse response = client.execute(request);

    Reader reader = null;
    try {// w ww  . jav  a  2  s .co m
        reader = new InputStreamReader(response.getEntity().getContent());

        StringBuilder sb = new StringBuilder();
        int read;
        char[] cbuf = new char[1024];
        while ((read = reader.read(cbuf)) != -1) {
            sb.append(cbuf, 0, read);
        }
        return sb.toString();

    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                //do nothing
            }
        }
    }
}

From source file:de.uzk.hki.da.sb.Utilities.java

/**
 * Deserializes the given file and returns its content as a string
 * /*from  w w  w  .  jav a 2s.  c  o m*/
 * @param file The file to read
 * @return The file content as a string
 * @throws Exception
 */
public static String readFile(File file) throws Exception {

    Reader reader;
    try {
        reader = new FileReader(file.getAbsolutePath());
    } catch (FileNotFoundException e) {
        throw new Exception("Couldn't create reader", e);
    }
    String text = "";

    try {
        for (int c; (c = reader.read()) != -1;)
            text += (char) c;

        reader.close();

    } catch (IOException e) {
        throw new Exception("Couldn't read file " + file.getAbsolutePath(), e);
    }

    return text;
}

From source file:com.google.dart.tools.core.utilities.io.FileUtilities.java

/**
 * Return the contents of the given reader, interpreted as a string. The {@link Reader} will be
 * closed.//from   ww w .j a v a2  s  .c om
 * 
 * @param reader the reader whose contents are to be returned
 * @return the contents of the given reader, interpreted as a string
 * @throws IOException if the reader could not be read
 */
public static String getContents(Reader reader) throws IOException {
    try {
        return CharStreams.toString(reader);
    } finally {
        reader.close();
    }
}

From source file:ded.model.Diagram.java

/** Read a Diagram from a file, expect the JSON format only. */
public static Diagram readFromFile(String fname) throws Exception {
    FileInputStream fis = new FileInputStream(fname);
    try {//  ww w. j  ava 2s  . c  o m
        Reader r = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
        try {
            return readFromReader(r);
        } finally {
            r.close();
            fis = null;
        }
    } finally {
        if (fis != null) {
            fis.close();
        }
    }
}

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

public static String readFully(Reader reader) throws IOException {
    try {//from   www.ja  v  a2s  .c o 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:com.glaf.core.util.IOUtils.java

/**
 * write string./*from w w  w .  ja  va2s.com*/
 * 
 * @param writer
 *            Writer instance.
 * @param string
 *            String.
 * @throws IOException
 */
public static long write(Writer writer, String string) throws IOException {
    Reader reader = new StringReader(string);
    try {
        return write(reader, writer);
    } finally {
        reader.close();
    }
}

From source file:net.opentracker.android.OTSend.java

/**
 * getResponseBody function gives out the HTTP POST data from the given
 * httpResponse output: data from the http as string input : httpEntity type
 * //from w  w  w .  j  a va2 s .c o  m
 * based on:
 * http://thinkandroid.wordpress.com/2009/12/30/getting-response-body
 * -of-httpresponse/
 */
private static String getResponseBody(final HttpEntity entity) throws IOException, ParseException {
    LogWrapper.v(TAG, "getResponseBody(final HttpEntity entity)");

    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 = EntityUtils.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:FileCopyUtils.java

/**
 * Copy the contents of the given Reader to the given Writer.
 * Closes both when done.//from   w  w  w.j a  va  2  s.co  m
 * @param in the Reader to copy from
 * @param out the Writer to copy to
 * @return the number of characters copied
 * @throws IOException in case of I/O errors
 */
public static int copy(Reader in, Writer out) throws IOException {

    try {
        int byteCount = 0;
        char[] buffer = new char[BUFFER_SIZE];
        int bytesRead = -1;
        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
            byteCount += bytesRead;
        }
        out.flush();
        return byteCount;
    } finally {
        try {
            in.close();
        } catch (IOException ex) {
            System.out.println("Could not close Reader" + ex);
        }
        try {
            out.close();
        } catch (IOException ex) {
            System.out.println("Could not close Writer:" + ex);
        }
    }
}