Debian Codegraph @

core/java/com/android/internal/util/ModifiedUtf8.java

raw symbols SCIP
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package com.android.internal.util;
19
20 import java.io.UTFDataFormatException;
21
22 public class ModifiedUtf8 {
23 /**
24 * Decodes a byte array containing <i>modified UTF-8</i> bytes into a string.
25 *
26 * <p>Note that although this method decodes the (supposedly impossible) zero byte to U+0000,
27 * that's what the RI does too.
28 */
29 public static String decode(byte[] in, char[] out, int offset, int utfSize)
30 throws UTFDataFormatException {
31 int count = 0, s = 0, a;
32 while (count < utfSize) {
33 if ((out[s] = (char) in[offset + count++]) < '\u0080') {
34 s++;
35 } else if (((a = out[s]) & 0xe0) == 0xc0) {
36 if (count >= utfSize) {
37 throw new UTFDataFormatException("bad second byte at " + count);
38 }
39 int b = in[offset + count++];
40 if ((b & 0xC0) != 0x80) {
41 throw new UTFDataFormatException("bad second byte at " + (count - 1));
42 }
43 out[s++] = (char) (((a & 0x1F) << 6) | (b & 0x3F));
44 } else if ((a & 0xf0) == 0xe0) {
45 if (count + 1 >= utfSize) {
46 throw new UTFDataFormatException("bad third byte at " + (count + 1));
47 }
48 int b = in[offset + count++];
49 int c = in[offset + count++];
50 if (((b & 0xC0) != 0x80) || ((c & 0xC0) != 0x80)) {
51 throw new UTFDataFormatException("bad second or third byte at " + (count - 2));
52 }
53 out[s++] = (char) (((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F));
54 } else {
55 throw new UTFDataFormatException("bad byte at " + (count - 1));
56 }
57 }
58 return new String(out, 0, s);
59 }
60
61 /**
62 * Returns the number of bytes the modified UTF-8 representation of 's' would take. Note
63 * that this is just the space for the bytes representing the characters, not the length
64 * which precedes those bytes, because different callers represent the length differently,
65 * as two, four, or even eight bytes. If {@code shortLength} is true, we'll throw an
66 * exception if the string is too long for its length to be represented by a short.
67 */
68 public static long countBytes(String s, boolean shortLength) throws UTFDataFormatException {
69 long result = 0;
70 final int length = s.length();
71 for (int i = 0; i < length; ++i) {
72 char ch = s.charAt(i);
73 if (ch != 0 && ch <= 127) { // U+0000 uses two bytes.
74 ++result;
75 } else if (ch <= 2047) {
76 result += 2;
77 } else {
78 result += 3;
79 }
80 if (shortLength && result > 65535) {
81 throw new UTFDataFormatException("String more than 65535 UTF bytes long");
82 }
83 }
84 return result;
85 }
86
87 /**
88 * Encodes the <i>modified UTF-8</i> bytes corresponding to string {@code s} into the
89 * byte array {@code dst}, starting at the given {@code offset}.
90 */
91 public static void encode(byte[] dst, int offset, String s) {
92 final int length = s.length();
93 for (int i = 0; i < length; i++) {
94 char ch = s.charAt(i);
95 if (ch != 0 && ch <= 127) { // U+0000 uses two bytes.
96 dst[offset++] = (byte) ch;
97 } else if (ch <= 2047) {
98 dst[offset++] = (byte) (0xc0 | (0x1f & (ch >> 6)));
99 dst[offset++] = (byte) (0x80 | (0x3f & ch));
100 } else {
101 dst[offset++] = (byte) (0xe0 | (0x0f & (ch >> 12)));
102 dst[offset++] = (byte) (0x80 | (0x3f & (ch >> 6)));
103 dst[offset++] = (byte) (0x80 | (0x3f & ch));
104 }
105 }
106 }
107
108 private ModifiedUtf8() {
109 }
110 }