121 uch = ((uch & ~mask) << 6) | (ch & 0x3F);
122 mask <<= 6 - 1;
123 }
124
125 if ((uch & 0xFFFF) != uch) {
126 throw new UTFDataFormatException("character out of range \\u" + Integer.toHexString(uch));
127 }
128 }
129
130 chars[j++] = (char)uch;
131 }
132 }
133
134 public static String stringFromMUTF8(byte[] bytes, int offset, int count) {
135 int length = charsFromMUTF8Length(bytes, offset, count);
136 char[] chars = new char[length];
137
138 try {
139 charsFromMUTF8(chars, bytes, offset, count);
140 } catch (UTFDataFormatException ex) {
141 throw new InternalError("Attempt to convert non modified UTF-8 byte sequence");
142 }
143
144 return new String(chars);
145 }
146
147 public static String stringFromMUTF8(byte[] bytes) {
148 return stringFromMUTF8(bytes, 0, bytes.length);
149 }
150
151 static int charsFromByteBufferLength(ByteBuffer buffer) {
152 int length = 0;
153
154 while(buffer.hasRemaining()) {
155 byte ch = buffer.get();
156
157 if (ch == 0) {
158 return length;
159 }
160
161 if ((ch & 0xC0) != 0x80) {
169 static void charsFromByteBuffer(char chars[], ByteBuffer buffer) {
170 int j = 0;
171
172 while(buffer.hasRemaining()) {
173 byte ch = buffer.get();
174
175 if (ch == 0) {
176 return;
177 }
178
179 boolean is_unicode = (ch & 0x80) != 0;
180 int uch = ch & 0x7F;
181
182 if (is_unicode) {
183 int mask = 0x40;
184
185 while ((uch & mask) != 0) {
186 ch = buffer.get();
187
188 if ((ch & 0xC0) != 0x80) {
189 throw new InternalError("Bad continuation in modified UTF-8 byte sequence");
190 }
191
192 uch = ((uch & ~mask) << 6) | (ch & 0x3F);
193 mask <<= 6 - 1;
194 }
195 }
196
197 if ((uch & 0xFFFF) != uch) {
198 throw new InternalError("UTF-32 char in modified UTF-8 byte sequence");
199 }
200
201 chars[j++] = (char)uch;
202 }
203
204 throw new InternalError("No terminating zero byte for modified UTF-8 byte sequence");
205 }
206
207 public static String stringFromByteBuffer(ByteBuffer buffer) {
208 int length = charsFromByteBufferLength(buffer);
209 buffer.rewind();
210 char[] chars = new char[length];
211 charsFromByteBuffer(chars, buffer);
212
213 return new String(chars);
214 }
215
216 static int mutf8FromCharsLength(char chars[]) {
217 int length = 0;
218
|
121 uch = ((uch & ~mask) << 6) | (ch & 0x3F);
122 mask <<= 6 - 1;
123 }
124
125 if ((uch & 0xFFFF) != uch) {
126 throw new UTFDataFormatException("character out of range \\u" + Integer.toHexString(uch));
127 }
128 }
129
130 chars[j++] = (char)uch;
131 }
132 }
133
134 public static String stringFromMUTF8(byte[] bytes, int offset, int count) {
135 int length = charsFromMUTF8Length(bytes, offset, count);
136 char[] chars = new char[length];
137
138 try {
139 charsFromMUTF8(chars, bytes, offset, count);
140 } catch (UTFDataFormatException ex) {
141 throw new InternalError("Attempt to convert non modified UTF-8 byte sequence", ex);
142 }
143
144 return new String(chars);
145 }
146
147 public static String stringFromMUTF8(byte[] bytes) {
148 return stringFromMUTF8(bytes, 0, bytes.length);
149 }
150
151 static int charsFromByteBufferLength(ByteBuffer buffer) {
152 int length = 0;
153
154 while(buffer.hasRemaining()) {
155 byte ch = buffer.get();
156
157 if (ch == 0) {
158 return length;
159 }
160
161 if ((ch & 0xC0) != 0x80) {
169 static void charsFromByteBuffer(char chars[], ByteBuffer buffer) {
170 int j = 0;
171
172 while(buffer.hasRemaining()) {
173 byte ch = buffer.get();
174
175 if (ch == 0) {
176 return;
177 }
178
179 boolean is_unicode = (ch & 0x80) != 0;
180 int uch = ch & 0x7F;
181
182 if (is_unicode) {
183 int mask = 0x40;
184
185 while ((uch & mask) != 0) {
186 ch = buffer.get();
187
188 if ((ch & 0xC0) != 0x80) {
189 throw new InternalError("Bad continuation in " +
190 "modified UTF-8 byte sequence: " + ch);
191 }
192
193 uch = ((uch & ~mask) << 6) | (ch & 0x3F);
194 mask <<= 6 - 1;
195 }
196 }
197
198 if ((uch & 0xFFFF) != uch) {
199 throw new InternalError("UTF-32 char in modified UTF-8 " +
200 "byte sequence: " + uch);
201 }
202
203 chars[j++] = (char)uch;
204 }
205
206 throw new InternalError("No terminating zero byte for modified UTF-8 byte sequence");
207 }
208
209 public static String stringFromByteBuffer(ByteBuffer buffer) {
210 int length = charsFromByteBufferLength(buffer);
211 buffer.rewind();
212 char[] chars = new char[length];
213 charsFromByteBuffer(chars, buffer);
214
215 return new String(chars);
216 }
217
218 static int mutf8FromCharsLength(char chars[]) {
219 int length = 0;
220
|