Search in sources :

Example 21 with CharBuffer

use of java.nio.CharBuffer in project jersey by jersey.

the class UriComponent method decodeOctets.

/**
     * Decodes octets to characters using the UTF-8 decoding and appends
     * the characters to a StringBuffer.
     *
     * @return the index to the next unchecked character in the string to decode
     */
private static int decodeOctets(final int i, final ByteBuffer bb, final StringBuilder sb) {
    // If there is only one octet and is an ASCII character
    if (bb.limit() == 1 && (bb.get(0) & 0xFF) < 0x80) {
        // Octet can be appended directly
        sb.append((char) bb.get(0));
        return i + 2;
    } else {
        //
        final CharBuffer cb = UTF_8_CHARSET.decode(bb);
        sb.append(cb.toString());
        return i + bb.limit() * 3 - 1;
    }
}
Also used : CharBuffer(java.nio.CharBuffer)

Example 22 with CharBuffer

use of java.nio.CharBuffer in project jersey by jersey.

the class UriComponent method appendUTF8EncodedCharacter.

private static void appendUTF8EncodedCharacter(final StringBuilder sb, final int codePoint) {
    final CharBuffer chars = CharBuffer.wrap(Character.toChars(codePoint));
    final ByteBuffer bytes = UTF_8_CHARSET.encode(chars);
    while (bytes.hasRemaining()) {
        appendPercentEncodedOctet(sb, bytes.get() & 0xFF);
    }
}
Also used : CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 23 with CharBuffer

use of java.nio.CharBuffer 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 24 with CharBuffer

use of java.nio.CharBuffer 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 25 with CharBuffer

use of java.nio.CharBuffer 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)

Aggregations

CharBuffer (java.nio.CharBuffer)387 ByteBuffer (java.nio.ByteBuffer)143 CoderResult (java.nio.charset.CoderResult)81 CharsetDecoder (java.nio.charset.CharsetDecoder)45 IOException (java.io.IOException)41 Charset (java.nio.charset.Charset)28 Test (org.junit.Test)23 CharacterCodingException (java.nio.charset.CharacterCodingException)12 CharsetEncoder (java.nio.charset.CharsetEncoder)12 FileInputStream (java.io.FileInputStream)11 IntBuffer (java.nio.IntBuffer)10 Reader (java.io.Reader)9 BufferOverflowException (java.nio.BufferOverflowException)9 DoubleBuffer (java.nio.DoubleBuffer)9 FloatBuffer (java.nio.FloatBuffer)9 LongBuffer (java.nio.LongBuffer)9 ShortBuffer (java.nio.ShortBuffer)9 BufferUnderflowException (java.nio.BufferUnderflowException)7 ValueWrapper (org.apache.geode.internal.memcached.ValueWrapper)7 ReadableByteChannel (java.nio.channels.ReadableByteChannel)6