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

public static String toString(InputStream is) {
    StringBuilder out = new StringBuilder();
    char[] buffer = new char[512];
    if (is != null) {
        try {//  w  ww . jav a 2 s  .  c  o m
            Reader in = new InputStreamReader(is, "UTF-8");
            try {
                int n;
                while ((n = in.read(buffer, 0, buffer.length)) >= 0) {
                    out.append(buffer, 0, n);
                }
            } finally {
                in.close();
            }
        } catch (IOException ex) {
            out.append("error");
        }
    }
    return out.toString();
}

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);/* w  w  w  . j  a  v a2s  . c o m*/
    }

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

From source file:com.swordlord.gozer.file.GozerFileLoader.java

public static String convertInputStreamToString(InputStream is) {
    String strReturn = "";

    if (is != null) {
        Writer writer = new StringWriter();

        char[] buffer = new char[1024];
        try {//  ww w .  j a va 2  s . 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);
            }

            reader.close();
            strReturn = writer.toString();
            writer.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
    }

    return strReturn;
}

From source file:com.cloudbees.diff.UnifiedDiff.java

private static void copyStreamsCloseAll(Writer writer, Reader reader) throws IOException {
    IOUtils.copy(reader, writer);//  w w  w.ja  v a2s  .  c om
    writer.close();
    reader.close();
}

From source file:co.cask.cdap.client.rest.RestClient.java

/**
 * Utility method for converting {@link org.apache.http.HttpEntity} HTTP entity content to String.
 *
 * @param httpEntity {@link org.apache.http.HttpEntity}
 * @return {@link String} generated from input content stream
 * @throws IOException if HTTP entity is not available
 *///from w  w w . j ava  2  s  .  c o m
public static String toString(HttpEntity httpEntity) throws IOException {
    if (httpEntity == null || httpEntity.getContent() == null) {
        throw new IOException("Empty HttpEntity is received.");
    }
    Charset charset = Charsets.UTF_8;
    ContentType contentType = ContentType.get(httpEntity);
    if (contentType != null && contentType.getCharset() != null) {
        charset = contentType.getCharset();
    }
    Reader reader = new InputStreamReader(httpEntity.getContent(), charset);
    try {
        return CharStreams.toString(reader);
    } finally {
        reader.close();
    }
}

From source file:Main.java

/**
 * Parses the given input stream and returns the corresponding desirialized
 * XML document./*  w ww.  j a  v a 2 s.  c  o  m*/
 * 
 * @param reader the reader containing the serialized XML document
 * @return the deserialized DOM document
 * @throws Exception
 */
public static Document readXML(Reader reader) throws Exception {
    try {
        DocumentBuilder builder = getDocumentBuilder();
        InputSource source = new InputSource(reader);
        Document doc = builder.parse(source);
        return doc;
    } finally {
        reader.close();
    }
}

From source file:Main.java

public static String slurp(final InputStream is, final int bufferSize) {
    final char[] buffer = new char[bufferSize];
    final StringBuilder out = new StringBuilder();
    try {/*from   ww w  . ja  v  a 2s.  c  om*/
        final Reader in = new InputStreamReader(is, "UTF-8");
        try {
            for (;;) {
                int rsz = in.read(buffer, 0, buffer.length);
                if (rsz < 0)
                    break;
                out.append(buffer, 0, rsz);
            }
        } finally {
            in.close();
        }
    } catch (Exception ex) {
    }
    return out.toString();
}

From source file:Main.java

/**
 * Copy a reader to writer./*from   www .  j a  v a  2 s.c  o m*/
 */
private static void copy(Reader in, Writer out) throws IOException {
    try {
        int bytesRead;
        char[] buffer = new char[COPY_BUFFER_SIZE];

        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
        }

        out.flush();
    } finally {
        in.close();
        out.close();
    }
}

From source file:Main.java

/**
 * Copies characters from a reader to a writer. When the reading is done,
 * the reader is closed.//from   w ww  .j a  v a 2 s  .com
 * 
 * @param reader
 *            The reader.
 * @param writer
 *            The writer.
 * @throws IOException
 */
public static void copy(Reader reader, java.io.Writer writer) throws IOException {
    int charsRead;
    char[] buffer = new char[2048];

    while ((charsRead = reader.read(buffer)) > 0) {
        writer.write(buffer, 0, charsRead);
    }

    writer.flush();
    reader.close();
}

From source file:com.thruzero.common.core.utils.FileUtilsExt.java

/**
 * If {@code reader} not {@code null}, close it and ignore {@code IOException}
 *
 * @param reader//from w w w. j a va 2s . c  o m
 */
public static void closeReader(final Reader reader) {
    if (reader != null) {
        try {
            reader.close();
        } catch (IOException e) {
            // ignore IOException
        }
    }
}