Search in sources :

Example 26 with CharBuffer

use of java.nio.CharBuffer in project hs4j by killme2008.

the class AbstractIoBuffer method getPrefixedString.

/**
     * Reads a string which has a length field before the actual encoded string,
     * using the specified <code>decoder</code> and returns it.
     * 
     * @param prefixLength
     *            the length of the length field (1, 2, or 4)
     * @param decoder
     *            the decoder to use for decoding the string
     * @return the prefixed string
     * @throws CharacterCodingException
     *             when decoding fails
     * @throws BufferUnderflowException
     *             when there is not enough data available
     */
@Override
public String getPrefixedString(int prefixLength, CharsetDecoder decoder) throws CharacterCodingException {
    if (!prefixedDataAvailable(prefixLength)) {
        throw new BufferUnderflowException();
    }
    int fieldSize = 0;
    switch(prefixLength) {
        case 1:
            fieldSize = getUnsigned();
            break;
        case 2:
            fieldSize = getUnsignedShort();
            break;
        case 4:
            fieldSize = getInt();
            break;
    }
    if (fieldSize == 0) {
        return "";
    }
    boolean utf16 = decoder.charset().name().startsWith("UTF-16");
    if (utf16 && (fieldSize & 1) != 0) {
        throw new BufferDataException("fieldSize is not even for a UTF-16 string.");
    }
    int oldLimit = limit();
    int end = position() + fieldSize;
    if (oldLimit < end) {
        throw new BufferUnderflowException();
    }
    limit(end);
    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;
        }
        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 27 with CharBuffer

use of java.nio.CharBuffer in project rest.li by linkedin.

the class BufferChain method bufferToUtf8CString.

private String bufferToUtf8CString(int numBytes, ArrayList<ByteBuffer> bufferList) throws IOException {
    String result;
    if (numBytes == 0) {
        result = "";
    } else if (bufferList == null) {
        _decoder.reset();
        // char should be smaller than # of bytes in buffer.
        CharBuffer charBuffer = CharBuffer.allocate(numBytes);
        int limit = _currentBuffer.limit();
        _currentBuffer.limit(_currentBuffer.position() + numBytes);
        checkCoderResult(_decoder.decode(_currentBuffer, charBuffer, true));
        _currentBuffer.limit(limit);
        _decoder.flush(charBuffer);
        charBuffer.flip();
        result = charBuffer.toString();
    } else {
        // char should be smaller than # of bytes in buffer.
        char[] charBuffer = new char[numBytes];
        BufferChain chain = new BufferChain(_order, bufferList, _bufferSize);
        InputStream inputStream = chain.asInputStream();
        Reader reader = new InputStreamReader(inputStream, _charset);
        int offset = 0;
        int remaining = numBytes;
        int charactersRead;
        while ((charactersRead = reader.read(charBuffer, offset, remaining)) > 0) {
            offset += charactersRead;
            remaining -= charactersRead;
        }
        result = new String(charBuffer, 0, offset);
    }
    advanceBufferIfCurrentBufferHasNoRemaining();
    // terminal zero byte
    _currentBuffer.get();
    return result;
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) CharBuffer(java.nio.CharBuffer) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ByteString(com.linkedin.data.ByteString)

Example 28 with CharBuffer

use of java.nio.CharBuffer in project jphp by jphp-compiler.

the class CharArrayMemory method put.

public void put(int index, String s) {
    int len = s.length();
    int sLen = buffer.limit();
    if (index < 0)
        return;
    char ch = len == 0 ? '\0' : s.charAt(0);
    if (index < sLen)
        buffer.put(index, ch);
    else {
        int cnt = index - sLen;
        CharBuffer tmp = CharBuffer.allocate(sLen + cnt + 1);
        tmp.put(buffer.array());
        buffer = tmp;
        for (int i = 0; i < cnt; i++) {
            buffer.append('\32');
        }
        buffer.append(ch);
    }
}
Also used : CharBuffer(java.nio.CharBuffer)

Example 29 with CharBuffer

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

the class AdversarialReader method read.

@Override
public int read(CharBuffer target) throws IOException {
    if (adversary.injectFailureOrMischief(IOException.class, BufferOverflowException.class, IndexOutOfBoundsException.class)) {
        CharBuffer dup = target.duplicate();
        dup.limit(Math.max(target.limit() / 2, 1));
        return reader.read(dup);
    }
    return reader.read(target);
}
Also used : CharBuffer(java.nio.CharBuffer)

Example 30 with CharBuffer

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

the class AsciiStringCharacterTest method caseInsensitiveHasherCharBuffer.

@Test
public void caseInsensitiveHasherCharBuffer() {
    String s1 = new String("TRANSFER-ENCODING");
    char[] array = new char[128];
    final int offset = 100;
    for (int i = 0; i < s1.length(); ++i) {
        array[offset + i] = s1.charAt(i);
    }
    CharBuffer buffer = CharBuffer.wrap(array, offset, s1.length());
    assertEquals(AsciiString.hashCode(s1), AsciiString.hashCode(buffer));
}
Also used : CharBuffer(java.nio.CharBuffer) Test(org.junit.Test)

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