Search in sources :

Example 91 with CoderResult

use of java.nio.charset.CoderResult in project hs4j by killme2008.

the class AbstractIoBuffer method getString.

/**
 * {@inheritDoc}
 */
@Override
public String getString(int fieldSize, CharsetDecoder decoder) throws CharacterCodingException {
    checkFieldSize(fieldSize);
    if (fieldSize == 0) {
        return "";
    }
    if (!hasRemaining()) {
        return "";
    }
    boolean utf16 = decoder.charset().name().startsWith("UTF-16");
    if (utf16 && (fieldSize & 1) != 0) {
        throw new IllegalArgumentException("fieldSize is not even.");
    }
    int oldPos = position();
    int oldLimit = limit();
    int end = oldPos + fieldSize;
    if (oldLimit < end) {
        throw new BufferUnderflowException();
    }
    int i;
    if (!utf16) {
        for (i = oldPos; i < end; i++) {
            if (get(i) == 0) {
                break;
            }
        }
        if (i == end) {
            limit(end);
        } else {
            limit(i);
        }
    } else {
        for (i = oldPos; i < end; i += 2) {
            if (get(i) == 0 && get(i + 1) == 0) {
                break;
            }
        }
        if (i == end) {
            limit(end);
        } else {
            limit(i);
        }
    }
    if (!hasRemaining()) {
        limit(oldLimit);
        position(end);
        return "";
    }
    decoder.reset();
    int expectedLength = (int) (remaining() * decoder.averageCharsPerByte()) + 1;
    CharBuffer out = CharBuffer.allocate(expectedLength);
    for (; ; ) {
        CoderResult cr;
        if (hasRemaining()) {
            cr = decoder.decode(buf(), out, true);
        } else {
            cr = decoder.flush(out);
        }
        if (cr.isUnderflow()) {
            break;
        }
        if (cr.isOverflow()) {
            CharBuffer o = CharBuffer.allocate(out.capacity() + expectedLength);
            out.flip();
            o.put(out);
            out = o;
            continue;
        }
        if (cr.isError()) {
            // Revert the buffer back to the previous state.
            limit(oldLimit);
            position(oldPos);
            cr.throwException();
        }
    }
    limit(oldLimit);
    position(end);
    return out.flip().toString();
}
Also used : CharBuffer(java.nio.CharBuffer) BufferUnderflowException(java.nio.BufferUnderflowException) CoderResult(java.nio.charset.CoderResult)

Example 92 with CoderResult

use of java.nio.charset.CoderResult in project hs4j by killme2008.

the class AbstractIoBuffer method putString.

/**
 * {@inheritDoc}
 */
@Override
public IoBuffer putString(CharSequence val, int fieldSize, CharsetEncoder encoder) throws CharacterCodingException {
    checkFieldSize(fieldSize);
    if (fieldSize == 0) {
        return this;
    }
    autoExpand(fieldSize);
    boolean utf16 = encoder.charset().name().startsWith("UTF-16");
    if (utf16 && (fieldSize & 1) != 0) {
        throw new IllegalArgumentException("fieldSize is not even.");
    }
    int oldLimit = limit();
    int end = position() + fieldSize;
    if (oldLimit < end) {
        throw new BufferOverflowException();
    }
    if (val.length() == 0) {
        if (!utf16) {
            put((byte) 0x00);
        } else {
            put((byte) 0x00);
            put((byte) 0x00);
        }
        position(end);
        return this;
    }
    CharBuffer in = CharBuffer.wrap(val);
    limit(end);
    encoder.reset();
    for (; ; ) {
        CoderResult cr;
        if (in.hasRemaining()) {
            cr = encoder.encode(in, buf(), true);
        } else {
            cr = encoder.flush(buf());
        }
        if (cr.isUnderflow() || cr.isOverflow()) {
            break;
        }
        cr.throwException();
    }
    limit(oldLimit);
    if (position() < end) {
        if (!utf16) {
            put((byte) 0x00);
        } else {
            put((byte) 0x00);
            put((byte) 0x00);
        }
    }
    position(end);
    return this;
}
Also used : CharBuffer(java.nio.CharBuffer) BufferOverflowException(java.nio.BufferOverflowException) CoderResult(java.nio.charset.CoderResult)

Example 93 with CoderResult

use of java.nio.charset.CoderResult in project hs4j by killme2008.

the class AbstractIoBuffer method putPrefixedString.

/**
 * {@inheritDoc}
 */
@Override
public IoBuffer putPrefixedString(CharSequence val, int prefixLength, int padding, byte padValue, CharsetEncoder encoder) throws CharacterCodingException {
    int maxLength;
    switch(prefixLength) {
        case 1:
            maxLength = 255;
            break;
        case 2:
            maxLength = 65535;
            break;
        case 4:
            maxLength = Integer.MAX_VALUE;
            break;
        default:
            throw new IllegalArgumentException("prefixLength: " + prefixLength);
    }
    if (val.length() > maxLength) {
        throw new IllegalArgumentException("The specified string is too long.");
    }
    if (val.length() == 0) {
        switch(prefixLength) {
            case 1:
                put((byte) 0);
                break;
            case 2:
                putShort((short) 0);
                break;
            case 4:
                putInt(0);
                break;
        }
        return this;
    }
    int padMask;
    switch(padding) {
        case 0:
        case 1:
            padMask = 0;
            break;
        case 2:
            padMask = 1;
            break;
        case 4:
            padMask = 3;
            break;
        default:
            throw new IllegalArgumentException("padding: " + padding);
    }
    CharBuffer in = CharBuffer.wrap(val);
    // make a room for the length field
    skip(prefixLength);
    int oldPos = position();
    encoder.reset();
    int expandedState = 0;
    for (; ; ) {
        CoderResult cr;
        if (in.hasRemaining()) {
            cr = encoder.encode(in, buf(), true);
        } else {
            cr = encoder.flush(buf());
        }
        if (position() - oldPos > maxLength) {
            throw new IllegalArgumentException("The specified string is too long.");
        }
        if (cr.isUnderflow()) {
            break;
        }
        if (cr.isOverflow()) {
            if (isAutoExpand()) {
                switch(expandedState) {
                    case 0:
                        autoExpand((int) Math.ceil(in.remaining() * encoder.averageBytesPerChar()));
                        expandedState++;
                        break;
                    case 1:
                        autoExpand((int) Math.ceil(in.remaining() * encoder.maxBytesPerChar()));
                        expandedState++;
                        break;
                    default:
                        throw new RuntimeException("Expanded by " + (int) Math.ceil(in.remaining() * encoder.maxBytesPerChar()) + " but that wasn't enough for '" + val + "'");
                }
                continue;
            }
        } else {
            expandedState = 0;
        }
        cr.throwException();
    }
    // Write the length field
    fill(padValue, padding - (position() - oldPos & padMask));
    int length = position() - oldPos;
    switch(prefixLength) {
        case 1:
            put(oldPos - 1, (byte) length);
            break;
        case 2:
            putShort(oldPos - 2, (short) length);
            break;
        case 4:
            putInt(oldPos - 4, length);
            break;
    }
    return this;
}
Also used : CharBuffer(java.nio.CharBuffer) CoderResult(java.nio.charset.CoderResult)

Example 94 with CoderResult

use of java.nio.charset.CoderResult in project fastjson by alibaba.

the class IOUtils method decode.

public static void decode(CharsetDecoder charsetDecoder, ByteBuffer byteBuf, CharBuffer charByte) {
    try {
        CoderResult cr = charsetDecoder.decode(byteBuf, charByte, true);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
        cr = charsetDecoder.flush(charByte);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
    } catch (CharacterCodingException x) {
        // so this shouldn't happen
        throw new JSONException("utf8 decode error, " + x.getMessage(), x);
    }
}
Also used : JSONException(com.alibaba.fastjson.JSONException) CharacterCodingException(java.nio.charset.CharacterCodingException) CoderResult(java.nio.charset.CoderResult)

Example 95 with CoderResult

use of java.nio.charset.CoderResult in project druid by druid-io.

the class StringInputRowParser method buildStringKeyMap.

private Map<String, Object> buildStringKeyMap(ByteBuffer input) {
    int payloadSize = input.remaining();
    if (chars == null || chars.remaining() < payloadSize) {
        chars = CharBuffer.allocate(payloadSize);
    }
    final CoderResult coderResult = charset.newDecoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE).decode(input, chars, true);
    Map<String, Object> theMap;
    if (coderResult.isUnderflow()) {
        chars.flip();
        try {
            theMap = parseString(chars.toString());
        } finally {
            chars.clear();
        }
    } else {
        throw new ParseException("Failed with CoderResult[%s]", coderResult);
    }
    return theMap;
}
Also used : ParseException(io.druid.java.util.common.parsers.ParseException) CoderResult(java.nio.charset.CoderResult)

Aggregations

CoderResult (java.nio.charset.CoderResult)188 CharBuffer (java.nio.CharBuffer)121 ByteBuffer (java.nio.ByteBuffer)70 CharsetDecoder (java.nio.charset.CharsetDecoder)40 IOException (java.io.IOException)33 CharacterCodingException (java.nio.charset.CharacterCodingException)30 CharsetEncoder (java.nio.charset.CharsetEncoder)25 Charset (java.nio.charset.Charset)15 ArrayDecoder (sun.nio.cs.ArrayDecoder)7 ArrayEncoder (sun.nio.cs.ArrayEncoder)7 JSONException (com.alibaba.fastjson.JSONException)3 BufferUnderflowException (java.nio.BufferUnderflowException)3 CloseReason (jakarta.websocket.CloseReason)2 UncheckedIOException (java.io.UncheckedIOException)2 BufferOverflowException (java.nio.BufferOverflowException)2 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)2 UnmappableCharacterException (java.nio.charset.UnmappableCharacterException)2 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)2 CloseReason (javax.websocket.CloseReason)2 InvalidConfigurationException (org.bukkit.configuration.InvalidConfigurationException)2