Java InputStream Read by Charset getText(InputStream stream, Charset charset)

Here you can find the source of getText(InputStream stream, Charset charset)

Description

Read an InputStream completely, using the specified encoding.

License

Open Source License

Parameter

Parameter Description
stream the InputStream to read.
charset the character set to use for the encoding of the file.

Return

the text, or an empty string if an error occurred.

Declaration

public static String getText(InputStream stream, Charset charset) 

Method Source Code

//package com.java2s;
/*---------------------------------------------------------------
*  Copyright 2005 by the Radiological Society of North America
*
*  This source software is released under the terms of the
*  RSNA Public License (http://mirc.rsna.org/rsnapubliclicense)
*----------------------------------------------------------------*/

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URL;
import java.nio.charset.Charset;

import java.util.jar.JarFile;

import java.util.zip.ZipFile;

public class Main {
    public static Charset utf8 = Charset.forName("UTF-8");

    /**/*from w  ww.  j  a  v  a2 s. c o  m*/
     * Read an InputStream completely, using the UTF-8 encoding.
     * @param stream the InputStream to read.
     * @return the text, or an empty string if an error occurred.
     */
    public static String getText(InputStream stream) {
        return getText(stream, utf8);
    }

    /**
     * Read an InputStream completely, using the specified encoding, or
     * UTF-8 if the specified encoding is not supported.
     * @param stream the InputStream to read.
     * @param encoding the name of the charset to use.
     * @return the text, or an empty string if an error occurred.
     */
    public static String getText(InputStream stream, String encoding) {
        Charset charset;
        try {
            charset = Charset.forName(encoding);
        } catch (Exception ex) {
            charset = utf8;
        }
        return getText(stream, charset);
    }

    /**
     * Read an InputStream completely, using the specified encoding.
     * @param stream the InputStream to read.
     * @param charset the character set to use for the encoding of the file.
     * @return the text, or an empty string if an error occurred.
     */
    public static String getText(InputStream stream, Charset charset) {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(stream, charset));
            StringWriter sw = new StringWriter();
            int n;
            char[] cbuf = new char[1024];
            while ((n = br.read(cbuf, 0, cbuf.length)) != -1)
                sw.write(cbuf, 0, n);
            br.close();
            return sw.toString();
        } catch (Exception e) {
            close(br);
            return "";
        }
    }

    /**
     * Read a text file completely, using the UTF-8 encoding.
     * @param file the file to read.
     * @return the text of the file, or an empty string if an error occurred.
     */
    public static String getText(File file) {
        return getText(file, utf8);
    }

    /**
     * Read a text file completely, using the specified encoding, or
     * UTF-8 if the specified encoding is not supported.
     * @param file the file to read.
     * @param encoding the name of the charset to use.
     * @return the text of the file, or an empty string if an error occurred.
     */
    public static String getText(File file, String encoding) {
        Charset charset;
        try {
            charset = Charset.forName(encoding);
        } catch (Exception ex) {
            charset = utf8;
        }
        return getText(file, charset);
    }

    /**
     * Read a text file completely, using the specified encoding.
     * @param file the file to read.
     * @param charset the character set to use for the encoding of the file.
     * @return the text of the file, or an empty string if an error occurred.
     */
    public static String getText(File file, Charset charset) {
        BufferedReader br = null;
        try {
            if (!file.exists())
                return "";
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
            StringWriter sw = new StringWriter();
            int n;
            char[] cbuf = new char[1024];
            while ((n = br.read(cbuf, 0, cbuf.length)) != -1)
                sw.write(cbuf, 0, n);
            br.close();
            return sw.toString();
        } catch (Exception e) {
            close(br);
            return "";
        }
    }

    /**
     * Read a resource completely, using the UTF-8 encoding.
     * This method uses the context classloader to find the resource.
     * @param url the resource to read.
     * @return the text, or an empty string if an error occurred.
     */
    public static String getText(URL url) {
        return getText(url, utf8);
    }

    /**
     * Read a resource completely, using the specified encoding, or
     * UTF-8 if the specified encoding is not supported.
     * This method uses the context classloader to find the resource.
     * @param url the resource to read.
     * @param encoding the name of the charset to use.
     * @return the text, or an empty string if an error occurred.
     */
    public static String getText(URL url, String encoding) {
        Charset charset;
        try {
            charset = Charset.forName(encoding);
        } catch (Exception ex) {
            charset = utf8;
        }
        return getText(url, charset);
    }

    /**
     * Read a resource completely, using the specified encoding.
     * This method uses the context classloader to find the resource.
     * @param url the resource to read.
     * @param charset the character set to use for the encoding of the file.
     * @return the text, or an empty string if an error occurred.
     */
    public static String getText(URL url, Charset charset) {
        InputStream is = null;
        try {
            is = url.openStream();
            return getText(is, charset);
        } catch (Exception e) {
            close(is);
            return "";
        }
    }

    /**
     * Close an InputStream and ignore Exceptions.
     * @param stream the stream to close.
     */
    public static void close(InputStream stream) {
        if (stream != null) {
            try {
                stream.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close an OutputStream and ignore Exceptions.
     * @param stream the stream to close.
     */
    public static void close(OutputStream stream) {
        if (stream != null) {
            try {
                stream.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close a Writer and ignore Exceptions.
     * @param writer the writer to close.
     */
    public static void close(Writer writer) {
        if (writer != null) {
            try {
                writer.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close a Reader and ignore Exceptions.
     * @param reader the reader to close.
     */
    public static void close(Reader reader) {
        if (reader != null) {
            try {
                reader.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close a JarFile and ignore Exceptions.
     * @param jarFile the JarFile to close.
     */
    public static void close(JarFile jarFile) {
        if (jarFile != null) {
            try {
                jarFile.close();
            } catch (Exception ignore) {
            }
        }
    }

    /**
     * Close a ZipFile and ignore Exceptions.
     * @param zipFile the zipFile to close.
     */
    public static void close(ZipFile zipFile) {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (Exception ignore) {
            }
        }
    }
}

Related

  1. fromUnicode(String charset, String input)
  2. getContentFromInputStream(InputStream in, String charset)
  3. getHeaderLen(FileInputStream in, Charset encoding)
  4. getInputCharset()
  5. getStreamAsString(InputStream source, Charset charset)
  6. input2String(InputStream input, Charset encoding)
  7. inputStreamAsString(InputStream stream, Charset cs)
  8. inputStreamToString(final InputStream inputStream, final String charsetName)
  9. inputstreamToString(InputStream input, CharsetDecoder decoder)