Example usage for java.nio.charset CoderResult isError

List of usage examples for java.nio.charset CoderResult isError

Introduction

In this page you can find the example usage for java.nio.charset CoderResult isError.

Prototype

public boolean isError() 

Source Link

Document

Tells whether or not this object describes an error condition.

Usage

From source file:com.microsoft.tfs.core.util.FileEncodingDetector.java

/**
 * Tests whether the given byte array looks like an ANSI text file with the
 * default text encoding, i.e. can be decoded with the current ANSI
 * character set. In multi-byte character sets (like Japanese, for example)
 * the entire byte array might not be converted entirely, because at the end
 * of array it might contain a broken multi-byte character. We still accept
 * this kind of files as ANSI ones if the not converted reminder of the
 * array is short enough.//from w ww .  j  a v a 2  s  .c om
 *
 * @param bytes
 *        the bytes to check for ANSI-ness (must not be <code>null</code>)
 * @param limit
 *        the maximum number of bytes to read.
 * @return true if the given bytes look like part of an ANSI text file,
 *         false if they do not (because they contain control characters or
 *         other patterns).
 */
protected static boolean looksLikeANSI(final byte[] bytes, final int limit) {
    final Charset charSet = CodePageMapping.getCharset(FileEncoding.getDefaultTextEncoding().getCodePage());

    final ByteBuffer byteBuffer = ByteBuffer.wrap(bytes, 0, limit);
    final CharBuffer charBuffer = CharBuffer.allocate(limit);

    final CharsetDecoder decoder = charSet.newDecoder();
    decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    decoder.onMalformedInput(CodingErrorAction.REPORT);

    final CoderResult rc = decoder.decode(byteBuffer, charBuffer, true);

    if (!rc.isError()) {
        return true;
    } else {
        return byteBuffer.position() > limit - 5;
    }
}

From source file:co.cask.cdap.logging.gateway.handlers.ChunkedLogReaderCallback.java

private void encodeSend(CharBuffer inBuffer, boolean endOfInput) throws IOException {
    while (true) {
        CoderResult coderResult = charsetEncoder.encode(inBuffer, chunkBuffer, endOfInput);
        if (coderResult.isOverflow()) {
            // if reached buffer capacity then flush chunk
            chunkBuffer.flip();//from  w  w w  .ja  v  a 2s  .  c om
            chunkResponder.sendChunk(ChannelBuffers.copiedBuffer(chunkBuffer));
            chunkBuffer.clear();
        } else if (coderResult.isError()) {
            // skip characters causing error, and retry
            inBuffer.position(inBuffer.position() + coderResult.length());
        } else {
            // log line was completely written
            break;
        }
    }
}

From source file:org.colombbus.tangara.io.ScriptReader.java

/**
  * Try to decode a byte buffer with a charset
  */*from  ww  w . java  2 s.  co m*/
  * @param content
  *            the bute buffer
  * @param cs
  *            the charset
  * @return <code>null</code> if the charset is not supported, or the decoded
  *         string
  */
 private static String tryDecodeBufferWithCharset(ByteBuffer content, Charset cs) {
     CharBuffer buffer = CharBuffer.allocate(content.capacity() * 2);
     CharsetDecoder decoder = createDecoder(cs);
     content.rewind();
     CoderResult coderRes = decoder.decode(content, buffer, true);
     if (coderRes.isError() == false) {
         buffer.rewind();
         return buffer.toString().trim();
     }
     return null;
 }