-
Notifications
You must be signed in to change notification settings - Fork 4
/
StringChunk.java
122 lines (108 loc) · 4.44 KB
/
StringChunk.java
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.axml.chunk;
import com.axml.chunk.base.BaseChunk;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Sens on 2021/8/27.
*/
public class StringChunk extends BaseChunk {
public int stringCount;
public int styleCount;
public boolean isUTF8;
public boolean isSorted;
public int stringStart;
public int styleStart;
public int[] stringOffsets;
public int[] styleOffsets;
public List<String> stringList;
public StringChunk(ByteBuffer byteBuffer) {
super(byteBuffer);
this.stringCount = byteBuffer.getInt();
this.styleCount = byteBuffer.getInt();
//utf8(0x0100) or default utf16le(0x0000)
this.isUTF8 = byteBuffer.getShort() != 0;
this.isSorted = byteBuffer.getShort() != 0;
this.stringStart = byteBuffer.getInt();
this.styleStart = byteBuffer.getInt();
stringOffsets = new int[stringCount];
for (int i = 0; i < stringOffsets.length; i++)
stringOffsets[i] = byteBuffer.getInt();
styleOffsets = new int[styleCount];
for (int i = 0; i < styleOffsets.length; i++)
styleOffsets[i] = byteBuffer.getInt();
stringList = new ArrayList<>(stringCount);
for (int i = 0; i < stringCount; i++) {
byteBuffer.position(ChunkStartPosition + stringStart + stringOffsets[i]);
int byteCount;
if (isUTF8) {
int strCount = byteBuffer.get() & 0xFF;
byteCount = byteBuffer.get() & 0xFF;
} else
byteCount = byteBuffer.getShort() * 2;
byte[] string = new byte[byteCount];
byteBuffer.get(string);
if (isUTF8) stringList.add(new String(string, StandardCharsets.UTF_8));
else stringList.add(new String(string, StandardCharsets.UTF_16LE));
}
//styleCount always = 0 [skip]
byteBuffer.position(ChunkStartPosition + chunkSize);
}
public String getString(int index) {
return stringList.get(index);
}
private void stringToBytes(ByteArrayOutputStream stream, String str) throws IOException {
ByteBuffer byteBuffer;
if (isUTF8) {
byte[] chars = str.getBytes(StandardCharsets.UTF_8);
byteBuffer = ByteBuffer.allocate(2 + chars.length + 1);
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
byteBuffer.put((byte) str.length());
byteBuffer.put((byte) chars.length);
byteBuffer.put(chars);
byteBuffer.put((byte) 0);
} else {
byte[] chars = str.getBytes(StandardCharsets.UTF_16LE);
byteBuffer = ByteBuffer.allocate(2 + chars.length + 2);
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
byteBuffer.putShort((short) str.length());
byteBuffer.put(chars);
byteBuffer.putShort((short) 0);
}
stream.write(byteBuffer.array());
}
@Override
protected void toBytes(ByteArrayOutputStream stream) throws IOException {
stringCount = stringList.size();
ByteBuffer byteBuffer = ByteBuffer.allocate(5 * 4 + stringOffsets.length * 4 + styleOffsets.length * 4);
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
byteBuffer.putInt(stringCount);
byteBuffer.putInt(styleCount);
byteBuffer.order(ByteOrder.BIG_ENDIAN);
byteBuffer.putShort((short) (isUTF8 ? 1 : 0));
byteBuffer.putShort((short) (isSorted ? 1 : 0));
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
byteBuffer.putInt(stringStart);
byteBuffer.putInt(styleStart);
int stringOffset = 0;
if (stringOffsets.length != stringCount)
stringOffsets = new int[stringCount];
for (int i = 0; i < stringCount; i++) {
stringOffsets[i] = stringOffset;
byteBuffer.putInt(stringOffset);
if (isUTF8)
stringOffset += 3 + stringList.get(i).getBytes(StandardCharsets.UTF_8).length;
else
stringOffset += 4 + stringList.get(i).getBytes(StandardCharsets.UTF_16LE).length;
}
//styleCount always = 0 [skip]
for (int offset : styleOffsets) byteBuffer.putInt(offset);
stream.write(byteBuffer.array());
for (String str : stringList)
stringToBytes(stream, str);
}
}