Search in sources :

Example 31 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project Bytecoder by mirkosertic.

the class ZipCoder method getBytes.

byte[] getBytes(String s) {
    CharsetEncoder ce = encoder().reset();
    char[] ca = s.toCharArray();
    int len = (int) (ca.length * ce.maxBytesPerChar());
    byte[] ba = new byte[len];
    if (len == 0)
        return ba;
    // CodingErrorAction.REPLACE mode.
    if (isUTF8 && ce instanceof ArrayEncoder) {
        int blen = ((ArrayEncoder) ce).encode(ca, 0, ca.length, ba);
        if (// malformed
        blen == -1)
            throw new IllegalArgumentException("MALFORMED");
        return Arrays.copyOf(ba, blen);
    }
    ByteBuffer bb = ByteBuffer.wrap(ba);
    CharBuffer cb = CharBuffer.wrap(ca);
    CoderResult cr = ce.encode(cb, bb, true);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    cr = ce.flush(bb);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    if (// defensive copy?
    bb.position() == ba.length)
        return ba;
    else
        return Arrays.copyOf(ba, bb.position());
}
Also used : CharBuffer(java.nio.CharBuffer) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer) ArrayEncoder(sun.nio.cs.ArrayEncoder) CoderResult(java.nio.charset.CoderResult)

Example 32 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project jackrabbit-oak by apache.

the class UTF8Encoder method encodeAsByteArray.

/**
 * Like {@link String#getBytes(java.nio.charset.Charset)} (with "UTF-8"),
 * except that invalid character sequences (such as unpaired surrogates) are
 * reported as exceptions (see {@link CodingErrorAction#REPORT}, instead of
 * being silently replaced by a replacement character as it would happen
 * otherwise.
 *
 * @param input
 *            String to encode
 * @return String encoded using {@link StandardCharsets#UTF_8}
 * @throws IOException
 *             on encoding error
 */
public static byte[] encodeAsByteArray(String input) throws IOException {
    CharsetEncoder e = CSE.get();
    e.reset();
    return bytes(e.encode(CharBuffer.wrap(input.toCharArray())));
}
Also used : CharsetEncoder(java.nio.charset.CharsetEncoder)

Example 33 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project jackrabbit-oak by apache.

the class UTF8Encoder method canEncode.

/**
 * @see {@link CharsetEncoder#canEncode(CharSequence)
 */
public static boolean canEncode(CharSequence input) {
    CharsetEncoder e = CSE.get();
    e.reset();
    return e.canEncode(input);
}
Also used : CharsetEncoder(java.nio.charset.CharsetEncoder)

Example 34 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project jackrabbit-oak by apache.

the class FileIOUtilsTest method getRandomTestString.

private static String getRandomTestString() throws Exception {
    boolean valid = false;
    StringBuilder buffer = new StringBuilder();
    while (!valid) {
        int length = RANDOM.nextInt(40);
        for (int i = 0; i < length; i++) {
            buffer.append((char) (RANDOM.nextInt(Character.MAX_VALUE)));
        }
        String s = buffer.toString();
        CharsetEncoder encoder = Charset.forName(UTF_8.toString()).newEncoder();
        try {
            encoder.encode(CharBuffer.wrap(s));
            valid = true;
        } catch (CharacterCodingException e) {
            buffer = new StringBuilder();
        }
    }
    return buffer.toString();
}
Also used : CharacterCodingException(java.nio.charset.CharacterCodingException) CharsetEncoder(java.nio.charset.CharsetEncoder)

Example 35 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project jackrabbit-oak by apache.

the class CharsetEncodingUtils method encodeAsUTF8.

/**
 * Like {@link String#getBytes(java.nio.charset.Charset)} (with "UTF-8"),
 * except that encoding problems (like unpaired surrogates) are reported as
 * exceptions (see {@link CodingErrorAction#REPORT}, instead of being
 * silently replaces as it would happen otherwise.
 *
 * @param input
 *            String to encode
 * @return String encoded using {@link StandardCharsets#UTF_8}
 * @throws IOException
 *             on encoding error
 */
public static byte[] encodeAsUTF8(String input) throws IOException {
    CharsetEncoder e = CSE.get();
    e.reset();
    return bytes(e.encode(CharBuffer.wrap(input.toCharArray())));
}
Also used : CharsetEncoder(java.nio.charset.CharsetEncoder)

Aggregations

CharsetEncoder (java.nio.charset.CharsetEncoder)177 ByteBuffer (java.nio.ByteBuffer)87 Charset (java.nio.charset.Charset)50 CharBuffer (java.nio.CharBuffer)42 CharacterCodingException (java.nio.charset.CharacterCodingException)31 CoderResult (java.nio.charset.CoderResult)31 OutputStreamWriter (java.io.OutputStreamWriter)21 IOException (java.io.IOException)15 BufferedWriter (java.io.BufferedWriter)13 OutputStream (java.io.OutputStream)10 FileOutputStream (java.io.FileOutputStream)7 InputStream (java.io.InputStream)7 IStatus (org.eclipse.core.runtime.IStatus)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 UnmappableCharacterException (java.nio.charset.UnmappableCharacterException)6 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)6 CoreException (org.eclipse.core.runtime.CoreException)6 Status (org.eclipse.core.runtime.Status)6 ArrayEncoder (sun.nio.cs.ArrayEncoder)6 Writer (java.io.Writer)5