1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package com.allanbank.mongodb.bson.io;
22
23 import java.io.IOException;
24 import java.io.OutputStream;
25
26
27
28
29
30
31
32
33
34
35
36 public class StringEncoder {
37
38
39
40
41
42
43
44
45 public static int computeCStringSize(final String string) {
46 return utf8Size(string) + 1;
47 }
48
49
50
51
52
53
54
55
56 public static int computeStringSize(final String string) {
57 return 4 + utf8Size(string) + 1;
58 }
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 public static int utf8Size(final String string) {
82 final int strLength = (string == null) ? 0 : string.length();
83
84 int length = 0;
85 int codePoint;
86 for (int i = 0; i < strLength; i += Character.charCount(codePoint)) {
87 codePoint = Character.codePointAt(string, i);
88 if (codePoint < 0x80) {
89 length += 1;
90 }
91 else if (codePoint < 0x800) {
92 length += 2;
93 }
94 else if (codePoint < 0x10000) {
95 length += 3;
96 }
97 else {
98 length += 4;
99 }
100 }
101
102 return length;
103 }
104
105
106 private final byte[] myBuffer = new byte[1024];
107
108
109 private final StringEncoderCache myCache;
110
111
112
113
114 public StringEncoder() {
115 this(new StringEncoderCache());
116 }
117
118
119
120
121
122
123
124 public StringEncoder(final StringEncoderCache cache) {
125 myCache = cache;
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139
140 public void encode(final String string, final OutputStream out)
141 throws IOException {
142
143 if (!string.isEmpty()) {
144 final byte[] encoded = myCache.find(string);
145
146 if (encoded == null) {
147
148 fastEncode(string, out);
149 }
150 else {
151 myCache.used(string, encoded, 0, encoded.length);
152 out.write(encoded);
153 }
154 }
155 }
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180 public int encodeSize(final String string) {
181 if (string.isEmpty()) {
182 return 0;
183 }
184
185 final byte[] cached = myCache.find(string);
186 if (cached != null) {
187
188 return cached.length;
189 }
190 return utf8Size(string);
191 }
192
193
194
195
196
197
198
199
200
201 @Deprecated
202 public StringEncoderCache getCache() {
203 return myCache;
204 }
205
206
207
208
209
210
211
212
213
214
215
216
217
218 protected void fastEncode(final String string, final OutputStream out)
219 throws IOException {
220
221 final int writeUpTo = myBuffer.length - 4;
222 final int strLength = string.length();
223
224 boolean bufferHasAllBytes = true;
225
226 int bufferOffset = 0;
227 int codePoint;
228 for (int i = 0; i < strLength; i += Character.charCount(codePoint)) {
229
230
231 if (writeUpTo < bufferOffset) {
232 bufferHasAllBytes = false;
233 if (out != null) {
234 out.write(myBuffer, 0, bufferOffset);
235 }
236 bufferOffset = 0;
237 }
238
239 codePoint = Character.codePointAt(string, i);
240 if (codePoint < 0x80) {
241 myBuffer[bufferOffset++] = (byte) codePoint;
242 }
243 else if (codePoint < 0x800) {
244 myBuffer[bufferOffset++] = (byte) (0xC0 + ((codePoint >> 6) & 0xFF));
245 myBuffer[bufferOffset++] = (byte) (0x80 + ((codePoint >> 0) & 0x3F));
246 }
247 else if (codePoint < 0x10000) {
248 myBuffer[bufferOffset++] = (byte) (0xE0 + ((codePoint >> 12) & 0xFF));
249 myBuffer[bufferOffset++] = (byte) (0x80 + ((codePoint >> 6) & 0x3F));
250 myBuffer[bufferOffset++] = (byte) (0x80 + ((codePoint >> 0) & 0x3f));
251 }
252 else {
253 myBuffer[bufferOffset++] = (byte) (0xF0 + ((codePoint >> 18) & 0xFF));
254 myBuffer[bufferOffset++] = (byte) (0x80 + ((codePoint >> 12) & 0x3F));
255 myBuffer[bufferOffset++] = (byte) (0x80 + ((codePoint >> 6) & 0x3F));
256 myBuffer[bufferOffset++] = (byte) (0x80 + ((codePoint >> 0) & 0x3F));
257 }
258 }
259
260
261 if (out != null) {
262 out.write(myBuffer, 0, bufferOffset);
263 }
264
265
266 if (bufferHasAllBytes) {
267 myCache.used(string, myBuffer, 0, bufferOffset);
268 }
269 }
270 }