Here you can find the source of encodeAsModifiedUTF8(String str)
Parameter | Description |
---|---|
str | string to encode |
Parameter | Description |
---|---|
UTFDataFormatException | if the string is too long |
public static byte[] encodeAsModifiedUTF8(String str) throws UTFDataFormatException
//package com.java2s; /**// w w w . j av a 2 s . c o m * Copyright (C) 2009 - present by OpenGamma Inc. and other contributors. * * 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 java.io.UTFDataFormatException; public class Main { /** * @param str string to encode * @return byte encoding * @throws UTFDataFormatException if the string is too long */ public static byte[] encodeAsModifiedUTF8(String str) throws UTFDataFormatException { // REVIEW wyliekir 2009-08-17 -- This was taken almost verbatim from // DataOutputStream. int strlen = str.length(); int utflen = 0; int c, count = 0; /* use charAt instead of copying String to char array */ for (int i = 0; i < strlen; i++) { c = str.charAt(i); if ((c >= 0x0001) && (c <= 0x007F)) { utflen++; } else if (c > 0x07FF) { utflen += 3; } else { utflen += 2; } } if (utflen > 65535) throw new UTFDataFormatException("encoded string too long: " + utflen + " bytes"); byte[] bytearr = new byte[utflen]; int i = 0; for (i = 0; i < strlen; i++) { c = str.charAt(i); if (!((c >= 0x0001) && (c <= 0x007F))) break; bytearr[count++] = (byte) c; } for (; i < strlen; i++) { c = str.charAt(i); if ((c >= 0x0001) && (c <= 0x007F)) { bytearr[count++] = (byte) c; } else if (c > 0x07FF) { bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F)); bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F)); bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); } else { bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F)); bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); } } assert count == utflen; return bytearr; } }