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.example.util.FileUtils.java

/**
 * Writes a String to a file creating the file if it does not exist.
 *
 * @param file  the file to write/*from  ww  w.  j  a v a  2  s . c o  m*/
 * @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 UnsupportedCharsetException
 *             thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
 *             supported by the VM
 * @since 2.1
 */
public static void writeStringToFile(File file, String data, String encoding, boolean append)
        throws IOException {
    writeStringToFile(file, data, Charsets.toCharset(encoding), append);
}

From source file:launcher.net.CustomIOUtils.java

/**
 * Copy chars from a <code>Reader</code> to bytes on an
 * <code>OutputStream</code> using the specified character encoding, and
 * calling flush.//from  w  ww  .ja  va2  s  .  c o m
 * <p>
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedReader</code>.
 * </p>
 * <p>
 * Due to the implementation of OutputStreamWriter, this method performs a
 * flush.
 * </p>
 * <p>
 * This method uses {@link OutputStreamWriter}.
 * </p>
 *
 * @param input  the <code>Reader</code> to read from
 * @param output  the <code>OutputStream</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(Reader input, OutputStream output, Charset encoding) throws IOException {
    OutputStreamWriter out = new OutputStreamWriter(output, Charsets.toCharset(encoding));
    copy(input, out);
    // XXX Unless anyone is planning on rewriting OutputStreamWriter,
    // we have to flush here.
    out.flush();
}

From source file:launcher.net.CustomIOUtils.java

/**
 * Copy chars from a <code>Reader</code> to bytes on an
 * <code>OutputStream</code> using the specified character encoding, and
 * calling flush.//from ww w . j  a  v  a  2s . c  om
 * <p>
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedReader</code>.
 * <p>
 * Character encoding names can be found at
 * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
 * <p>
 * Due to the implementation of OutputStreamWriter, this method performs a
 * flush.
 * <p>
 * This method uses {@link OutputStreamWriter}.
 *
 * @param input  the <code>Reader</code> to read from
 * @param output  the <code>OutputStream</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(Reader input, OutputStream output, String encoding) throws IOException {
    copy(input, output, Charsets.toCharset(encoding));
}

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

/**
 * Writes a CharSequence to a file creating the file if it does not exist.
 *
 * @param file  the file to write// w  w  w .  jav  a2  s. c  om
 * @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 UnsupportedCharsetException
 *             thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
 *             supported by the VM
 * @since IO 2.1
 */
public static void write(File file, CharSequence data, String encoding, boolean append) throws IOException {
    write(file, data, Charsets.toCharset(encoding), append);
}

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

/**
 * Copies chars from a <code>Reader</code> to bytes on an
 * <code>OutputStream</code> using the specified character encoding, and
 * calling flush./*  ww  w.ja v a 2  s  .  c  o  m*/
 * <p>
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedReader</code>.
 * </p>
 * <p>
 * Due to the implementation of OutputStreamWriter, this method performs a
 * flush.
 * </p>
 * <p>
 * This method uses {@link OutputStreamWriter}.
 * </p>
 *
 * @param input the <code>Reader</code> to read from
 * @param output the <code>OutputStream</code> to write to
 * @param outputEncoding the encoding to use for the OutputStream, 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 Reader input, final OutputStream output, final Charset outputEncoding)
        throws IOException {
    final OutputStreamWriter out = new OutputStreamWriter(output, Charsets.toCharset(outputEncoding));
    copy(input, out);
    // XXX Unless anyone is planning on rewriting OutputStreamWriter,
    // we have to flush here.
    out.flush();
}

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

/**
 * Copies chars from a <code>Reader</code> to bytes on an
 * <code>OutputStream</code> using the specified character encoding, and
 * calling flush.//from   w w w.  ja  v  a  2 s.  co m
 * <p/>
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedReader</code>.
 * <p/>
 * Character encoding names can be found at
 * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
 * <p/>
 * Due to the implementation of OutputStreamWriter, this method performs a
 * flush.
 * <p/>
 * This method uses {@link OutputStreamWriter}.
 *
 * @param input the <code>Reader</code> to read from
 * @param output the <code>OutputStream</code> to write to
 * @param outputEncoding the encoding to use for the OutputStream, 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 Reader input, final OutputStream output, final String outputEncoding)
        throws IOException {
    copy(input, output, Charsets.toCharset(outputEncoding));
}

From source file:org.apache.jackrabbit.oak.plugins.segment.file.ReversedLinesFileReader.java

/**
 * Creates a ReversedLinesFileReader with the given block size and encoding.
 *
 * @param file/*from   ww  w.j a v  a  2s  . 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 ReversedLinesFileReader(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
            charset == Charset.forName("windows-31j") || // Windows code page 932 (Japanese)
            charset == Charset.forName("x-windows-949") || // Windows code page 949 (Korean)
            charset == Charset.forName("gbk") || // Windows code page 936 (Simplified Chinese)
            charset == Charset.forName("x-windows-950")) { // Windows code page 950 (Traditional Chinese)
        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:org.apache.jackrabbit.oak.plugins.segment.file.ReversedLinesFileReader.java

/**
 * Creates a ReversedLinesFileReader with the given block size and encoding.
 *
 * @param file/*from w w w .  j a  va 2 s  . com*/
 *            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 ReversedLinesFileReader(final File file, final int blockSize, final String encoding) throws IOException {
    this(file, blockSize, Charsets.toCharset(encoding));
}

From source file:org.apache.kylin.measure.extendedcolumn.ExtendedColumnMeasureType.java

public IAdvMeasureFiller getAdvancedTupleFiller(FunctionDesc function, TupleInfo returnTupleInfo,
        Map<TblColRef, Dictionary<String>> dictionaryMap) {
    final TblColRef extended = getExtendedColumn(function);
    final int extendedColumnInTupleIdx = returnTupleInfo.hasColumn(extended)
            ? returnTupleInfo.getColumnIndex(extended)
            : -1;/*from w  w  w .  j  a  v a 2s. c  o  m*/

    if (extendedColumnInTupleIdx == -1) {
        throw new RuntimeException("Extended column is not required in returnTupleInfo");
    }

    return new IAdvMeasureFiller() {
        private String value;

        @Override
        public void reload(Object measureValue) {
            ByteArray byteArray = (ByteArray) measureValue;
            //the array in ByteArray is garanteed to be completed owned by the ByteArray 
            value = new String(byteArray.array(), Charsets.toCharset("UTF-8"));
        }

        @Override
        public int getNumOfRows() {
            return 1;
        }

        @Override
        public void fillTuple(Tuple tuple, int row) {
            tuple.setDimensionValue(extendedColumnInTupleIdx, value);
        }
    };
}

From source file:org.apache.nifi.processors.aws.wag.AbstractAWSGatewayApiProcessor.java

/**
 * Returns a map of Query Parameter Name to Values
 *
 * @param context ProcessContext/* w w  w  .  j  a  v  a 2  s. c om*/
 * @return Map of names and values
 */
protected Map<String, List<String>> getParameters(ProcessContext context) {

    if (!context.getProperty(PROP_QUERY_PARAMS).isSet()) {
        return new HashMap<>();
    }
    final String queryString = context.getProperty(PROP_QUERY_PARAMS).evaluateAttributeExpressions().getValue();
    List<NameValuePair> params = URLEncodedUtils.parse(queryString, Charsets.toCharset("UTF-8"));

    if (params.isEmpty()) {
        return new HashMap<>();
    }

    Map<String, List<String>> map = new HashMap<>();

    for (NameValuePair nvp : params) {
        if (!map.containsKey(nvp.getName())) {
            map.put(nvp.getName(), new ArrayList<>());
        }
        map.get(nvp.getName()).add(nvp.getValue());
    }
    return map;
}