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);
}
}
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;
}
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;
}
}
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);
}
}
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();
}
Aggregations