Here you can find the source of charstoUTF8Bytes(char[] srcBuf, int srcOffset, int srcLength, ByteBuffer dest, int destOffset)
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 |
public static int charstoUTF8Bytes(char[] srcBuf, int srcOffset, int srcLength, ByteBuffer dest, int destOffset)
//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; } }