List of usage examples for org.apache.commons.io Charsets toCharset
public static Charset toCharset(String charset)
From source file:com.synopsys.integration.util.ResourceUtil.java
public static String getResourceAsString(final Class<?> clazz, final String resourceName, final String encoding) throws IOException { return getResourceAsString(clazz, resourceName, Charsets.toCharset(encoding)); }
From source file:com.geewhiz.pacify.utils.FileUtils.java
public static List<String> getFileAsLines(URL fileURL, String encoding) { InputStream is = null;//from w w w.java2s.co m try { is = fileURL.openStream(); return IOUtils.readLines(is, Charsets.toCharset(encoding)); } catch (IOException e) { throw new RuntimeException(e); } finally { IOUtils.closeQuietly(is); } }
From source file:com.geewhiz.pacify.utils.FileUtils.java
public static String getFileInOneString(File file, String encoding) { InputStream is = null;// w ww.j ava 2 s. co m try { is = new FileInputStream(file); return IOUtils.toString(is, Charsets.toCharset(encoding)); } catch (IOException e) { throw new RuntimeException(e); } finally { IOUtils.closeQuietly(is); } }
From source file:com.sc.l45.weblogviewer.reader.ReversedLinesFileReaderLineSeparator.java
/** * Creates a ReversedLinesFileReader with the given block size and encoding. * * @param file/* w w w.j a v a 2 s.co m*/ * 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 * @since 2.3 */ public ReversedLinesFileReaderLineSeparator(final File file, final int blockSize, final Charset encoding) throws IOException { this.blockSize = blockSize; this.encoding = encoding; randomAccessFile = new RandomAccessFile(file, "r"); totalByteLength = randomAccessFile.length(); int lastBlockLength = (int) (totalByteLength % blockSize); if (lastBlockLength > 0) { totalBlockCount = totalByteLength / blockSize + 1; } else { totalBlockCount = totalByteLength / blockSize; if (totalByteLength > 0) { lastBlockLength = blockSize; } } currentFilePart = new FilePart(totalBlockCount, lastBlockLength, null); // --- check & prepare encoding --- Charset charset = Charsets.toCharset(encoding); CharsetEncoder charsetEncoder = charset.newEncoder(); float maxBytesPerChar = charsetEncoder.maxBytesPerChar(); if (maxBytesPerChar == 1f) { // all one byte encodings are no problem byteDecrement = 1; } else if (charset == Charset.forName("UTF-8")) { // UTF-8 works fine out of the box, for multibyte sequences a second UTF-8 byte can never be a newline byte // http://en.wikipedia.org/wiki/UTF-8 byteDecrement = 1; } else if (charset == Charset.forName("Shift_JIS")) { // Same as for UTF-8 // http://www.herongyang.com/Unicode/JIS-Shift-JIS-Encoding.html byteDecrement = 1; } else if (charset == Charset.forName("UTF-16BE") || charset == Charset.forName("UTF-16LE")) { // UTF-16 new line sequences are not allowed as second tuple of four byte sequences, // however byte order has to be specified byteDecrement = 2; } else if (charset == Charset.forName("UTF-16")) { throw new UnsupportedEncodingException( "For UTF-16, you need to specify the byte order (use UTF-16BE or UTF-16LE)"); } else { throw new UnsupportedEncodingException( "Encoding " + encoding + " is not supported yet (feel free to submit a patch)"); } // NOTE: The new line sequences are matched in the order given, so it is important that \r\n is BEFORE \n newLineSequences = new byte[][] { "\r\n".getBytes(encoding), "\n".getBytes(encoding), "\r".getBytes(encoding) }; avoidNewlineSplitBufferSize = newLineSequences[0].length; }
From source file:com.izforge.izpack.panels.licence.LicenceLoader.java
/** * Loads the licence into a string with the specified {@code encoding}. * * @param encoding The target character encoding. * @return A string representation of the licence in the specified encoding. * @throws ResourceException If the licence could not be found or if file * content could not be converted to the specified {@code encoding}. *///from w w w. j ava2s. co m String asString(final String encoding) throws ResourceException { URL url = asURL(); InputStream in = null; try { in = url.openStream(); return IOUtils.toString(in, Charsets.toCharset(encoding)); } catch (IOException e) { throw new ResourceNotFoundException("Cannot convert license document from resource " + url.getFile() + " to text: " + e.getMessage()); } finally { IOUtils.closeQuietly(in); } }
From source file:com.izforge.izpack.panels.licence.AbstractLicenceConsolePanel.java
protected String loadLicenceAsString(final String encoding) { URL url = null;//from w w w.j a va 2 s .c om String result = null; try { url = loadLicence(); InputStream in = url.openStream(); try { result = IOUtils.toString(in, Charsets.toCharset(encoding)); } finally { IOUtils.closeQuietly(in); } } catch (IOException e) { logger.warning("Cannot convert license document from resource " + url.getFile() + " to text: " + e.getMessage()); } return result; }
From source file:com.sc.l45.weblogviewer.reader.ReversedLinesFileReaderLineSeparator.java
/** * Creates a ReversedLinesFileReader with the given block size and encoding. * * @param file// w w w . jav a2s.co m * 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 UnsupportedCharsetException * thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not * supported. */ public ReversedLinesFileReaderLineSeparator(final File file, final int blockSize, final String encoding) throws IOException { this(file, blockSize, Charsets.toCharset(encoding)); }
From source file:com.sc.l45.weblogviewer.reader.ReversedLinesFileReaderLineSeparator.java
public ReversedLinesFileReaderLineSeparator(final File file, final Charset encoding2) throws IOException { this(file, 4096, Charsets.toCharset(encoding2)); }
From source file:com.izforge.izpack.core.resource.AbstractResources.java
/** * Reads a string resource./*from w ww .jav a 2s.c o m*/ * * @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; }
From source file:launcher.net.CustomIOUtils.java
/** * Get the contents of a <code>Reader</code> as a <code>byte[]</code> * using the specified character encoding. * <p>/*from w ww.j ava 2 s . c o m*/ * 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 UnsupportedCharsetException * thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not * supported. * @since 1.1 */ public static byte[] toByteArray(Reader input, String encoding) throws IOException { return toByteArray(input, Charsets.toCharset(encoding)); }