Search in sources :

Example 36 with CharBuffer

use of java.nio.CharBuffer in project cassandra by apache.

the class TypeValidationTest method testValidUtf8.

@Test
public void testValidUtf8() throws UnsupportedEncodingException {
    assert Character.MAX_CODE_POINT == 0x0010ffff;
    CharBuffer cb = CharBuffer.allocate(2837314);
    // let's test all of the unicode space.
    for (int i = 0; i < Character.MAX_CODE_POINT; i++) {
        // valid byte sequences (gives us '?' instead), so there is no point testing them.
        if (i >= 55296 && i <= 57343)
            continue;
        char[] ch = Character.toChars(i);
        for (char c : ch) cb.append(c);
    }
    String s = new String(cb.array());
    byte[] arr = s.getBytes("UTF8");
    ByteBuffer buf = ByteBuffer.wrap(arr);
    UTF8Type.instance.validate(buf);
    // some you might not expect.
    UTF8Type.instance.validate(ByteBuffer.wrap(new byte[] {}));
    // valid Utf8, unspecified in modified utf8.
    UTF8Type.instance.validate(ByteBuffer.wrap(new byte[] { 0 }));
    // modified utf8 null.
    UTF8Type.instance.validate(ByteBuffer.wrap(new byte[] { 99, (byte) 0xc0, (byte) 0x80, 112 }));
    // edges, for my sanity.
    UTF8Type.instance.validate(ByteBuffer.wrap(new byte[] { (byte) 0xc2, (byte) 0x81 }));
    UTF8Type.instance.validate(ByteBuffer.wrap(new byte[] { (byte) 0xe0, (byte) 0xa0, (byte) 0x81 }));
    UTF8Type.instance.validate(ByteBuffer.wrap(new byte[] { (byte) 0xf0, (byte) 0x90, (byte) 0x81, (byte) 0x81 }));
}
Also used : CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 37 with CharBuffer

use of java.nio.CharBuffer in project platform_frameworks_base by android.

the class TimeFormatter method formatInternal.

/**
     * Format the specified {@code wallTime} using {@code pattern}. The output is written to
     * {@link #outputBuilder}.
     */
private void formatInternal(String pattern, ZoneInfo.WallTime wallTime, ZoneInfo zoneInfo) {
    CharBuffer formatBuffer = CharBuffer.wrap(pattern);
    while (formatBuffer.remaining() > 0) {
        boolean outputCurrentChar = true;
        char currentChar = formatBuffer.get(formatBuffer.position());
        if (currentChar == '%') {
            outputCurrentChar = handleToken(formatBuffer, wallTime, zoneInfo);
        }
        if (outputCurrentChar) {
            outputBuilder.append(formatBuffer.get(formatBuffer.position()));
        }
        formatBuffer.position(formatBuffer.position() + 1);
    }
}
Also used : CharBuffer(java.nio.CharBuffer)

Example 38 with CharBuffer

use of java.nio.CharBuffer in project cassandra by apache.

the class CBUtil method decodeString.

// Taken from Netty's ChannelBuffers.decodeString(). We need to use our own decoder to properly handle invalid
// UTF-8 sequences.  See CASSANDRA-8101 for more details.  This can be removed once https://github.com/netty/netty/pull/2999
// is resolved in a release used by Cassandra.
private static String decodeString(ByteBuffer src) throws CharacterCodingException {
    // the decoder needs to be reset every time we use it, hence the copy per thread
    CharsetDecoder theDecoder = TL_UTF8_DECODER.get();
    theDecoder.reset();
    CharBuffer dst = TL_CHAR_BUFFER.get();
    int capacity = (int) ((double) src.remaining() * theDecoder.maxCharsPerByte());
    if (dst == null) {
        capacity = Math.max(capacity, 4096);
        dst = CharBuffer.allocate(capacity);
        TL_CHAR_BUFFER.set(dst);
    } else {
        dst.clear();
        if (dst.capacity() < capacity) {
            dst = CharBuffer.allocate(capacity);
            TL_CHAR_BUFFER.set(dst);
        }
    }
    CoderResult cr = theDecoder.decode(src, dst, true);
    if (!cr.isUnderflow())
        cr.throwException();
    return dst.flip().toString();
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) CoderResult(java.nio.charset.CoderResult)

Example 39 with CharBuffer

use of java.nio.CharBuffer in project carat by amplab.

the class FastXmlSerializer method flush.

public void flush() throws IOException {
    //Log.i("PackageManager", "flush mPos=" + mPos);
    if (mPos > 0) {
        if (mOutputStream != null) {
            CharBuffer charBuffer = CharBuffer.wrap(mText, 0, mPos);
            CoderResult result = mCharset.encode(charBuffer, mBytes, true);
            while (true) {
                if (result.isError()) {
                    throw new IOException(result.toString());
                } else if (result.isOverflow()) {
                    flushBytes();
                    result = mCharset.encode(charBuffer, mBytes, true);
                    continue;
                }
                break;
            }
            flushBytes();
            mOutputStream.flush();
        } else {
            mWriter.write(mText, 0, mPos);
            mWriter.flush();
        }
        mPos = 0;
    }
}
Also used : CharBuffer(java.nio.CharBuffer) IOException(java.io.IOException) CoderResult(java.nio.charset.CoderResult)

Example 40 with CharBuffer

use of java.nio.CharBuffer in project platform_frameworks_base by android.

the class FastXmlSerializer method flush.

public void flush() throws IOException {
    //Log.i("PackageManager", "flush mPos=" + mPos);
    if (mPos > 0) {
        if (mOutputStream != null) {
            CharBuffer charBuffer = CharBuffer.wrap(mText, 0, mPos);
            CoderResult result = mCharset.encode(charBuffer, mBytes, true);
            while (true) {
                if (result.isError()) {
                    throw new IOException(result.toString());
                } else if (result.isOverflow()) {
                    flushBytes();
                    result = mCharset.encode(charBuffer, mBytes, true);
                    continue;
                }
                break;
            }
            flushBytes();
            mOutputStream.flush();
        } else {
            mWriter.write(mText, 0, mPos);
            mWriter.flush();
        }
        mPos = 0;
    }
}
Also used : CharBuffer(java.nio.CharBuffer) IOException(java.io.IOException) CoderResult(java.nio.charset.CoderResult)

Aggregations

CharBuffer (java.nio.CharBuffer)401 ByteBuffer (java.nio.ByteBuffer)152 CoderResult (java.nio.charset.CoderResult)83 CharsetDecoder (java.nio.charset.CharsetDecoder)47 IOException (java.io.IOException)45 Charset (java.nio.charset.Charset)33 Test (org.junit.Test)23 CharacterCodingException (java.nio.charset.CharacterCodingException)15 CharsetEncoder (java.nio.charset.CharsetEncoder)15 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 InputStream (java.io.InputStream)6