Example usage for org.apache.commons.io Charsets toCharset

List of usage examples for org.apache.commons.io Charsets toCharset

Introduction

In this page you can find the example usage for org.apache.commons.io Charsets toCharset.

Prototype

public static Charset toCharset(String charset) 

Source Link

Document

Returns a Charset for the named charset.

Usage

From source file:com.tomagoyaky.jdwp.IOUtils.java

/**
 * Writes the <code>toString()</code> value of each item in a collection to
 * an <code>OutputStream</code> line by line, using the specified character
 * encoding and the specified line ending.
 *
 * @param lines the lines to write, null entries produce blank lines
 * @param lineEnding the line separator to use, null is system default
 * @param output the <code>OutputStream</code> to write to, not null, not closed
 * @param encoding the encoding to use, null means platform default
 * @throws NullPointerException if the output is null
 * @throws IOException          if an I/O error occurs
 * @since 2.3//ww  w.java 2s  .c o  m
 */
public static void writeLines(final Collection<?> lines, String lineEnding, final OutputStream output,
        final Charset encoding) throws IOException {
    if (lines == null) {
        return;
    }
    if (lineEnding == null) {
        lineEnding = LINE_SEPARATOR;
    }
    final Charset cs = Charsets.toCharset(encoding);
    for (final Object line : lines) {
        if (line != null) {
            output.write(line.toString().getBytes(cs));
        }
        output.write(lineEnding.getBytes(cs));
    }
}

From source file:com.tomagoyaky.jdwp.IOUtils.java

/**
 * Writes the <code>toString()</code> value of each item in a collection to
 * an <code>OutputStream</code> line by line, using the specified character
 * encoding and the specified line ending.
 * <p/>/*from  ww w .  jav a2s .  c  o m*/
 * Character encoding names can be found at
 * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
 *
 * @param lines the lines to write, null entries produce blank lines
 * @param lineEnding the line separator to use, null is system default
 * @param output the <code>OutputStream</code> to write to, not null, not closed
 * @param encoding the encoding to use, null means platform default
 * @throws NullPointerException                         if the output is null
 * @throws IOException                                  if an I/O error occurs
 * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io
 *                                                      .UnsupportedEncodingException} in version 2.2 if the
 *                                                      encoding is not supported.
 * @since 1.1
 */
public static void writeLines(final Collection<?> lines, final String lineEnding, final OutputStream output,
        final String encoding) throws IOException {
    writeLines(lines, lineEnding, output, Charsets.toCharset(encoding));
}

From source file:com.example.util.FileUtils.java

/**
 * Reads the contents of a file into a String.
 * The file is always closed.// w w  w  .  j  a  v a2s.  c o m
 *
 * @param file  the file to read, must not be {@code null}
 * @param encoding  the encoding to use, {@code null} means platform default
 * @return the file contents, never {@code null}
 * @throws IOException in case of an I/O error
 * @since 2.3
 */
public static String readFileToString(File file, Charset encoding) throws IOException {
    InputStream in = null;
    try {
        in = openInputStream(file);
        return IOUtils.toString(in, Charsets.toCharset(encoding));
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:com.example.util.FileUtils.java

/**
 * Reads the contents of a file into a String. The file is always closed.
 * //from   www.j a  v a  2  s. c o m
 * @param file
 *            the file to read, must not be {@code null}
 * @param encoding
 *            the encoding to use, {@code null} means platform default
 * @return the file contents, never {@code null}
 * @throws IOException
 *             in case of an I/O error
 * @throws UnsupportedCharsetException
 *             thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
 *             supported.
 * @since 2.3
 */
public static String readFileToString(File file, String encoding) throws IOException {
    return readFileToString(file, Charsets.toCharset(encoding));
}

From source file:launcher.net.CustomIOUtils.java

/**
 * Copy bytes from an <code>InputStream</code> to chars on a
 * <code>Writer</code> using the specified character encoding.
 * <p>/* w ww . j ava2 s  . c o  m*/
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedInputStream</code>.
 * <p>
 * This method uses {@link InputStreamReader}.
 *
 * @param input  the <code>InputStream</code> to read from
 * @param output  the <code>Writer</code> to write to
 * @param encoding  the encoding to use, null means platform default
 * @throws NullPointerException if the input or output is null
 * @throws IOException if an I/O error occurs
 * @since 2.3
 */
public static void copy(InputStream input, Writer output, Charset encoding) throws IOException {
    InputStreamReader in = new InputStreamReader(input, Charsets.toCharset(encoding));
    copy(in, output);
}

From source file:com.example.util.FileUtils.java

/**
 * Reads the contents of a file line by line to a List of Strings.
 * The file is always closed.//from  ww  w.j a  v a  2  s  .  c o  m
 *
 * @param file  the file to read, must not be {@code null}
 * @param encoding  the encoding to use, {@code null} means platform default
 * @return the list of Strings representing each line in the file, never {@code null}
 * @throws IOException in case of an I/O error
 * @since 2.3
 */
public static List<String> readLines(File file, Charset encoding) throws IOException {
    InputStream in = null;
    try {
        in = openInputStream(file);
        return IOUtils.readLines(in, Charsets.toCharset(encoding));
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:launcher.net.CustomIOUtils.java

/**
 * Copy bytes from an <code>InputStream</code> to chars on a
 * <code>Writer</code> using the specified character encoding.
 * <p>//  w w  w  . j  a  va  2 s. c o m
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedInputStream</code>.
 * <p>
 * Character encoding names can be found at
 * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
 * <p>
 * This method uses {@link InputStreamReader}.
 *
 * @param input  the <code>InputStream</code> to read from
 * @param output  the <code>Writer</code> to write to
 * @param encoding  the encoding to use, null means platform default
 * @throws NullPointerException if the input or output is null
 * @throws IOException if an I/O error occurs
 * @throws UnsupportedCharsetException
 *             thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
 *             supported.
 * @since 1.1
 */
public static void copy(InputStream input, Writer output, String encoding) throws IOException {
    copy(input, output, Charsets.toCharset(encoding));
}

From source file:com.example.util.FileUtils.java

/**
 * Reads the contents of a file line by line to a List of Strings. The file is always closed.
 * /*from  ww w.  j a  va  2 s  .  co  m*/
 * @param file
 *            the file to read, must not be {@code null}
 * @param encoding
 *            the encoding to use, {@code null} means platform default
 * @return the list of Strings representing each line in the file, never {@code null}
 * @throws IOException
 *             in case of an I/O error
 * @throws UnsupportedCharsetException
 *             thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
 *             supported.
 * @since 1.1
 */
public static List<String> readLines(File file, String encoding) throws IOException {
    return readLines(file, Charsets.toCharset(encoding));
}

From source file:com.tomagoyaky.jdwp.IOUtils.java

/**
 * Copies bytes from an <code>InputStream</code> to chars on a
 * <code>Writer</code> using the specified character encoding.
 * <p/>//w ww . j  a  v  a 2s. co m
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedInputStream</code>.
 * <p/>
 * This method uses {@link InputStreamReader}.
 *
 * @param input the <code>InputStream</code> to read from
 * @param output the <code>Writer</code> to write to
 * @param inputEncoding the encoding to use for the input stream, null means platform default
 * @throws NullPointerException if the input or output is null
 * @throws IOException          if an I/O error occurs
 * @since 2.3
 */
public static void copy(final InputStream input, final Writer output, final Charset inputEncoding)
        throws IOException {
    final InputStreamReader in = new InputStreamReader(input, Charsets.toCharset(inputEncoding));
    copy(in, output);
}

From source file:com.tomagoyaky.jdwp.IOUtils.java

/**
 * Copies bytes from an <code>InputStream</code> to chars on a
 * <code>Writer</code> using the specified character encoding.
 * <p/>//from   ww  w.jav  a  2 s . c om
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedInputStream</code>.
 * <p/>
 * Character encoding names can be found at
 * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
 * <p/>
 * This method uses {@link InputStreamReader}.
 *
 * @param input the <code>InputStream</code> to read from
 * @param output the <code>Writer</code> to write to
 * @param inputEncoding the encoding to use for the InputStream, null means platform default
 * @throws NullPointerException                         if the input or output is null
 * @throws IOException                                  if an I/O error occurs
 * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io
 *                                                      .UnsupportedEncodingException} in version 2.2 if the
 *                                                      encoding is not supported.
 * @since 1.1
 */
public static void copy(final InputStream input, final Writer output, final String inputEncoding)
        throws IOException {
    copy(input, output, Charsets.toCharset(inputEncoding));
}