Example usage for java.nio.charset CoderResult isUnderflow

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

Introduction

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

Prototype

public boolean isUnderflow() 

Source Link

Document

Tells whether or not this object describes an underflow condition.

Usage

From source file:com.google.flatbuffers.Table.java

/**
 * Create a Java `String` from UTF-8 data stored inside the FlatBuffer.
 *
 * This allocates a new string and converts to wide chars upon each access,
 * which is not very efficient. Instead, each FlatBuffer string also comes with an
 * accessor based on __vector_as_bytebuffer below, which is much more efficient,
 * assuming your Java program can handle UTF-8 data directly.
 *
 * @param offset An `int` index into the Table's ByteBuffer.
 * @return Returns a `String` from the data stored inside the FlatBuffer at `offset`.
 *///from  ww w  .j a va2 s.c o  m
protected String __string(int offset) {
    CharsetDecoder decoder = UTF8_DECODER.get();
    decoder.reset();

    offset += bb.getInt(offset);
    ByteBuffer src = bb.duplicate().order(ByteOrder.LITTLE_ENDIAN);
    int length = src.getInt(offset);
    src.position(offset + SIZEOF_INT);
    src.limit(offset + SIZEOF_INT + length);

    int required = (int) ((float) length * decoder.maxCharsPerByte());
    CharBuffer dst = CHAR_BUFFER.get();
    if (dst == null || dst.capacity() < required) {
        dst = CharBuffer.allocate(required);
        CHAR_BUFFER.set(dst);
    }

    dst.clear();

    try {
        CoderResult cr = decoder.decode(src, dst, true);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
    } catch (CharacterCodingException x) {
        throw new Error(x);
    }

    return dst.flip().toString();
}

From source file:org.apache.tajo.util.StringUtils.java

public static char[] convertBytesToChars(byte[] src, Charset charset) {
    CharsetDecoder decoder = charset.newDecoder();
    char[] resultArray = new char[(int) (src.length * decoder.maxCharsPerByte())];

    if (src.length != 0) {
        ByteBuffer byteBuffer = ByteBuffer.wrap(src);
        CharBuffer charBuffer = CharBuffer.wrap(resultArray);

        decoder.onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
        decoder.reset();//from ww  w. j a  v a2s .  c o m

        CoderResult coderResult = decoder.decode(byteBuffer, charBuffer, true);
        if (coderResult.isUnderflow()) {
            coderResult = decoder.flush(charBuffer);

            if (coderResult.isUnderflow()) {
                if (resultArray.length != charBuffer.position()) {
                    resultArray = Arrays.copyOf(resultArray, charBuffer.position());
                }
            }
        }
    }

    return resultArray;
}

From source file:org.apache.tajo.util.StringUtils.java

public static byte[] convertCharsToBytes(char[] src, Charset charset) {
    CharsetEncoder encoder = charset.newEncoder();
    byte[] resultArray = new byte[(int) (src.length * encoder.maxBytesPerChar())];

    if (src.length != 0) {
        CharBuffer charBuffer = CharBuffer.wrap(src);
        ByteBuffer byteBuffer = ByteBuffer.wrap(resultArray);

        encoder.onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
        encoder.reset();//from ww  w .  j  av  a 2  s  .c o  m

        CoderResult coderResult = encoder.encode(charBuffer, byteBuffer, true);
        if (coderResult.isUnderflow()) {
            coderResult = encoder.flush(byteBuffer);

            if (coderResult.isUnderflow()) {
                if (resultArray.length != byteBuffer.position()) {
                    resultArray = Arrays.copyOf(resultArray, byteBuffer.position());
                }
            }
        }
    }

    return resultArray;
}

From source file:org.codehaus.groovy.grails.web.util.StreamByteBuffer.java

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;// w  ww .j a  v a  2s  .com
        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 = ArrayUtils.subarray(ch, 0, len);
            }
            str = StringCharArrayAccessor.createString(ch);
        } else {
            str = charbuffer.toString();
        }
        return str;
    }
    return null;
}

From source file:org.gradle.testkit.runner.internal.io.WriterOutputStream.java

/**
 * Decode the contents of the input ByteBuffer into a CharBuffer.
 *
 * @param endOfInput indicates end of input
 * @throws IOException if an I/O error occurs
 *//* w w  w .  ja v  a  2s.  c o  m*/
private void processInput(boolean endOfInput) throws IOException {
    // Prepare decoderIn for reading
    decoderIn.flip();
    CoderResult coderResult;
    while (true) {
        coderResult = decoder.decode(decoderIn, decoderOut, endOfInput);
        if (coderResult.isOverflow()) {
            flushOutput();
        } else if (coderResult.isUnderflow()) {
            break;
        } else {
            // The decoder is configured to replace malformed input and unmappable characters,
            // so we should not get here.
            throw new IOException("Unexpected coder result");
        }
    }
    // Discard the bytes that have been read
    decoderIn.compact();
}

From source file:org.openbel.framework.ws.utils.Converter.java

private static Byte encode(final CharsetEncoder encoder, final char c) {
    encoder.reset();/*from   w  ww  .  j a v  a  2 s  .  c  om*/
    if (!encoder.canEncode(c)) {
        return null;
    }
    encoder.reset();
    ByteBuffer buffer = ByteBuffer.allocate(1);
    CharBuffer charBuffer = CharBuffer.allocate(1).put(c);
    charBuffer.flip();
    CoderResult result = null;

    result = encoder.encode(charBuffer, buffer, false);
    if (!result.isUnderflow()) {
        return null;
    }
    result = encoder.encode(charBuffer, buffer, true);
    if (result.isMalformed() || result.isUnmappable()) {
        return null;
    }
    result = encoder.flush(buffer);
    if (!result.isUnderflow()) {
        return null;
    }
    buffer.flip();
    return buffer.get(0);
}