/** * 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)); }
private Set<String> getRequiredCharsetNames() { return Charsets.requiredCharsets().keySet(); }
/** * 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(File file, Charset encoding) throws IOException { InputStream in = null; try { in = openInputStream(file); return IOUtils.toString(in, Charsets.toCharset(encoding)); } finally { IOUtils.closeQuietly(in); } }
/** * Copies bytes from an <code>InputStream</code> to chars on a * <code>Writer</code> using the specified character encoding. * <p> * 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)); }
/** * Reads a string resource. * * @param name the resource name * @param encoding the resource encoding. May be {@code null} * @return the resource as a string * @throws ResourceNotFoundException if the resource cannot be found * @throws java.io.IOException for any I/O error */ protected String readString(String name, String encoding) throws IOException { String result; InputStream in = getInputStream(name); try { result = IOUtils.toString(in, Charsets.toCharset(encoding)); } finally { IOUtils.closeQuietly(in); } return result; }
/** * 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 { InputStream in = null; try { in = openInputStream(file); return IOUtils.readLines(in, Charsets.toCharset(encoding)); } finally { IOUtils.closeQuietly(in); } }
/** * 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)); } }
/** {@inheritDoc}*/ @Override public String detectPackageName(final InputStream stream) { try { LineIterator iterator = new LineIterator(new InputStreamReader(stream, Charsets.toCharset(Charset.defaultCharset()))); while (iterator.hasNext()) { String line = iterator.nextLine(); Matcher matcher = pattern.matcher(line); if (matcher.matches()) { return matcher.group(1); } } } finally { IOUtils.closeQuietly(stream); } return UNKNOWN_PACKAGE; }
/** * 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)); }
/** * Gets the contents of a <code>Reader</code> as a <code>byte[]</code> * using the specified character encoding. * <p> * Character encoding names can be found at * <a href="http://www.iana.org/assignments/character-sets">IANA</a>. * <p> * This method buffers the input internally, so there is no need to use a * <code>BufferedReader</code>. * * @param input the <code>Reader</code> to read from * @param encoding the encoding to use, null means platform default * @return the requested byte array * @throws NullPointerException if the input 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 byte[] toByteArray(final Reader input, final String encoding) throws IOException { return toByteArray(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 chars from a <code>CharSequence</code> to bytes on an * <code>OutputStream</code> using the specified character encoding. * <p> * Character encoding names can be found at * <a href="http://www.iana.org/assignments/character-sets">IANA</a>. * <p> * This method uses {@link String#getBytes(String)}. * * @param data the <code>CharSequence</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 * @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 void write(final CharSequence data, final OutputStream output, final String encoding) throws IOException { write(data, output, Charsets.toCharset(encoding)); }
/** * Initialise the wrapped file writer. * Ensure that a cleanup occurs if the writer creation fails. * * @param file the file to be accessed * @param encoding the encoding to use * @param append true to append * @return The initialised writer * @throws IOException if an error occurs */ private Writer initWriter(final File file, final Charset encoding, final boolean append) throws IOException { final boolean fileExistedAlready = file.exists(); try { return new OutputStreamWriter(new FileOutputStream(file.getAbsolutePath(), append), Charsets.toCharset(encoding)); } catch (final IOException | RuntimeException ex) { FileUtils.deleteQuietly(lockFile); if (fileExistedAlready == false) { FileUtils.deleteQuietly(file); } throw ex; } }
/** * Gets the contents of an <code>InputStream</code> as a list of Strings, * one entry per line, using the specified character encoding. * <p> * Character encoding names can be found at * <a href="http://www.iana.org/assignments/character-sets">IANA</a>. * <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 * @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 InputStream input, final String encoding) throws IOException { return readLines(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)); } }
/** * 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)); }
/** * 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 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)); }
return lineIterator(input, Charsets.toCharset(encoding));
/** * 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)); }