/** * Creates a ReversedLinesFileReader with the given block size and encoding. * * @param file * the file to be read * @param blockSize * size of the internal buffer (for ideal performance this should * match with the block size of the underlying file system). * @param encoding * the encoding of the file * @throws IOException if an I/O error occurs * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link UnsupportedEncodingException} in * version 2.2 if the encoding is not supported. */ public ReversedLinesFileReader(final File file, final int blockSize, final String encoding) throws IOException { this(file, blockSize, Charsets.toCharset(encoding)); }
/** * Constructs a LockableFileWriter with a file encoding. * * @param file the file to write to, not null * @param encoding the encoding to use, null means platform default * @param append true if content should be appended, false to overwrite * @param lockDir the directory in which the lock file should be held * @throws NullPointerException if the file is null * @throws IOException in case of an I/O error * @throws java.nio.charset.UnsupportedCharsetException * thrown instead of {@link java.io.UnsupportedEncodingException} in version 2.2 if the encoding is not * supported. */ public LockableFileWriter(final File file, final String encoding, final boolean append, final String lockDir) throws IOException { this(file, Charsets.toCharset(encoding), append, lockDir); }
/** * Reads the contents of a file into a String. The file is always closed. * * @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 java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io * .UnsupportedEncodingException} in version 2.2 if the encoding is not supported. * @since 2.3 */ public static String readFileToString(final File file, final String encoding) throws IOException { return readFileToString(file, Charsets.toCharset(encoding)); }
/** * Reads the contents of a file line by line to a List of Strings. The file is always closed. * * @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 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 List<String> readLines(final File file, final String encoding) throws IOException { return readLines(file, Charsets.toCharset(encoding)); }
/** * Gets the contents at the given URL. * * @param url The URL source. * @param encoding The encoding name for the URL contents. * @return The contents of the URL as a String. * @throws IOException if an I/O exception occurs. * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io * .UnsupportedEncodingException} in version 2.2 if the * encoding is not supported. * @since 2.1 */ public static String toString(final URL url, final String encoding) throws IOException { return toString(url, Charsets.toCharset(encoding)); }
/** * Converts the specified string to an input stream, encoded as bytes * using the specified character encoding. * * @param input the string to convert * @param encoding the encoding to use, null means platform default * @return an input stream * @since 2.3 */ public static InputStream toInputStream(final String input, final Charset encoding) { return new ByteArrayInputStream(input.getBytes(Charsets.toCharset(encoding))); }
/** * Gets the contents at the given URI. * * @param uri The URI source. * @param encoding The encoding name for the URL contents. * @return The contents of the URL as a String. * @throws IOException if an I/O exception occurs. * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io * .UnsupportedEncodingException} in version 2.2 if the * encoding is not supported. * @since 2.1 */ public static String toString(final URI uri, final String encoding) throws IOException { return toString(uri, Charsets.toCharset(encoding)); }
/** * Gets the contents at the given URI. * * @param uri The URI source. * @param encoding The encoding name for the URL contents. * @return The contents of the URL as a String. * @throws IOException if an I/O exception occurs. * @since 2.3. */ public static String toString(final URI uri, final Charset encoding) throws IOException { return toString(uri.toURL(), Charsets.toCharset(encoding)); }
/** * Gets the contents of a <code>byte[]</code> as a String * using the specified character encoding. * <p> * Character encoding names can be found at * <a href="http://www.iana.org/assignments/character-sets">IANA</a>. * * @param input the byte array to read from * @param encoding the encoding to use, null means platform default * @return the requested String * @throws NullPointerException if the input is null * @throws IOException if an I/O error occurs (never occurs) */ public static String toString(final byte[] input, final String encoding) throws IOException { return new String(input, Charsets.toCharset(encoding)); }
/** * Writes a String to a file creating the file if it does not exist. * * @param file the file to write * @param data the content to write to the file * @param encoding the encoding to use, {@code null} means platform default * @param append if {@code true}, then the String will be added to the * end of the file rather than overwriting * @throws IOException in case of an I/O error * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io * .UnsupportedEncodingException} in version 2.2 if the encoding is not supported by the VM * @since 2.1 */ public static void writeStringToFile(final File file, final String data, final String encoding, final boolean append) throws IOException { writeStringToFile(file, data, Charsets.toCharset(encoding), append); }
/** * Writes a CharSequence to a file creating the file if it does not exist. * * @param file the file to write * @param data the content to write to the file * @param encoding the encoding to use, {@code null} means platform default * @param append if {@code true}, then the data will be added to the * end of the file rather than overwriting * @throws IOException in case of an I/O error * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io * .UnsupportedEncodingException} in version 2.2 if the encoding is not supported by the VM * @since 2.1 */ public static void write(final File file, final CharSequence data, final String encoding, final boolean append) throws IOException { write(file, data, Charsets.toCharset(encoding), append); }
/** * Reads the contents of a file into a String. * The file is always closed. * * @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(final File file, final Charset encoding) throws IOException { try (InputStream in = openInputStream(file)) { return IOUtils.toString(in, Charsets.toCharset(encoding)); } }
/** * Converts the specified CharSequence to an input stream, encoded as bytes * using the specified character encoding. * <p> * Character encoding names can be found at * <a href="http://www.iana.org/assignments/character-sets">IANA</a>. * * @param input the CharSequence to convert * @param encoding the encoding to use, null means platform default * @return an input stream * @throws IOException if the encoding is invalid * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io * .UnsupportedEncodingException} in version 2.2 if the * encoding is not supported. * @since 2.0 */ public static InputStream toInputStream(final CharSequence input, final String encoding) throws IOException { return toInputStream(input, Charsets.toCharset(encoding)); }
/** * Reads the contents of a file line by line to a List of Strings. * The file is always closed. * * @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(final File file, final Charset encoding) throws IOException { try (InputStream in = openInputStream(file)) { return IOUtils.readLines(in, Charsets.toCharset(encoding)); } }
/** * Writes chars from a <code>String</code> to bytes on an * <code>OutputStream</code> using the specified character encoding. * <p> * This method uses {@link String#getBytes(String)}. * * @param data the <code>String</code> to write, null ignored * @param output the <code>OutputStream</code> to write to * @param encoding the encoding to use, null means platform default * @throws NullPointerException if output is null * @throws IOException if an I/O error occurs * @since 2.3 */ public static void write(final String data, final OutputStream output, final Charset encoding) throws IOException { if (data != null) { output.write(data.getBytes(Charsets.toCharset(encoding))); } }
/** * Gets the contents of an <code>InputStream</code> as a list of Strings, * one entry per line, using the specified character encoding. * <p> * This method buffers the input internally, so there is no need to use a * <code>BufferedInputStream</code>. * * @param input the <code>InputStream</code> to read from, not null * @param encoding the encoding to use, null means platform default * @return the list of Strings, never null * @throws NullPointerException if the input is null * @throws IOException if an I/O error occurs * @since 2.3 */ public static List<String> readLines(final InputStream input, final Charset encoding) throws IOException { final InputStreamReader reader = new InputStreamReader(input, Charsets.toCharset(encoding)); return readLines(reader); }
/** * Writes bytes from a <code>byte[]</code> to chars on a <code>Writer</code> * using the specified character encoding. * <p> * This method uses {@link String#String(byte[], String)}. * * @param data the byte array to write, do not modify during output, * null ignored * @param output the <code>Writer</code> to write to * @param encoding the encoding to use, null means platform default * @throws NullPointerException if output is null * @throws IOException if an I/O error occurs * @since 2.3 */ public static void write(final byte[] data, final Writer output, final Charset encoding) throws IOException { if (data != null) { output.write(new String(data, Charsets.toCharset(encoding))); } }
/** * Converts the specified string to an input stream, encoded as bytes * using the specified character encoding. * <p> * Character encoding names can be found at * <a href="http://www.iana.org/assignments/character-sets">IANA</a>. * * @param input the string to convert * @param encoding the encoding to use, null means platform default * @return an input stream * @throws IOException if the encoding is invalid * @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 InputStream toInputStream(final String input, final String encoding) throws IOException { final byte[] bytes = input.getBytes(Charsets.toCharset(encoding)); return new ByteArrayInputStream(bytes); }
/** * Writes chars from a <code>char[]</code> to bytes on an * <code>OutputStream</code> using the specified character encoding. * <p> * This method uses {@link String#String(char[])} and * {@link String#getBytes(String)}. * * @param data the char array to write, do not modify during output, * null ignored * @param output the <code>OutputStream</code> to write to * @param encoding the encoding to use, null means platform default * @throws NullPointerException if output is null * @throws IOException if an I/O error occurs * @since 2.3 */ public static void write(final char[] data, final OutputStream output, final Charset encoding) throws IOException { if (data != null) { output.write(new String(data).getBytes(Charsets.toCharset(encoding))); } }
@Test public void testToCharset() { Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset((String) null)); Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset((Charset) null)); Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset(Charset.defaultCharset())); Assert.assertEquals(Charset.forName("UTF-8"), Charsets.toCharset(Charset.forName("UTF-8"))); }