Search in sources :

Example 76 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project voltdb by VoltDB.

the class ByteBufUtil method decodeString.

private static void decodeString(CharsetDecoder decoder, ByteBuffer src, CharBuffer dst) {
    try {
        CoderResult cr = decoder.decode(src, dst, true);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
        cr = decoder.flush(dst);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
    } catch (CharacterCodingException x) {
        throw new IllegalStateException(x);
    }
}
Also used : CharacterCodingException(java.nio.charset.CharacterCodingException) CoderResult(java.nio.charset.CoderResult)

Example 77 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project drill by apache.

the class CharSequenceWrapper method decodeUT8.

/**
   * Decode the buffer using the CharsetDecoder.
   * @param byteBuf
   * @return false if failed because the charbuffer was not big enough
   * @throws RuntimeException if it fails for encoding errors
   */
private boolean decodeUT8(ByteBuffer byteBuf) {
    // We give it all of the input data in call.
    boolean endOfInput = true;
    decoder.reset();
    charBuffer.rewind();
    // Convert utf-8 bytes to sequence of chars
    CoderResult result = decoder.decode(byteBuf, charBuffer, endOfInput);
    if (result.isOverflow()) {
        // Not enough space in the charBuffer.
        return false;
    } else if (result.isError()) {
        // Any other error
        try {
            result.throwException();
        } catch (CharacterCodingException e) {
            throw new RuntimeException(e);
        }
    }
    return true;
}
Also used : CharacterCodingException(java.nio.charset.CharacterCodingException) CoderResult(java.nio.charset.CoderResult)

Example 78 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project drill by apache.

the class Text method find.

/**
   * Finds any occurence of <code>what</code> in the backing buffer, starting as position <code>start</code>. The
   * starting position is measured in bytes and the return value is in terms of byte position in the buffer. The backing
   * buffer is not converted to a string for this operation.
   *
   * @return byte position of the first occurence of the search string in the UTF-8 buffer or -1 if not found
   */
public int find(String what, int start) {
    try {
        ByteBuffer src = ByteBuffer.wrap(this.bytes, 0, this.length);
        ByteBuffer tgt = encode(what);
        byte b = tgt.get();
        src.position(start);
        while (src.hasRemaining()) {
            if (b == src.get()) {
                // matching first byte
                // save position in loop
                src.mark();
                // save position in target
                tgt.mark();
                boolean found = true;
                int pos = src.position() - 1;
                while (tgt.hasRemaining()) {
                    if (!src.hasRemaining()) {
                        // src expired first
                        tgt.reset();
                        src.reset();
                        found = false;
                        break;
                    }
                    if (!(tgt.get() == src.get())) {
                        tgt.reset();
                        src.reset();
                        found = false;
                        // no match
                        break;
                    }
                }
                if (found) {
                    return pos;
                }
            }
        }
        // not found
        return -1;
    } catch (CharacterCodingException e) {
        // can't get here
        e.printStackTrace();
        return -1;
    }
}
Also used : CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer)

Example 79 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project drill by apache.

the class Text method set.

/**
   * Set to contain the contents of a string.
   */
public void set(String string) {
    try {
        ByteBuffer bb = encode(string, true);
        bytes = bb.array();
        length = bb.limit();
    } catch (CharacterCodingException e) {
        throw new RuntimeException("Should not have happened ", e);
    }
}
Also used : CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer)

Example 80 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project ignite by apache.

the class VisorTaskUtils method decode.

/**
     * Decode file charset.
     *
     * @param f File to process.
     * @return File charset.
     * @throws IOException in case of error.
     */
public static Charset decode(File f) throws IOException {
    SortedMap<String, Charset> charsets = Charset.availableCharsets();
    String[] firstCharsets = { Charset.defaultCharset().name(), "US-ASCII", "UTF-8", "UTF-16BE", "UTF-16LE" };
    Collection<Charset> orderedCharsets = U.newLinkedHashSet(charsets.size());
    for (String c : firstCharsets) if (charsets.containsKey(c))
        orderedCharsets.add(charsets.get(c));
    orderedCharsets.addAll(charsets.values());
    try (RandomAccessFile raf = new RandomAccessFile(f, "r")) {
        FileChannel ch = raf.getChannel();
        ByteBuffer buf = ByteBuffer.allocate(DFLT_BUFFER_SIZE);
        ch.read(buf);
        buf.flip();
        for (Charset charset : orderedCharsets) {
            CharsetDecoder decoder = charset.newDecoder();
            decoder.reset();
            try {
                decoder.decode(buf);
                return charset;
            } catch (CharacterCodingException ignored) {
            }
        }
    }
    return Charset.defaultCharset();
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) Charset(java.nio.charset.Charset) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer)

Aggregations

CharacterCodingException (java.nio.charset.CharacterCodingException)80 ByteBuffer (java.nio.ByteBuffer)39 CoderResult (java.nio.charset.CoderResult)13 CharBuffer (java.nio.CharBuffer)12 IOException (java.io.IOException)10 CharsetEncoder (java.nio.charset.CharsetEncoder)10 CharsetDecoder (java.nio.charset.CharsetDecoder)9 HumanReadableException (com.facebook.buck.util.HumanReadableException)4 Charset (java.nio.charset.Charset)4 Date (java.util.Date)4 Path (java.nio.file.Path)3 ParseException (java.text.ParseException)3 Test (org.junit.Test)3 Portfolio (com.datastax.demo.portfolio.Portfolio)2 Point (java.awt.Point)2 OutputStream (java.io.OutputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)2 MalformedInputException (java.nio.charset.MalformedInputException)2 UnmappableCharacterException (java.nio.charset.UnmappableCharacterException)2