|
| 1 | +/* |
| 2 | + * Copyright 2020 Volker Berlin (i-net software) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package de.inetsoftware.jwebassembly.api.java.lang; |
| 17 | + |
| 18 | +import java.util.Arrays; |
| 19 | + |
| 20 | +import de.inetsoftware.jwebassembly.api.annotation.Replace; |
| 21 | + |
| 22 | +/** |
| 23 | + * Replacement methods for the class java.lang.StringCoding. |
| 24 | + * |
| 25 | + * @author Volker Berlin |
| 26 | + */ |
| 27 | +public class ReplacementForStringCoding { |
| 28 | + |
| 29 | + /** |
| 30 | + * Replacement for StringCoding.decode(byte[] ba, int off, int len) in Java 8. Decode UTF8 bytes to unicode 16 chars. |
| 31 | + */ |
| 32 | + @Replace( "java/lang/StringCoding.decode([BII)[C" ) |
| 33 | + private static char[] decode(byte[] bytes, int offset, int length) { |
| 34 | + int count = 0; |
| 35 | + char[] buffer = new char[length - offset]; |
| 36 | + while( offset < length ) { |
| 37 | + int ch = bytes[offset++] & 0xFF; |
| 38 | + |
| 39 | + if( ch <= 0x7F ) { |
| 40 | + //ch = ch; |
| 41 | + } else if( ch <= 0xDF ) { |
| 42 | + ch = ((ch & 0x1F) << 6) | (bytes[offset++] & 0x3F); |
| 43 | + } else if( ch <= 0xEF ) { |
| 44 | + ch = ((ch & 0x0F) << 12) | ((bytes[offset++] & 0x3F) << 6) | (bytes[offset++] & 0x3F); |
| 45 | + } else { |
| 46 | + ch = ((ch & 0x07) << 18) | ((bytes[offset++] & 0x3F) << 12) | ((bytes[offset++] & 0x3F) << 6) | (bytes[offset++] & 0x3F); |
| 47 | + // high surrogate |
| 48 | + buffer[count++] = (char)(0xD7C0 + ((ch >> 10) & 0x3ff)); |
| 49 | + // low surrogate |
| 50 | + ch = 0xDC00 + (ch & 0x3ff); |
| 51 | + } |
| 52 | + |
| 53 | + buffer[count++] = (char)ch; |
| 54 | + } |
| 55 | + |
| 56 | + return count == length ? buffer : Arrays.copyOf( buffer, count ); |
| 57 | + } |
| 58 | +} |
0 commit comments