Search in sources :

Example 91 with CharBuffer

use of java.nio.CharBuffer in project android_frameworks_base by ResurrectionRemix.

the class GLLogWrapper method toByteBuffer.

private ByteBuffer toByteBuffer(int byteCount, Buffer input) {
    ByteBuffer result = null;
    boolean convertWholeBuffer = (byteCount < 0);
    if (input instanceof ByteBuffer) {
        ByteBuffer input2 = (ByteBuffer) input;
        int position = input2.position();
        if (convertWholeBuffer) {
            byteCount = input2.limit() - position;
        }
        result = ByteBuffer.allocate(byteCount).order(input2.order());
        for (int i = 0; i < byteCount; i++) {
            result.put(input2.get());
        }
        input2.position(position);
    } else if (input instanceof CharBuffer) {
        CharBuffer input2 = (CharBuffer) input;
        int position = input2.position();
        if (convertWholeBuffer) {
            byteCount = (input2.limit() - position) * 2;
        }
        result = ByteBuffer.allocate(byteCount).order(input2.order());
        CharBuffer result2 = result.asCharBuffer();
        for (int i = 0; i < byteCount / 2; i++) {
            result2.put(input2.get());
        }
        input2.position(position);
    } else if (input instanceof ShortBuffer) {
        ShortBuffer input2 = (ShortBuffer) input;
        int position = input2.position();
        if (convertWholeBuffer) {
            byteCount = (input2.limit() - position) * 2;
        }
        result = ByteBuffer.allocate(byteCount).order(input2.order());
        ShortBuffer result2 = result.asShortBuffer();
        for (int i = 0; i < byteCount / 2; i++) {
            result2.put(input2.get());
        }
        input2.position(position);
    } else if (input instanceof IntBuffer) {
        IntBuffer input2 = (IntBuffer) input;
        int position = input2.position();
        if (convertWholeBuffer) {
            byteCount = (input2.limit() - position) * 4;
        }
        result = ByteBuffer.allocate(byteCount).order(input2.order());
        IntBuffer result2 = result.asIntBuffer();
        for (int i = 0; i < byteCount / 4; i++) {
            result2.put(input2.get());
        }
        input2.position(position);
    } else if (input instanceof FloatBuffer) {
        FloatBuffer input2 = (FloatBuffer) input;
        int position = input2.position();
        if (convertWholeBuffer) {
            byteCount = (input2.limit() - position) * 4;
        }
        result = ByteBuffer.allocate(byteCount).order(input2.order());
        FloatBuffer result2 = result.asFloatBuffer();
        for (int i = 0; i < byteCount / 4; i++) {
            result2.put(input2.get());
        }
        input2.position(position);
    } else if (input instanceof DoubleBuffer) {
        DoubleBuffer input2 = (DoubleBuffer) input;
        int position = input2.position();
        if (convertWholeBuffer) {
            byteCount = (input2.limit() - position) * 8;
        }
        result = ByteBuffer.allocate(byteCount).order(input2.order());
        DoubleBuffer result2 = result.asDoubleBuffer();
        for (int i = 0; i < byteCount / 8; i++) {
            result2.put(input2.get());
        }
        input2.position(position);
    } else if (input instanceof LongBuffer) {
        LongBuffer input2 = (LongBuffer) input;
        int position = input2.position();
        if (convertWholeBuffer) {
            byteCount = (input2.limit() - position) * 8;
        }
        result = ByteBuffer.allocate(byteCount).order(input2.order());
        LongBuffer result2 = result.asLongBuffer();
        for (int i = 0; i < byteCount / 8; i++) {
            result2.put(input2.get());
        }
        input2.position(position);
    } else {
        throw new RuntimeException("Unimplemented Buffer subclass.");
    }
    result.rewind();
    // The OpenGL API will interpret the result in hardware byte order,
    // so we better do that as well:
    result.order(ByteOrder.nativeOrder());
    return result;
}
Also used : DoubleBuffer(java.nio.DoubleBuffer) LongBuffer(java.nio.LongBuffer) IntBuffer(java.nio.IntBuffer) CharBuffer(java.nio.CharBuffer) FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer) ShortBuffer(java.nio.ShortBuffer)

Example 92 with CharBuffer

use of java.nio.CharBuffer in project voltdb by VoltDB.

the class ReaderInputStream method grow.

/** Returns a new CharBuffer identical to buf, except twice the capacity. */
private static CharBuffer grow(CharBuffer buf) {
    char[] copy = Arrays.copyOf(buf.array(), buf.capacity() * 2);
    CharBuffer bigger = CharBuffer.wrap(copy);
    bigger.position(buf.position());
    bigger.limit(buf.limit());
    return bigger;
}
Also used : CharBuffer(java.nio.CharBuffer)

Example 93 with CharBuffer

use of java.nio.CharBuffer in project voltdb by VoltDB.

the class CharStreams method exhaust.

/**
   * Reads and discards data from the given {@code Readable} until the end of the stream is
   * reached. Returns the total number of chars read. Does not close the stream.
   *
   * @since 20.0
   */
@CanIgnoreReturnValue
public static long exhaust(Readable readable) throws IOException {
    long total = 0;
    long read;
    CharBuffer buf = createBuffer();
    while ((read = readable.read(buf)) != -1) {
        total += read;
        buf.clear();
    }
    return total;
}
Also used : CharBuffer(java.nio.CharBuffer) CanIgnoreReturnValue(com.google_voltpatches.errorprone.annotations.CanIgnoreReturnValue)

Example 94 with CharBuffer

use of java.nio.CharBuffer in project voltdb by VoltDB.

the class ByteBufUtil method decodeString.

static String decodeString(ByteBuf src, int readerIndex, int len, Charset charset) {
    if (len == 0) {
        return StringUtil.EMPTY_STRING;
    }
    final CharsetDecoder decoder = CharsetUtil.decoder(charset);
    final int maxLength = (int) ((double) len * decoder.maxCharsPerByte());
    CharBuffer dst = CHAR_BUFFERS.get();
    if (dst.length() < maxLength) {
        dst = CharBuffer.allocate(maxLength);
        if (maxLength <= MAX_CHAR_BUFFER_SIZE) {
            CHAR_BUFFERS.set(dst);
        }
    } else {
        dst.clear();
    }
    if (src.nioBufferCount() == 1) {
        // Use internalNioBuffer(...) to reduce object creation.
        decodeString(decoder, src.internalNioBuffer(readerIndex, len), dst);
    } else {
        // We use a heap buffer as CharsetDecoder is most likely able to use a fast-path if src and dst buffers
        // are both backed by a byte array.
        ByteBuf buffer = src.alloc().heapBuffer(len);
        try {
            buffer.writeBytes(src, readerIndex, len);
            // Use internalNioBuffer(...) to reduce object creation.
            decodeString(decoder, buffer.internalNioBuffer(0, len), dst);
        } finally {
            // Release the temporary buffer again.
            buffer.release();
        }
    }
    return dst.flip().toString();
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer)

Example 95 with CharBuffer

use of java.nio.CharBuffer in project voltdb by VoltDB.

the class Unpooled method copiedBuffer.

/**
     * Creates a new big-endian buffer whose content is a subregion of
     * the specified {@code string} encoded in the specified {@code charset}.
     * The new buffer's {@code readerIndex} and {@code writerIndex} are
     * {@code 0} and the length of the encoded string respectively.
     */
public static ByteBuf copiedBuffer(CharSequence string, int offset, int length, Charset charset) {
    if (string == null) {
        throw new NullPointerException("string");
    }
    if (length == 0) {
        return EMPTY_BUFFER;
    }
    if (string instanceof CharBuffer) {
        CharBuffer buf = (CharBuffer) string;
        if (buf.hasArray()) {
            return copiedBuffer(buf.array(), buf.arrayOffset() + buf.position() + offset, length, charset);
        }
        buf = buf.slice();
        buf.limit(length);
        buf.position(offset);
        return copiedBuffer(buf, charset);
    }
    return copiedBuffer(CharBuffer.wrap(string, offset, offset + length), charset);
}
Also used : CharBuffer(java.nio.CharBuffer)

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