use of java.nio.charset.CharsetDecoder in project grails-core by grails.
the class StreamByteBuffer method readAsString.
public String readAsString(Charset charset) throws CharacterCodingException {
int unreadSize = totalBytesUnread();
if (unreadSize > 0) {
CharsetDecoder decoder = charset.newDecoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
CharBuffer charbuffer = CharBuffer.allocate(unreadSize);
ByteBuffer buf = null;
while (prepareRead() != -1) {
buf = currentReadChunk.readToNioBuffer();
boolean endOfInput = (prepareRead() == -1);
CoderResult result = decoder.decode(buf, charbuffer, endOfInput);
if (endOfInput) {
if (!result.isUnderflow()) {
result.throwException();
}
}
}
CoderResult result = decoder.flush(charbuffer);
if (buf.hasRemaining()) {
throw new IllegalStateException("There's a bug here, buffer wasn't read fully.");
}
if (!result.isUnderflow()) {
result.throwException();
}
charbuffer.flip();
String str;
if (charbuffer.hasArray()) {
int len = charbuffer.remaining();
char[] ch = charbuffer.array();
if (len != ch.length) {
ch = (char[]) GrailsArrayUtils.subarray(ch, 0, len);
}
str = StringCharArrayAccessor.createString(ch);
} else {
str = charbuffer.toString();
}
return str;
}
return null;
}
use of java.nio.charset.CharsetDecoder in project jdk8u_jdk by JetBrains.
the class NIOJISAutoDetectTest method detectingCharset.
private static String detectingCharset(byte[] bytes) throws Exception {
//----------------------------------------------------------------
// Test special public methods of CharsetDecoder while we're here
//----------------------------------------------------------------
CharsetDecoder cd = Charset.forName("JISAutodetect").newDecoder();
check(cd.isAutoDetecting(), "isAutodecting()");
check(!cd.isCharsetDetected(), "isCharsetDetected");
cd.decode(ByteBuffer.wrap(new byte[] { (byte) 'A' }));
check(!cd.isCharsetDetected(), "isCharsetDetected");
try {
cd.detectedCharset();
fail("no IllegalStateException");
} catch (IllegalStateException e) {
}
cd.decode(ByteBuffer.wrap(bytes));
check(cd.isCharsetDetected(), "isCharsetDetected");
Charset cs = cd.detectedCharset();
check(cs != null, "cs != null");
check(!cs.newDecoder().isAutoDetecting(), "isAutodetecting()");
return cs.name();
}
use of java.nio.charset.CharsetDecoder in project voltdb by VoltDB.
the class CharsetUtil method decoder.
/**
* Returns a cached thread-local {@link CharsetDecoder} for the specified {@link Charset}.
*
* @param charset The specified charset
* @return The decoder for the specified <code>charset</code>
*/
public static CharsetDecoder decoder(Charset charset) {
checkNotNull(charset, "charset");
Map<Charset, CharsetDecoder> map = InternalThreadLocalMap.get().charsetDecoderCache();
CharsetDecoder d = map.get(charset);
if (d != null) {
d.reset().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
return d;
}
d = decoder(charset, CodingErrorAction.REPLACE, CodingErrorAction.REPLACE);
map.put(charset, d);
return d;
}
use of java.nio.charset.CharsetDecoder in project voltdb by VoltDB.
the class CharsetUtil method decoder.
/**
* Returns a new {@link CharsetDecoder} for the {@link Charset} with specified error actions.
*
* @param charset The specified charset
* @param malformedInputAction The decoder's action for malformed-input errors
* @param unmappableCharacterAction The decoder's action for unmappable-character errors
* @return The decoder for the specified <code>charset</code>
*/
public static CharsetDecoder decoder(Charset charset, CodingErrorAction malformedInputAction, CodingErrorAction unmappableCharacterAction) {
checkNotNull(charset, "charset");
CharsetDecoder d = charset.newDecoder();
d.onMalformedInput(malformedInputAction).onUnmappableCharacter(unmappableCharacterAction);
return d;
}
use of java.nio.charset.CharsetDecoder in project voltdb by VoltDB.
the class ByteBufUtil method decodeString.
static String decodeString(ByteBuf src, int readerIndex, int len, Charset charset) {
if (len == 0) {
return StringUtil.EMPTY_STRING;
}
final CharsetDecoder decoder = CharsetUtil.decoder(charset);
final int maxLength = (int) ((double) len * decoder.maxCharsPerByte());
CharBuffer dst = CHAR_BUFFERS.get();
if (dst.length() < maxLength) {
dst = CharBuffer.allocate(maxLength);
if (maxLength <= MAX_CHAR_BUFFER_SIZE) {
CHAR_BUFFERS.set(dst);
}
} else {
dst.clear();
}
if (src.nioBufferCount() == 1) {
// Use internalNioBuffer(...) to reduce object creation.
decodeString(decoder, src.internalNioBuffer(readerIndex, len), dst);
} else {
// We use a heap buffer as CharsetDecoder is most likely able to use a fast-path if src and dst buffers
// are both backed by a byte array.
ByteBuf buffer = src.alloc().heapBuffer(len);
try {
buffer.writeBytes(src, readerIndex, len);
// Use internalNioBuffer(...) to reduce object creation.
decodeString(decoder, buffer.internalNioBuffer(0, len), dst);
} finally {
// Release the temporary buffer again.
buffer.release();
}
}
return dst.flip().toString();
}
Aggregations