Java ByteBuffer Set charstoUTF8Bytes(char[] srcBuf, int srcOffset, int srcLength, ByteBuffer dest, int destOffset)

Here you can find the source of charstoUTF8Bytes(char[] srcBuf, int srcOffset, int srcLength, ByteBuffer dest, int destOffset)

Description

Custom UTF-8 encoding.

License

Open Source License

Parameter

Parameter Description
srcBuf the source char[] to be encoded as UTF-8 byte sequence
srcOffset offset in the char[] from where the conversion to UTF-8 should begin
srcLength the number of chars to be encoded as UTF-8 bytes
dest the destination ByteBuffer
destOffset offset in the ByteBuffer starting where the encoded UTF-8 bytes should be copied

Return

the number of bytes encoded

Declaration

public static int charstoUTF8Bytes(char[] srcBuf, int srcOffset, int srcLength, ByteBuffer dest,
        int destOffset) 

Method Source Code

//package com.java2s;
/**//from www  .j  av  a  2  s. c  o m
 * Copyright 2007-2015, Kaazing Corporation. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import static java.lang.String.format;

import java.nio.ByteBuffer;

public class Main {
    private static final String MSG_INVALID_CODEPOINT = "Invalid UTF-16 codepoint %d";

    /**
     * Custom UTF-8 encoding. Generates UTF-8 byte sequence for the specified char[]. The UTF-8 byte sequence is
     * encoded in the specified ByteBuffer.
     *
     * @param srcBuf         the source char[] to be encoded as UTF-8 byte sequence
     * @param srcOffset      offset in the char[] from where the conversion to UTF-8 should begin
     * @param srcLength      the number of chars to be encoded as UTF-8 bytes
     * @param dest           the destination ByteBuffer
     * @param destOffset     offset in the ByteBuffer starting where the encoded UTF-8 bytes should be copied
     * @return the number of bytes encoded
     */
    public static int charstoUTF8Bytes(char[] srcBuf, int srcOffset, int srcLength, ByteBuffer dest,
            int destOffset) {
        int destMark = destOffset;

        for (int i = srcOffset; i < srcLength;) {
            char ch = srcBuf[i];

            if (ch < 0x0080) {
                dest.put(destOffset++, (byte) ch);
            } else if (ch < 0x0800) {
                dest.put(destOffset++, (byte) (0xc0 | (ch >> 6)));
                dest.put(destOffset++, (byte) (0x80 | ((ch >> 0) & 0x3f)));
            } else if (((ch >= 0x0800) && (ch <= 0xD7FF)) || ((ch >= 0xE000) && (ch <= 0xFFFF))) {
                dest.put(destOffset++, (byte) (0xe0 | (ch >> 12)));
                dest.put(destOffset++, (byte) (0x80 | ((ch >> 6) & 0x3F)));
                dest.put(destOffset++, (byte) (0x80 | ((ch >> 0) & 0x3F)));
            } else if ((ch >= Character.MIN_SURROGATE) && (ch <= Character.MAX_SURROGATE)) { // Surrogate pair
                if (i == srcBuf.length) {
                    throw new IllegalStateException(format(MSG_INVALID_CODEPOINT, ch));
                }

                char ch1 = ch;
                char ch2 = srcBuf[++i];

                if (ch1 > Character.MAX_HIGH_SURROGATE) {
                    throw new IllegalStateException(format(MSG_INVALID_CODEPOINT, ch1));
                }

                int codePoint = Character.toCodePoint(ch1, ch2);
                //                int codePoint = (((ch1 & 0x03FF) << 10) | (ch2 & 0x03FF)) + Character.MIN_SUPPLEMENTARY_CODE_POINT;

                dest.put(destOffset++, (byte) (0xf0 | (codePoint >> 18)));
                dest.put(destOffset++, (byte) (0x80 | ((codePoint >> 12) & 0x3F)));
                dest.put(destOffset++, (byte) (0x80 | ((codePoint >> 6) & 0x3F)));
                dest.put(destOffset++, (byte) (0x80 | ((codePoint >> 0) & 0x3F)));
            } else {
                throw new IllegalStateException(format(MSG_INVALID_CODEPOINT, ch));
            }

            i++;
        }

        return destOffset - destMark;
    }
}

Related

  1. blockCopy(ByteBuffer source, int srcOffset, byte[] dest, int destOffset, int count)
  2. byteBuffersToString(ByteBuffer[] bufs, Charset cs)
  3. byteIndexOf(ByteBuffer buffer, byte[] temp, int offset)
  4. c_memset(ByteBuffer b, int c_, int size)
  5. charSequence2ByteBuffer(CharSequence cs, Charset charset)
  6. equals(final ByteBuffer keyValue, final int offset, final int keyLen, final byte[] otherKey)
  7. find(ByteBuffer buffer, int offset, byte searchKey)
  8. findCommonPrefix(ByteBuffer buffer, int offsetLeft, int offsetRight, int limit)
  9. from(ByteBuffer buffer, int offset)