Latin1Converter.java
3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.internal.xmp.impl;
import com.adobe.internal.xmp.impl.ByteBuffer;
import java.io.UnsupportedEncodingException;
public class Latin1Converter {
private static final int STATE_START = 0;
private static final int STATE_UTF8CHAR = 11;
private Latin1Converter() {
}
public static ByteBuffer convert(ByteBuffer buffer) {
if ("UTF-8".equals(buffer.getEncoding())) {
int b;
byte[] readAheadBuffer = new byte[8];
int readAhead = 0;
int expectedBytes = 0;
ByteBuffer out = new ByteBuffer(buffer.length() * 4 / 3);
int state = 0;
block3 : for (int i = 0; i < buffer.length(); ++i) {
b = buffer.charAt(i);
switch (state) {
default: {
if (b < 127) {
out.append((byte)b);
continue block3;
}
if (b >= 192) {
expectedBytes = -1;
int test = b;
while (expectedBytes < 8 && (test & 128) == 128) {
++expectedBytes;
test <<= 1;
}
readAheadBuffer[readAhead++] = b;
state = 11;
continue block3;
}
byte[] utf8 = Latin1Converter.convertToUTF8((byte)b);
out.append(utf8);
continue block3;
}
case 11: {
if (expectedBytes > 0 && (b & 192) == 128) {
readAheadBuffer[readAhead++] = b;
if (--expectedBytes != 0) continue block3;
out.append(readAheadBuffer, 0, readAhead);
readAhead = 0;
state = 0;
continue block3;
}
byte[] utf8 = Latin1Converter.convertToUTF8(readAheadBuffer[0]);
out.append(utf8);
i -= readAhead;
readAhead = 0;
state = 0;
}
}
}
if (state == 11) {
for (int j = 0; j < readAhead; ++j) {
b = readAheadBuffer[j];
byte[] utf8 = Latin1Converter.convertToUTF8((byte)b);
out.append(utf8);
}
}
return out;
}
return buffer;
}
private static byte[] convertToUTF8(byte ch) {
int c = ch & 255;
try {
if (c >= 128) {
if (c == 129 || c == 141 || c == 143 || c == 144 || c == 157) {
return new byte[]{32};
}
return new String(new byte[]{ch}, "cp1252").getBytes("UTF-8");
}
}
catch (UnsupportedEncodingException e) {
// empty catch block
}
return new byte[]{ch};
}
}