| 1 |
package com.fasterxml.jackson.dataformat.smile; |
| 2 |
|
| 3 |
import java.io.*; |
| 4 |
import java.lang.ref.SoftReference; |
| 5 |
import java.math.BigDecimal; |
| 6 |
import java.math.BigInteger; |
| 7 |
import java.util.Arrays; |
| 8 |
|
| 9 |
import com.fasterxml.jackson.core.*; |
| 10 |
import com.fasterxml.jackson.core.base.ParserBase; |
| 11 |
import com.fasterxml.jackson.core.io.IOContext; |
| 12 |
import com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer; |
| 13 |
|
| 14 |
import static com.fasterxml.jackson.dataformat.smile.SmileConstants.BYTE_MARKER_END_OF_STRING; |
| 15 |
|
| 16 |
public class SmileParser extends ParserBase |
| 17 |
{ |
| 18 |
/** |
| 19 |
* Enumeration that defines all togglable features for Smile generators. |
| 20 |
*/ |
| 21 |
public enum Feature implements FormatFeature |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Feature that determines whether 4-byte Smile header is mandatory in input, |
| 25 |
* or optional. If enabled, it means that only input that starts with the header |
| 26 |
* is accepted as valid; if disabled, header is optional. In latter case,r |
| 27 |
* settings for content are assumed to be defaults. |
| 28 |
*/ |
| 29 |
REQUIRE_HEADER(true) |
| 30 |
; |
| 31 |
|
| 32 |
final boolean _defaultState; |
| 33 |
final int _mask; |
| 34 |
|
| 35 |
/** |
| 36 |
* Method that calculates bit set (flags) of all features that |
| 37 |
* are enabled by default. |
| 38 |
*/ |
| 39 |
public static int collectDefaults() |
| 40 |
{ |
| 41 |
int flags = 0; |
| 42 |
for (Feature f : values()) { |
| 43 |
if (f.enabledByDefault()) { |
| 44 |
flags |= f.getMask(); |
| 45 |
} |
| 46 |
} |
| 47 |
return flags; |
| 48 |
} |
| 49 |
|
| 50 |
private Feature(boolean defaultState) { |
| 51 |
_defaultState = defaultState; |
| 52 |
_mask = (1 << ordinal()); |
| 53 |
} |
| 54 |
|
| 55 |
@Override public boolean enabledByDefault() { return _defaultState; } |
| 56 |
@Override public int getMask() { return _mask; } |
| 57 |
@Override public boolean enabledIn(int flags) { return (flags & getMask()) != 0; } |
| 58 |
} |
| 59 |
|
| 60 |
private final static int[] NO_INTS = new int[0]; |
| 61 |
|
| 62 |
private final static String[] NO_STRINGS = new String[0]; |
| 63 |
|
| 64 |
/* |
| 65 |
/********************************************************** |
| 66 |
/* Configuration |
| 67 |
/********************************************************** |
| 68 |
*/ |
| 69 |
|
| 70 |
/** |
| 71 |
* Codec used for data binding when (if) requested. |
| 72 |
*/ |
| 73 |
protected ObjectCodec _objectCodec; |
| 74 |
|
| 75 |
/** |
| 76 |
* Flag that indicates whether content can legally have raw (unquoted) |
| 77 |
* binary data. Since this information is included both in header and |
| 78 |
* in actual binary data blocks there is redundancy, and we want to |
| 79 |
* ensure settings are compliant. Using application may also want to |
| 80 |
* know this setting in case it does some direct (random) access. |
| 81 |
*/ |
| 82 |
protected boolean _mayContainRawBinary; |
| 83 |
|
| 84 |
/** |
| 85 |
* Helper object used for low-level recycling of Smile-generator |
| 86 |
* specific buffers. |
| 87 |
*/ |
| 88 |
final protected SmileBufferRecycler<String> _smileBufferRecycler; |
| 89 |
|
| 90 |
/* |
| 91 |
/********************************************************** |
| 92 |
/* Input source config, state (from ex StreamBasedParserBase) |
| 93 |
/********************************************************** |
| 94 |
*/ |
| 95 |
|
| 96 |
/** |
| 97 |
* Input stream that can be used for reading more content, if one |
| 98 |
* in use. May be null, if input comes just as a full buffer, |
| 99 |
* or if the stream has been closed. |
| 100 |
*/ |
| 101 |
protected InputStream _inputStream; |
| 102 |
|
| 103 |
/** |
| 104 |
* Current buffer from which data is read; generally data is read into |
| 105 |
* buffer from input source, but in some cases pre-loaded buffer |
| 106 |
* is handed to the parser. |
| 107 |
*/ |
| 108 |
protected byte[] _inputBuffer; |
| 109 |
|
| 110 |
/** |
| 111 |
* Bit flag composed of bits that indicate which |
| 112 |
* {@link SmileParser.Feature}s are enabled. |
| 113 |
*<p> |
| 114 |
* NOTE: currently the only feature ({@link SmileParser.Feature#REQUIRE_HEADER} |
| 115 |
* takes effect during bootstrapping. |
| 116 |
*/ |
| 117 |
protected int _formatFeatures; |
| 118 |
|
| 119 |
/** |
| 120 |
* Flag that indicates whether the input buffer is recycable (and |
| 121 |
* needs to be returned to recycler once we are done) or not. |
| 122 |
*<p> |
| 123 |
* If it is not, it also means that parser can NOT modify underlying |
| 124 |
* buffer. |
| 125 |
*/ |
| 126 |
protected boolean _bufferRecyclable; |
| 127 |
|
| 128 |
/* |
| 129 |
/********************************************************** |
| 130 |
/* Additional parsing state |
| 131 |
/********************************************************** |
| 132 |
*/ |
| 133 |
|
| 134 |
/** |
| 135 |
* Flag that indicates that the current token has not yet |
| 136 |
* been fully processed, and needs to be finished for |
| 137 |
* some access (or skipped to obtain the next token) |
| 138 |
*/ |
| 139 |
protected boolean _tokenIncomplete = false; |
| 140 |
|
| 141 |
/** |
| 142 |
* Type byte of the current token (as in) |
| 143 |
*/ |
| 144 |
protected int _typeAsInt; |
| 145 |
|
| 146 |
/** |
| 147 |
* Specific flag that is set when we encountered a 32-bit |
| 148 |
* floating point value; needed since numeric super classes do |
| 149 |
* not track distinction between float and double, but Smile |
| 150 |
* format does, and we want to retain that separation. |
| 151 |
*/ |
| 152 |
protected boolean _got32BitFloat; |
| 153 |
|
| 154 |
/** |
| 155 |
* Alternative to {@link #_tokenInputTotal} that will only contain |
| 156 |
* offset within input buffer, as int. |
| 157 |
*/ |
| 158 |
protected int _tokenOffsetForTotal; |
| 159 |
|
| 160 |
/* |
| 161 |
/********************************************************** |
| 162 |
/* Symbol handling, decoding |
| 163 |
/********************************************************** |
| 164 |
*/ |
| 165 |
|
| 166 |
/** |
| 167 |
* Symbol table that contains field names encountered so far |
| 168 |
*/ |
| 169 |
final protected ByteQuadsCanonicalizer _symbols; |
| 170 |
|
| 171 |
/** |
| 172 |
* Temporary buffer used for name parsing. |
| 173 |
*/ |
| 174 |
protected int[] _quadBuffer = NO_INTS; |
| 175 |
|
| 176 |
/** |
| 177 |
* Quads used for hash calculation |
| 178 |
*/ |
| 179 |
protected int _quad1, _quad2, _quad3; |
| 180 |
|
| 181 |
/** |
| 182 |
* Array of recently seen field names, which may be back referenced |
| 183 |
* by later fields. |
| 184 |
* Defaults set to enable handling even if no header found. |
| 185 |
*/ |
| 186 |
protected String[] _seenNames = NO_STRINGS; |
| 187 |
|
| 188 |
protected int _seenNameCount = 0; |
| 189 |
|
| 190 |
/** |
| 191 |
* Array of recently seen field names, which may be back referenced |
| 192 |
* by later fields |
| 193 |
* Defaults set to disable handling if no header found. |
| 194 |
*/ |
| 195 |
protected String[] _seenStringValues = null; |
| 196 |
|
| 197 |
protected int _seenStringValueCount = -1; |
| 198 |
|
| 199 |
/* |
| 200 |
/********************************************************** |
| 201 |
/* Thread-local recycling |
| 202 |
/********************************************************** |
| 203 |
*/ |
| 204 |
|
| 205 |
/** |
| 206 |
* <code>ThreadLocal</code> contains a {@link java.lang.ref.SoftReference} |
| 207 |
* to a buffer recycler used to provide a low-cost |
| 208 |
* buffer recycling for Smile-specific buffers. |
| 209 |
*/ |
| 210 |
final protected static ThreadLocal<SoftReference<SmileBufferRecycler<String>>> _smileRecyclerRef |
| 211 |
= new ThreadLocal<SoftReference<SmileBufferRecycler<String>>>(); |
| 212 |
|
| 213 |
/* |
| 214 |
/********************************************************** |
| 215 |
/* Life-cycle |
| 216 |
/********************************************************** |
| 217 |
*/ |
| 218 |
|
| 219 |
public SmileParser(IOContext ctxt, int parserFeatures, int smileFeatures, |
| 220 |
ObjectCodec codec, |
| 221 |
ByteQuadsCanonicalizer sym, |
| 222 |
InputStream in, byte[] inputBuffer, int start, int end, |
| 223 |
boolean bufferRecyclable) |
| 224 |
{ |
| 225 |
super(ctxt, parserFeatures); |
| 226 |
_objectCodec = codec; |
| 227 |
_symbols = sym; |
| 228 |
_formatFeatures = smileFeatures; |
| 229 |
|
| 230 |
_inputStream = in; |
| 231 |
_inputBuffer = inputBuffer; |
| 232 |
_inputPtr = start; |
| 233 |
_inputEnd = end; |
| 234 |
_bufferRecyclable = bufferRecyclable; |
| 235 |
|
| 236 |
_tokenInputRow = -1; |
| 237 |
_tokenInputCol = -1; |
| 238 |
_smileBufferRecycler = _smileBufferRecycler(); |
| 239 |
} |
| 240 |
|
| 241 |
@Override |
| 242 |
public ObjectCodec getCodec() { |
| 243 |
return _objectCodec; |
| 244 |
} |
| 245 |
|
| 246 |
@Override |
| 247 |
public void setCodec(ObjectCodec c) { |
| 248 |
_objectCodec = c; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Helper method called when it looks like input might contain the signature; |
| 253 |
* and it is necessary to detect and handle signature to get configuration |
| 254 |
* information it might have. |
| 255 |
* |
| 256 |
* @return True if valid signature was found and handled; false if not |
| 257 |
*/ |
| 258 |
protected boolean handleSignature(boolean consumeFirstByte, boolean throwException) throws IOException |
| 259 |
{ |
| 260 |
if (consumeFirstByte) { |
| 261 |
++_inputPtr; |
| 262 |
} |
| 263 |
if (_inputPtr >= _inputEnd) { |
| 264 |
loadMoreGuaranteed(); |
| 265 |
} |
| 266 |
if (_inputBuffer[_inputPtr] != SmileConstants.HEADER_BYTE_2) { |
| 267 |
if (throwException) { |
| 268 |
_reportError("Malformed content: signature not valid, starts with 0x3a but followed by 0x" |
| 269 |
+Integer.toHexString(_inputBuffer[_inputPtr])+", not 0x29"); |
| 270 |
} |
| 271 |
return false; |
| 272 |
} |
| 273 |
if (++_inputPtr >= _inputEnd) { |
| 274 |
loadMoreGuaranteed(); |
| 275 |
} |
| 276 |
if (_inputBuffer[_inputPtr] != SmileConstants.HEADER_BYTE_3) { |
| 277 |
if (throwException) { |
| 278 |
_reportError("Malformed content: signature not valid, starts with 0x3a, 0x29, but followed by 0x" |
| 279 |
+Integer.toHexString(_inputBuffer[_inputPtr])+", not 0xA"); |
| 280 |
} |
| 281 |
return false; |
| 282 |
} |
| 283 |
// Good enough; just need version info from 4th byte... |
| 284 |
if (++_inputPtr >= _inputEnd) { |
| 285 |
loadMoreGuaranteed(); |
| 286 |
} |
| 287 |
int ch = _inputBuffer[_inputPtr++]; |
| 288 |
int versionBits = (ch >> 4) & 0x0F; |
| 289 |
// but failure with version number is fatal, can not ignore |
| 290 |
if (versionBits != SmileConstants.HEADER_VERSION_0) { |
| 291 |
_reportError("Header version number bits (0x"+Integer.toHexString(versionBits)+") indicate unrecognized version; only 0x0 handled by parser"); |
| 292 |
} |
| 293 |
|
| 294 |
// can avoid tracking names, if explicitly disabled |
| 295 |
if ((ch & SmileConstants.HEADER_BIT_HAS_SHARED_NAMES) == 0) { |
| 296 |
_seenNames = null; |
| 297 |
_seenNameCount = -1; |
| 298 |
} |
| 299 |
// conversely, shared string values must be explicitly enabled |
| 300 |
if ((ch & SmileConstants.HEADER_BIT_HAS_SHARED_STRING_VALUES) != 0) { |
| 301 |
_seenStringValues = NO_STRINGS; |
| 302 |
_seenStringValueCount = 0; |
| 303 |
} |
| 304 |
_mayContainRawBinary = ((ch & SmileConstants.HEADER_BIT_HAS_RAW_BINARY) != 0); |
| 305 |
return true; |
| 306 |
} |
| 307 |
|
| 308 |
protected final static SmileBufferRecycler<String> _smileBufferRecycler() |
| 309 |
{ |
| 310 |
SoftReference<SmileBufferRecycler<String>> ref = _smileRecyclerRef.get(); |
| 311 |
SmileBufferRecycler<String> br = (ref == null) ? null : ref.get(); |
| 312 |
|
| 313 |
if (br == null) { |
| 314 |
br = new SmileBufferRecycler<String>(); |
| 315 |
_smileRecyclerRef.set(new SoftReference<SmileBufferRecycler<String>>(br)); |
| 316 |
} |
| 317 |
return br; |
| 318 |
} |
| 319 |
|
| 320 |
/* |
| 321 |
/********************************************************** |
| 322 |
/* Versioned |
| 323 |
/********************************************************** |
| 324 |
*/ |
| 325 |
|
| 326 |
@Override |
| 327 |
public Version version() { |
| 328 |
return PackageVersion.VERSION; |
| 329 |
} |
| 330 |
|
| 331 |
/* |
| 332 |
/********************************************************** |
| 333 |
/* FormatFeature support |
| 334 |
/********************************************************** |
| 335 |
*/ |
| 336 |
|
| 337 |
@Override |
| 338 |
public int getFormatFeatures() { |
| 339 |
return _formatFeatures; |
| 340 |
} |
| 341 |
|
| 342 |
@Override |
| 343 |
public JsonParser overrideFormatFeatures(int values, int mask) { |
| 344 |
_formatFeatures = (_formatFeatures & ~mask) | (values & mask); |
| 345 |
return this; |
| 346 |
} |
| 347 |
|
| 348 |
/* |
| 349 |
/********************************************************** |
| 350 |
/* Former StreamBasedParserBase methods |
| 351 |
/********************************************************** |
| 352 |
*/ |
| 353 |
|
| 354 |
@Override |
| 355 |
public int releaseBuffered(OutputStream out) throws IOException |
| 356 |
{ |
| 357 |
int count = _inputEnd - _inputPtr; |
| 358 |
if (count < 1) { |
| 359 |
return 0; |
| 360 |
} |
| 361 |
// let's just advance ptr to end |
| 362 |
int origPtr = _inputPtr; |
| 363 |
out.write(_inputBuffer, origPtr, count); |
| 364 |
return count; |
| 365 |
} |
| 366 |
|
| 367 |
@Override |
| 368 |
public Object getInputSource() { |
| 369 |
return _inputStream; |
| 370 |
} |
| 371 |
|
| 372 |
/** |
| 373 |
* Overridden since we do not really have character-based locations, |
| 374 |
* but we do have byte offset to specify. |
| 375 |
*/ |
| 376 |
@Override |
| 377 |
public JsonLocation getTokenLocation() |
| 378 |
{ |
| 379 |
// token location is correctly managed... |
| 380 |
long total = _currInputProcessed + _tokenOffsetForTotal; |
| 381 |
// 2.4: used to be: _tokenInputTotal |
| 382 |
return new JsonLocation(_ioContext.getSourceReference(), |
| 383 |
total, // bytes |
| 384 |
-1, -1, (int) total); // char offset, line, column |
| 385 |
} |
| 386 |
|
| 387 |
/** |
| 388 |
* Overridden since we do not really have character-based locations, |
| 389 |
* but we do have byte offset to specify. |
| 390 |
*/ |
| 391 |
@Override |
| 392 |
public JsonLocation getCurrentLocation() |
| 393 |
{ |
| 394 |
final long offset = _currInputProcessed + _inputPtr; |
| 395 |
return new JsonLocation(_ioContext.getSourceReference(), |
| 396 |
offset, // bytes |
| 397 |
-1, -1, (int) offset); // char offset, line, column |
| 398 |
} |
| 399 |
|
| 400 |
/* |
| 401 |
/********************************************************** |
| 402 |
/* Low-level reading, other |
| 403 |
/********************************************************** |
| 404 |
*/ |
| 405 |
|
| 406 |
@Override |
| 407 |
protected final boolean loadMore() throws IOException |
| 408 |
{ |
| 409 |
//_currInputRowStart -= _inputEnd; |
| 410 |
|
| 411 |
if (_inputStream != null) { |
| 412 |
int count = _inputStream.read(_inputBuffer, 0, _inputBuffer.length); |
| 413 |
_currInputProcessed += _inputEnd; |
| 414 |
_inputPtr = 0; |
| 415 |
if (count > 0) { |
| 416 |
_inputEnd = count; |
| 417 |
return true; |
| 418 |
} |
| 419 |
// important: move pointer to same as end, to keep location accurate |
| 420 |
_inputEnd = 0; |
| 421 |
// End of input |
| 422 |
_closeInput(); |
| 423 |
// Should never return 0, so let's fail |
| 424 |
if (count == 0) { |
| 425 |
throw new IOException("InputStream.read() returned 0 characters when trying to read "+_inputBuffer.length+" bytes"); |
| 426 |
} |
| 427 |
} |
| 428 |
return false; |
| 429 |
} |
| 430 |
|
| 431 |
/** |
| 432 |
* Helper method that will try to load at least specified number bytes in |
| 433 |
* input buffer, possible moving existing data around if necessary |
| 434 |
*/ |
| 435 |
protected final void _loadToHaveAtLeast(int minAvailable) throws IOException |
| 436 |
{ |
| 437 |
// No input stream, no leading (either we are closed, or have non-stream input source) |
| 438 |
if (_inputStream == null) { |
| 439 |
throw _constructError("Needed to read "+minAvailable+" bytes, reached end-of-input"); |
| 440 |
} |
| 441 |
// Need to move remaining data in front? |
| 442 |
int amount = _inputEnd - _inputPtr; |
| 443 |
_currInputProcessed += _inputPtr; |
| 444 |
if (amount > 0 && _inputPtr > 0) { |
| 445 |
//_currInputRowStart -= _inputPtr; |
| 446 |
System.arraycopy(_inputBuffer, _inputPtr, _inputBuffer, 0, amount); |
| 447 |
_inputEnd = amount; |
| 448 |
} else { |
| 449 |
_inputEnd = 0; |
| 450 |
} |
| 451 |
_inputPtr = 0; |
| 452 |
while (_inputEnd < minAvailable) { |
| 453 |
int count = _inputStream.read(_inputBuffer, _inputEnd, _inputBuffer.length - _inputEnd); |
| 454 |
if (count < 1) { |
| 455 |
// End of input |
| 456 |
_closeInput(); |
| 457 |
// Should never return 0, so let's fail |
| 458 |
if (count == 0) { |
| 459 |
throw new IOException("InputStream.read() returned 0 characters when trying to read "+amount+" bytes"); |
| 460 |
} |
| 461 |
throw _constructError("Needed to read "+minAvailable+" bytes, missed "+minAvailable+" before end-of-input"); |
| 462 |
} |
| 463 |
_inputEnd += count; |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
@Override |
| 468 |
protected void _closeInput() throws IOException |
| 469 |
{ |
| 470 |
if (_inputStream != null) { |
| 471 |
if (_ioContext.isResourceManaged() || isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE)) { |
| 472 |
_inputStream.close(); |
| 473 |
} |
| 474 |
_inputStream = null; |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
/* |
| 479 |
/********************************************************** |
| 480 |
/* Overridden methods |
| 481 |
/********************************************************** |
| 482 |
*/ |
| 483 |
|
| 484 |
@Override |
| 485 |
protected void _finishString() throws IOException { |
| 486 |
// should never be called; but must be defined for superclass |
| 487 |
_throwInternal(); |
| 488 |
} |
| 489 |
|
| 490 |
@Override |
| 491 |
public void close() throws IOException { |
| 492 |
super.close(); |
| 493 |
// Merge found symbols, if any: |
| 494 |
_symbols.release(); |
| 495 |
} |
| 496 |
|
| 497 |
@Override |
| 498 |
public boolean hasTextCharacters() |
| 499 |
{ |
| 500 |
if (_currToken == JsonToken.VALUE_STRING) { |
| 501 |
// yes; is or can be made available efficiently as char[] |
| 502 |
return _textBuffer.hasTextAsCharacters(); |
| 503 |
} |
| 504 |
if (_currToken == JsonToken.FIELD_NAME) { |
| 505 |
// not necessarily; possible but: |
| 506 |
return _nameCopied; |
| 507 |
} |
| 508 |
// other types, no benefit from accessing as char[] |
| 509 |
return false; |
| 510 |
} |
| 511 |
|
| 512 |
/** |
| 513 |
* Method called to release internal buffers owned by the base |
| 514 |
* reader. This may be called along with {@link #_closeInput} (for |
| 515 |
* example, when explicitly closing this reader instance), or |
| 516 |
* separately (if need be). |
| 517 |
*/ |
| 518 |
@Override |
| 519 |
protected void _releaseBuffers() throws IOException |
| 520 |
{ |
| 521 |
super._releaseBuffers(); |
| 522 |
if (_bufferRecyclable) { |
| 523 |
byte[] buf = _inputBuffer; |
| 524 |
if (buf != null) { |
| 525 |
_inputBuffer = null; |
| 526 |
_ioContext.releaseReadIOBuffer(buf); |
| 527 |
} |
| 528 |
} |
| 529 |
{ |
| 530 |
String[] nameBuf = _seenNames; |
| 531 |
if (nameBuf != null && nameBuf.length > 0) { |
| 532 |
_seenNames = null; |
| 533 |
/* 28-Jun-2011, tatu: With 1.9, caller needs to clear the buffer; |
| 534 |
* but we only need to clear up to count as it is not a hash area |
| 535 |
*/ |
| 536 |
if (_seenNameCount > 0) { |
| 537 |
Arrays.fill(nameBuf, 0, _seenNameCount, null); |
| 538 |
} |
| 539 |
_smileBufferRecycler.releaseSeenNamesBuffer(nameBuf); |
| 540 |
} |
| 541 |
} |
| 542 |
{ |
| 543 |
String[] valueBuf = _seenStringValues; |
| 544 |
if (valueBuf != null && valueBuf.length > 0) { |
| 545 |
_seenStringValues = null; |
| 546 |
/* 28-Jun-2011, tatu: With 1.9, caller needs to clear the buffer; |
| 547 |
* but we only need to clear up to count as it is not a hash area |
| 548 |
*/ |
| 549 |
if (_seenStringValueCount > 0) { |
| 550 |
Arrays.fill(valueBuf, 0, _seenStringValueCount, null); |
| 551 |
} |
| 552 |
_smileBufferRecycler.releaseSeenStringValuesBuffer(valueBuf); |
| 553 |
} |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
/* |
| 558 |
/********************************************************** |
| 559 |
/* Extended API |
| 560 |
/********************************************************** |
| 561 |
*/ |
| 562 |
|
| 563 |
public boolean mayContainRawBinary() { |
| 564 |
return _mayContainRawBinary; |
| 565 |
} |
| 566 |
|
| 567 |
/* |
| 568 |
/********************************************************** |
| 569 |
/* JsonParser impl |
| 570 |
/********************************************************** |
| 571 |
*/ |
| 572 |
|
| 573 |
@Override |
| 574 |
public JsonToken nextToken() throws IOException |
| 575 |
{ |
| 576 |
_numTypesValid = NR_UNKNOWN; |
| 577 |
// For longer tokens (text, binary), we'll only read when requested |
| 578 |
if (_tokenIncomplete) { |
| 579 |
_skipIncomplete(); |
| 580 |
} |
| 581 |
_tokenOffsetForTotal = _inputPtr; |
| 582 |
// _tokenInputTotal = _currInputProcessed + _inputPtr; |
| 583 |
// also: clear any data retained so far |
| 584 |
_binaryValue = null; |
| 585 |
// Two main modes: values, and field names. |
| 586 |
if ((_currToken != JsonToken.FIELD_NAME) && _parsingContext.inObject()) { |
| 587 |
return (_currToken = _handleFieldName()); |
| 588 |
} |
| 589 |
if (_inputPtr >= _inputEnd) { |
| 590 |
if (!loadMore()) { |
| 591 |
return _eofAsNextToken(); |
| 592 |
} |
| 593 |
} |
| 594 |
int ch = _inputBuffer[_inputPtr++] & 0xFF; |
| 595 |
_typeAsInt = ch; |
| 596 |
switch (ch >> 5) { |
| 597 |
case 0: // short shared string value reference |
| 598 |
if (ch != 0) { // 0x0 is invalid |
| 599 |
return _handleSharedString(ch-1); |
| 600 |
} |
| 601 |
break; |
| 602 |
|
| 603 |
case 1: // simple literals, numbers |
| 604 |
{ |
| 605 |
int typeBits = ch & 0x1F; |
| 606 |
if (typeBits < 4) { |
| 607 |
switch (typeBits) { |
| 608 |
case 0x00: |
| 609 |
_textBuffer.resetWithEmpty(); |
| 610 |
return (_currToken = JsonToken.VALUE_STRING); |
| 611 |
case 0x01: |
| 612 |
return (_currToken = JsonToken.VALUE_NULL); |
| 613 |
case 0x02: // false |
| 614 |
return (_currToken = JsonToken.VALUE_FALSE); |
| 615 |
default: // 0x03 == true |
| 616 |
return (_currToken = JsonToken.VALUE_TRUE); |
| 617 |
} |
| 618 |
} |
| 619 |
if (typeBits == 4) { |
| 620 |
_finishInt(); |
| 621 |
return (_currToken = JsonToken.VALUE_NUMBER_INT); |
| 622 |
} |
| 623 |
// next 3 bytes define subtype |
| 624 |
if (typeBits <= 6) { // VInt (zigzag), BigInteger |
| 625 |
_tokenIncomplete = true; |
| 626 |
return (_currToken = JsonToken.VALUE_NUMBER_INT); |
| 627 |
} |
| 628 |
if (typeBits < 11 && typeBits != 7) { // floating-point |
| 629 |
_got32BitFloat = (typeBits == 8); |
| 630 |
_tokenIncomplete = true; |
| 631 |
return (_currToken = JsonToken.VALUE_NUMBER_FLOAT); |
| 632 |
} |
| 633 |
if (typeBits == 0x1A) { // == 0x3A == ':' -> possibly header signature for next chunk? |
| 634 |
if (handleSignature(false, false)) { |
| 635 |
/* Ok, now; end-marker and header both imply doc boundary and a |
| 636 |
* 'null token'; but if both are seen, they are collapsed. |
| 637 |
* We can check this by looking at current token; if it's null, |
| 638 |
* need to get non-null token |
| 639 |
*/ |
| 640 |
if (_currToken == null) { |
| 641 |
return nextToken(); |
| 642 |
} |
| 643 |
return (_currToken = null); |
| 644 |
} |
| 645 |
_reportError("Unrecognized token byte 0x3A (malformed segment header?"); |
| 646 |
} |
| 647 |
} |
| 648 |
// and everything else is reserved, for now |
| 649 |
break; |
| 650 |
case 2: // tiny ASCII |
| 651 |
// fall through |
| 652 |
case 3: // short ASCII |
| 653 |
// fall through |
| 654 |
case 4: // tiny Unicode |
| 655 |
// fall through |
| 656 |
case 5: // short Unicode |
| 657 |
// No need to decode, unless we have to keep track of back-references (for shared string values) |
| 658 |
if (_seenStringValueCount >= 0) { // shared text values enabled |
| 659 |
return _addSeenStringValue(); |
| 660 |
} |
| 661 |
_tokenIncomplete = true; |
| 662 |
return (_currToken = JsonToken.VALUE_STRING); |
| 663 |
case 6: // small integers; zigzag encoded |
| 664 |
_numberInt = SmileUtil.zigzagDecode(ch & 0x1F); |
| 665 |
_numTypesValid = NR_INT; |
| 666 |
return (_currToken = JsonToken.VALUE_NUMBER_INT); |
| 667 |
case 7: // binary/long-text/long-shared/start-end-markers |
| 668 |
switch (ch & 0x1F) { |
| 669 |
case 0x00: // long variable length ASCII |
| 670 |
case 0x04: // long variable length unicode |
| 671 |
_tokenIncomplete = true; |
| 672 |
return (_currToken = JsonToken.VALUE_STRING); |
| 673 |
case 0x08: // binary, 7-bit (0xE8) |
| 674 |
_tokenIncomplete = true; |
| 675 |
return (_currToken = JsonToken.VALUE_EMBEDDED_OBJECT); |
| 676 |
case 0x0C: // long shared string (0xEC) |
| 677 |
case 0x0D: |
| 678 |
case 0x0E: |
| 679 |
case 0x0F: |
| 680 |
if (_inputPtr >= _inputEnd) { |
| 681 |
loadMoreGuaranteed(); |
| 682 |
} |
| 683 |
return _handleSharedString(((ch & 0x3) << 8) + (_inputBuffer[_inputPtr++] & 0xFF)); |
| 684 |
case 0x18: // START_ARRAY |
| 685 |
_parsingContext = _parsingContext.createChildArrayContext(-1, -1); |
| 686 |
return (_currToken = JsonToken.START_ARRAY); |
| 687 |
case 0x19: // END_ARRAY |
| 688 |
if (!_parsingContext.inArray()) { |
| 689 |
_reportMismatchedEndMarker(']', '}'); |
| 690 |
} |
| 691 |
_parsingContext = _parsingContext.getParent(); |
| 692 |
return (_currToken = JsonToken.END_ARRAY); |
| 693 |
case 0x1A: // START_OBJECT |
| 694 |
_parsingContext = _parsingContext.createChildObjectContext(-1, -1); |
| 695 |
return (_currToken = JsonToken.START_OBJECT); |
| 696 |
case 0x1B: // not used in this mode; would be END_OBJECT |
| 697 |
_reportError("Invalid type marker byte 0xFB in value mode (would be END_OBJECT in key mode)"); |
| 698 |
case 0x1D: // binary, raw |
| 699 |
_tokenIncomplete = true; |
| 700 |
return (_currToken = JsonToken.VALUE_EMBEDDED_OBJECT); |
| 701 |
case 0x1F: // 0xFF, end of content |
| 702 |
return (_currToken = null); |
| 703 |
} |
| 704 |
break; |
| 705 |
} |
| 706 |
// If we get this far, type byte is corrupt |
| 707 |
_reportError("Invalid type marker byte 0x"+Integer.toHexString(ch & 0xFF)+" for expected value token"); |
| 708 |
return null; |
| 709 |
} |
| 710 |
|
| 711 |
private final JsonToken _handleSharedString(int index) throws IOException |
| 712 |
{ |
| 713 |
if (index >= _seenStringValueCount) { |
| 714 |
_reportInvalidSharedStringValue(index); |
| 715 |
} |
| 716 |
_textBuffer.resetWithString(_seenStringValues[index]); |
| 717 |
return (_currToken = JsonToken.VALUE_STRING); |
| 718 |
} |
| 719 |
|
| 720 |
private final JsonToken _addSeenStringValue() throws IOException |
| 721 |
{ |
| 722 |
_finishToken(); |
| 723 |
String v = _textBuffer.contentsAsString(); |
| 724 |
if (_seenStringValueCount < _seenStringValues.length) { |
| 725 |
// !!! TODO: actually only store char[], first time around? |
| 726 |
_seenStringValues[_seenStringValueCount++] = v; |
| 727 |
} else { |
| 728 |
_expandSeenStringValues(v); |
| 729 |
} |
| 730 |
return (_currToken = JsonToken.VALUE_STRING); |
| 731 |
} |
| 732 |
|
| 733 |
private final void _expandSeenStringValues(String newText) |
| 734 |
{ |
| 735 |
String[] oldShared = _seenStringValues; |
| 736 |
int len = oldShared.length; |
| 737 |
String[] newShared; |
| 738 |
if (len == 0) { |
| 739 |
newShared = _smileBufferRecycler.allocSeenStringValuesBuffer(); |
| 740 |
if (newShared == null) { |
| 741 |
newShared = new String[SmileBufferRecycler.DEFAULT_STRING_VALUE_BUFFER_LENGTH]; |
| 742 |
} |
| 743 |
} else if (len == SmileConstants.MAX_SHARED_STRING_VALUES) { // too many? Just flush... |
| 744 |
newShared = oldShared; |
| 745 |
_seenStringValueCount = 0; // could also clear, but let's not yet bother |
| 746 |
} else { |
| 747 |
int newSize = (len == SmileBufferRecycler.DEFAULT_NAME_BUFFER_LENGTH) ? 256 : SmileConstants.MAX_SHARED_STRING_VALUES; |
| 748 |
newShared = Arrays.copyOf(oldShared, newSize); |
| 749 |
} |
| 750 |
_seenStringValues = newShared; |
| 751 |
_seenStringValues[_seenStringValueCount++] = newText; |
| 752 |
} |
| 753 |
|
| 754 |
// base impl is fine: |
| 755 |
//public String getCurrentName() throws IOException |
| 756 |
|
| 757 |
@Override |
| 758 |
public NumberType getNumberType() throws IOException { |
| 759 |
if (_got32BitFloat && _currToken == JsonToken.VALUE_NUMBER_FLOAT) { |
| 760 |
return NumberType.FLOAT; |
| 761 |
} |
| 762 |
return super.getNumberType(); |
| 763 |
} |
| 764 |
|
| 765 |
/* |
| 766 |
/********************************************************** |
| 767 |
/* Optimized accessors, isXxx, nextXxx (except for nextToken() |
| 768 |
/********************************************************** |
| 769 |
*/ |
| 770 |
|
| 771 |
// Not (yet?) overridden, as of 2.6 |
| 772 |
/* |
| 773 |
public boolean hasTokenId(int id) { |
| 774 |
return super.hasTokenId(id); |
| 775 |
} |
| 776 |
*/ |
| 777 |
|
| 778 |
//public boolean isExpectedStartArrayToken() { return getCurrentToken() == JsonToken.START_ARRAY; } |
| 779 |
|
| 780 |
//public boolean isExpectedStartObjectToken() { return getCurrentToken() == JsonToken.START_OBJECT; } |
| 781 |
|
| 782 |
@Override |
| 783 |
public boolean nextFieldName(SerializableString str) throws IOException |
| 784 |
{ |
| 785 |
// Two parsing modes; can only succeed if expecting field name, so handle that first: |
| 786 |
if (_currToken != JsonToken.FIELD_NAME && _parsingContext.inObject()) { |
| 787 |
// first, clear up state |
| 788 |
_numTypesValid = NR_UNKNOWN; |
| 789 |
if (_tokenIncomplete) { |
| 790 |
_skipIncomplete(); |
| 791 |
} |
| 792 |
_tokenOffsetForTotal = _inputPtr; |
| 793 |
_binaryValue = null; |
| 794 |
|
| 795 |
byte[] nameBytes = str.asQuotedUTF8(); |
| 796 |
final int byteLen = nameBytes.length; |
| 797 |
// need room for type byte, name bytes, possibly end marker, so: |
| 798 |
if ((_inputPtr + byteLen + 1) < _inputEnd) { // maybe... |
| 799 |
int ptr = _inputPtr; |
| 800 |
int ch = _inputBuffer[ptr++] & 0xFF; |
| 801 |
|
| 802 |
_typeAsInt = ch; |
| 803 |
main_switch: |
| 804 |
switch (ch >> 6) { |
| 805 |
case 0: // misc, including end marker |
| 806 |
switch (ch) { |
| 807 |
case 0x20: // empty String as name, legal if unusual |
| 808 |
_currToken = JsonToken.FIELD_NAME; |
| 809 |
_inputPtr = ptr; |
| 810 |
_parsingContext.setCurrentName(""); |
| 811 |
return (byteLen == 0); |
| 812 |
case 0x30: // long shared |
| 813 |
case 0x31: |
| 814 |
case 0x32: |
| 815 |
case 0x33: |
| 816 |
{ |
| 817 |
int index = ((ch & 0x3) << 8) + (_inputBuffer[ptr++] & 0xFF); |
| 818 |
if (index >= _seenNameCount) { |
| 819 |
_reportInvalidSharedName(index); |
| 820 |
} |
| 821 |
String name = _seenNames[index]; |
| 822 |
_parsingContext.setCurrentName(name); |
| 823 |
_inputPtr = ptr; |
| 824 |
_currToken = JsonToken.FIELD_NAME; |
| 825 |
return (name.equals(str.getValue())); |
| 826 |
} |
| 827 |
//case 0x34: // long ASCII/Unicode name; let's not even try... |
| 828 |
} |
| 829 |
break; |
| 830 |
case 1: // short shared, can fully process |
| 831 |
{ |
| 832 |
int index = (ch & 0x3F); |
| 833 |
if (index >= _seenNameCount) { |
| 834 |
_reportInvalidSharedName(index); |
| 835 |
} |
| 836 |
_parsingContext.setCurrentName(_seenNames[index]); |
| 837 |
String name = _seenNames[index]; |
| 838 |
_parsingContext.setCurrentName(name); |
| 839 |
_inputPtr = ptr; |
| 840 |
_currToken = JsonToken.FIELD_NAME; |
| 841 |
return (name.equals(str.getValue())); |
| 842 |
} |
| 843 |
case 2: // short ASCII |
| 844 |
{ |
| 845 |
int len = 1 + (ch & 0x3f); |
| 846 |
if (len == byteLen) { |
| 847 |
int i = 0; |
| 848 |
for (; i < len; ++i) { |
| 849 |
if (nameBytes[i] != _inputBuffer[ptr+i]) { |
| 850 |
break main_switch; |
| 851 |
} |
| 852 |
} |
| 853 |
// yes, does match... |
| 854 |
_inputPtr = ptr + len; |
| 855 |
final String name = str.getValue(); |
| 856 |
if (_seenNames != null) { |
| 857 |
if (_seenNameCount >= _seenNames.length) { |
| 858 |
_seenNames = _expandSeenNames(_seenNames); |
| 859 |
} |
| 860 |
_seenNames[_seenNameCount++] = name; |
| 861 |
} |
| 862 |
_parsingContext.setCurrentName(name); |
| 863 |
_currToken = JsonToken.FIELD_NAME; |
| 864 |
return true; |
| 865 |
} |
| 866 |
} |
| 867 |
break; |
| 868 |
case 3: // short Unicode |
| 869 |
// all valid, except for 0xFF |
| 870 |
{ |
| 871 |
int len = (ch & 0x3F); |
| 872 |
if (len > 0x37) { |
| 873 |
if (len == 0x3B) { |
| 874 |
_currToken = JsonToken.END_OBJECT; |
| 875 |
if (!_parsingContext.inObject()) { |
| 876 |
_reportMismatchedEndMarker('}', ']'); |
| 877 |
} |
| 878 |
_inputPtr = ptr; |
| 879 |
_parsingContext = _parsingContext.getParent(); |
| 880 |
return false; |
| 881 |
} |
| 882 |
// error, but let's not worry about that here |
| 883 |
break; |
| 884 |
} |
| 885 |
len += 2; // values from 2 to 57... |
| 886 |
if (len == byteLen) { |
| 887 |
int i = 0; |
| 888 |
for (; i < len; ++i) { |
| 889 |
if (nameBytes[i] != _inputBuffer[ptr+i]) { |
| 890 |
break main_switch; |
| 891 |
} |
| 892 |
} |
| 893 |
// yes, does match... |
| 894 |
_inputPtr = ptr + len; |
| 895 |
final String name = str.getValue(); |
| 896 |
if (_seenNames != null) { |
| 897 |
if (_seenNameCount >= _seenNames.length) { |
| 898 |
_seenNames = _expandSeenNames(_seenNames); |
| 899 |
} |
| 900 |
_seenNames[_seenNameCount++] = name; |
| 901 |
} |
| 902 |
_parsingContext.setCurrentName(name); |
| 903 |
_currToken = JsonToken.FIELD_NAME; |
| 904 |
return true; |
| 905 |
} |
| 906 |
} |
| 907 |
break; |
| 908 |
} |
| 909 |
} |
| 910 |
// wouldn't fit in buffer, just fall back to default processing |
| 911 |
} |
| 912 |
// otherwise just fall back to default handling; should occur rarely |
| 913 |
return (nextToken() == JsonToken.FIELD_NAME) && str.getValue().equals(getCurrentName()); |
| 914 |
} |
| 915 |
|
| 916 |
@Override |
| 917 |
public String nextFieldName() throws IOException |
| 918 |
{ |
| 919 |
// Two parsing modes; can only succeed if expecting field name, so handle that first: |
| 920 |
if (_currToken != JsonToken.FIELD_NAME && _parsingContext.inObject()) { |
| 921 |
// first, clear up state |
| 922 |
_numTypesValid = NR_UNKNOWN; |
| 923 |
if (_tokenIncomplete) { |
| 924 |
_skipIncomplete(); |
| 925 |
} |
| 926 |
_tokenOffsetForTotal = _inputPtr; |
| 927 |
_binaryValue = null; |
| 928 |
|
| 929 |
if (_inputPtr >= _inputEnd) { |
| 930 |
loadMoreGuaranteed(); |
| 931 |
} |
| 932 |
int ch = _inputBuffer[_inputPtr++] & 0xFF; |
| 933 |
// is this needed? |
| 934 |
_typeAsInt = ch; |
| 935 |
switch (ch >> 6) { |
| 936 |
case 0: // misc, including end marker |
| 937 |
switch (ch) { |
| 938 |
case 0x20: // empty String as name, legal if unusual |
| 939 |
_parsingContext.setCurrentName(""); |
| 940 |
_currToken = JsonToken.FIELD_NAME; |
| 941 |
return ""; |
| 942 |
case 0x30: // long shared |
| 943 |
case 0x31: |
| 944 |
case 0x32: |
| 945 |
case 0x33: |
| 946 |
if (_inputPtr >= _inputEnd) { |
| 947 |
loadMoreGuaranteed(); |
| 948 |
} |
| 949 |
{ |
| 950 |
int index = ((ch & 0x3) << 8) + (_inputBuffer[_inputPtr++] & 0xFF); |
| 951 |
if (index >= _seenNameCount) { |
| 952 |
_reportInvalidSharedName(index); |
| 953 |
} |
| 954 |
String name = _seenNames[index]; |
| 955 |
_parsingContext.setCurrentName(name); |
| 956 |
_currToken = JsonToken.FIELD_NAME; |
| 957 |
return name; |
| 958 |
} |
| 959 |
case 0x34: // long ASCII/Unicode name |
| 960 |
_handleLongFieldName(); |
| 961 |
_currToken = JsonToken.FIELD_NAME; |
| 962 |
return getCurrentName(); |
| 963 |
} |
| 964 |
break; |
| 965 |
case 1: // short shared, can fully process |
| 966 |
{ |
| 967 |
int index = (ch & 0x3F); |
| 968 |
if (index >= _seenNameCount) { |
| 969 |
_reportInvalidSharedName(index); |
| 970 |
} |
| 971 |
String name = _seenNames[index]; |
| 972 |
_parsingContext.setCurrentName(name); |
| 973 |
_currToken = JsonToken.FIELD_NAME; |
| 974 |
return name; |
| 975 |
} |
| 976 |
case 2: // short ASCII |
| 977 |
{ |
| 978 |
int len = 1 + (ch & 0x3f); |
| 979 |
String name = _findDecodedFromSymbols(len); |
| 980 |
if (name != null) { |
| 981 |
_inputPtr += len; |
| 982 |
} else { |
| 983 |
name = _decodeShortAsciiName(len); |
| 984 |
name = _addDecodedToSymbols(len, name); |
| 985 |
} |
| 986 |
if (_seenNames != null) { |
| 987 |
if (_seenNameCount >= _seenNames.length) { |
| 988 |
_seenNames = _expandSeenNames(_seenNames); |
| 989 |
} |
| 990 |
_seenNames[_seenNameCount++] = name; |
| 991 |
} |
| 992 |
_parsingContext.setCurrentName(name); |
| 993 |
_currToken = JsonToken.FIELD_NAME; |
| 994 |
return name; |
| 995 |
} |
| 996 |
case 3: // short Unicode |
| 997 |
// all valid, except for 0xFF |
| 998 |
ch &= 0x3F; |
| 999 |
{ |
| 1000 |
if (ch > 0x37) { |
| 1001 |
if (ch == 0x3B) { |
| 1002 |
if (!_parsingContext.inObject()) { |
| 1003 |
_reportMismatchedEndMarker('}', ']'); |
| 1004 |
} |
| 1005 |
_parsingContext = _parsingContext.getParent(); |
| 1006 |
_currToken = JsonToken.END_OBJECT; |
| 1007 |
return null; |
| 1008 |
} |
| 1009 |
} else { |
| 1010 |
final int len = ch + 2; // values from 2 to 57... |
| 1011 |
String name = _findDecodedFromSymbols(len); |
| 1012 |
if (name != null) { |
| 1013 |
_inputPtr += len; |
| 1014 |
} else { |
| 1015 |
name = _decodeShortUnicodeName(len); |
| 1016 |
name = _addDecodedToSymbols(len, name); |
| 1017 |
} |
| 1018 |
if (_seenNames != null) { |
| 1019 |
if (_seenNameCount >= _seenNames.length) { |
| 1020 |
_seenNames = _expandSeenNames(_seenNames); |
| 1021 |
} |
| 1022 |
_seenNames[_seenNameCount++] = name; |
| 1023 |
} |
| 1024 |
_parsingContext.setCurrentName(name); |
| 1025 |
_currToken = JsonToken.FIELD_NAME; |
| 1026 |
return name; |
| 1027 |
} |
| 1028 |
} |
| 1029 |
break; |
| 1030 |
} |
| 1031 |
// Other byte values are illegal |
| 1032 |
_reportError("Invalid type marker byte 0x"+Integer.toHexString(_typeAsInt)+" for expected field name (or END_OBJECT marker)"); |
| 1033 |
return null; |
| 1034 |
} |
| 1035 |
|
| 1036 |
// otherwise just fall back to default handling; should occur rarely |
| 1037 |
return (nextToken() == JsonToken.FIELD_NAME) ? getCurrentName() : null; |
| 1038 |
} |
| 1039 |
|
| 1040 |
@Override |
| 1041 |
public String nextTextValue() throws IOException |
| 1042 |
{ |
| 1043 |
// can't get text value if expecting name, so |
| 1044 |
if (!_parsingContext.inObject() || _currToken == JsonToken.FIELD_NAME) { |
| 1045 |
if (_tokenIncomplete) { |
| 1046 |
_skipIncomplete(); |
| 1047 |
} |
| 1048 |
int ptr = _inputPtr; |
| 1049 |
if (ptr >= _inputEnd) { |
| 1050 |
if (!loadMore()) { |
| 1051 |
_eofAsNextToken(); |
| 1052 |
return null; |
| 1053 |
} |
| 1054 |
ptr = _inputPtr; |
| 1055 |
} |
| 1056 |
_tokenOffsetForTotal = ptr; |
| 1057 |
// _tokenInputTotal = _currInputProcessed + _inputPtr; |
| 1058 |
int ch = _inputBuffer[ptr++] & 0xFF; |
| 1059 |
_typeAsInt = ch; |
| 1060 |
|
| 1061 |
// also: clear any data retained so far |
| 1062 |
_binaryValue = null; |
| 1063 |
|
| 1064 |
switch (ch >> 5) { |
| 1065 |
case 0: // short shared string value reference |
| 1066 |
if (ch != 0) { |
| 1067 |
// _handleSharedString... |
| 1068 |
--ch; |
| 1069 |
if (ch >= _seenStringValueCount) { |
| 1070 |
_reportInvalidSharedStringValue(ch); |
| 1071 |
} |
| 1072 |
_inputPtr = ptr; |
| 1073 |
String text = _seenStringValues[ch]; |
| 1074 |
_textBuffer.resetWithString(text); |
| 1075 |
_currToken = JsonToken.VALUE_STRING; |
| 1076 |
return text; |
| 1077 |
} else { |
| 1078 |
// important: this is invalid, don't accept |
| 1079 |
_reportError("Invalid token byte 0x00"); |
| 1080 |
} |
| 1081 |
|
| 1082 |
case 1: // simple literals, numbers |
| 1083 |
{ |
| 1084 |
int typeBits = ch & 0x1F; |
| 1085 |
if (typeBits == 0x00) { |
| 1086 |
_inputPtr = ptr; |
| 1087 |
_textBuffer.resetWithEmpty(); |
| 1088 |
_currToken = JsonToken.VALUE_STRING; |
| 1089 |
return ""; |
| 1090 |
} |
| 1091 |
} |
| 1092 |
break; |
| 1093 |
case 2: // tiny ASCII |
| 1094 |
// fall through |
| 1095 |
case 3: // short ASCII |
| 1096 |
_currToken = JsonToken.VALUE_STRING; |
| 1097 |
_inputPtr = ptr; |
| 1098 |
{ |
| 1099 |
final String text = _decodeShortAsciiValue(1 + (ch & 0x3F)); |
| 1100 |
if (_seenStringValueCount >= 0) { // shared text values enabled |
| 1101 |
if (_seenStringValueCount < _seenStringValues.length) { |
| 1102 |
_seenStringValues[_seenStringValueCount++] = text; |
| 1103 |
} else { |
| 1104 |
_expandSeenStringValues(text); |
| 1105 |
} |
| 1106 |
} |
| 1107 |
return text; |
| 1108 |
} |
| 1109 |
|
| 1110 |
case 4: // tiny Unicode |
| 1111 |
// fall through |
| 1112 |
case 5: // short Unicode |
| 1113 |
_currToken = JsonToken.VALUE_STRING; |
| 1114 |
_inputPtr = ptr; |
| 1115 |
{ |
| 1116 |
final String text = _decodeShortUnicodeValue(2 + (ch & 0x3F)); |
| 1117 |
if (_seenStringValueCount >= 0) { // shared text values enabled |
| 1118 |
if (_seenStringValueCount < _seenStringValues.length) { |
| 1119 |
_seenStringValues[_seenStringValueCount++] = text; |
| 1120 |
} else { |
| 1121 |
_expandSeenStringValues(text); |
| 1122 |
} |
| 1123 |
} |
| 1124 |
return text; |
| 1125 |
} |
| 1126 |
case 6: // small integers; zigzag encoded |
| 1127 |
break; |
| 1128 |
case 7: // binary/long-text/long-shared/start-end-markers |
| 1129 |
// TODO: support longer strings too? |
| 1130 |
/* |
| 1131 |
switch (ch & 0x1F) { |
| 1132 |
case 0x00: // long variable length ASCII |
| 1133 |
case 0x04: // long variable length unicode |
| 1134 |
_tokenIncomplete = true; |
| 1135 |
return (_currToken = JsonToken.VALUE_STRING); |
| 1136 |
case 0x08: // binary, 7-bit |
| 1137 |
break main; |
| 1138 |
case 0x0C: // long shared string |
| 1139 |
case 0x0D: |
| 1140 |
case 0x0E: |
| 1141 |
case 0x0F: |
| 1142 |
if (_inputPtr >= _inputEnd) { |
| 1143 |
loadMoreGuaranteed(); |
| 1144 |
} |
| 1145 |
return _handleSharedString(((ch & 0x3) << 8) + (_inputBuffer[_inputPtr++] & 0xFF)); |
| 1146 |
} |
| 1147 |
break; |
| 1148 |
*/ |
| 1149 |
break; |
| 1150 |
} |
| 1151 |
} |
| 1152 |
// otherwise fall back to generic handling (note: we do NOT assign 'ptr') |
| 1153 |
return (nextToken() == JsonToken.VALUE_STRING) ? getText() : null; |
| 1154 |
} |
| 1155 |
|
| 1156 |
@Override |
| 1157 |
public int nextIntValue(int defaultValue) throws IOException |
| 1158 |
{ |
| 1159 |
if (nextToken() == JsonToken.VALUE_NUMBER_INT) { |
| 1160 |
return getIntValue(); |
| 1161 |
} |
| 1162 |
return defaultValue; |
| 1163 |
} |
| 1164 |
|
| 1165 |
@Override |
| 1166 |
public long nextLongValue(long defaultValue) throws IOException |
| 1167 |
{ |
| 1168 |
if (nextToken() == JsonToken.VALUE_NUMBER_INT) { |
| 1169 |
return getLongValue(); |
| 1170 |
} |
| 1171 |
return defaultValue; |
| 1172 |
} |
| 1173 |
|
| 1174 |
@Override |
| 1175 |
public Boolean nextBooleanValue() |
| 1176 |
throws IOException |
| 1177 |
{ |
| 1178 |
switch (nextToken()) { |
| 1179 |
case VALUE_TRUE: |
| 1180 |
return Boolean.TRUE; |
| 1181 |
case VALUE_FALSE: |
| 1182 |
return Boolean.FALSE; |
| 1183 |
default: |
| 1184 |
return null; |
| 1185 |
} |
| 1186 |
} |
| 1187 |
|
| 1188 |
/* |
| 1189 |
/********************************************************** |
| 1190 |
/* Public API, access to token information, text |
| 1191 |
/********************************************************** |
| 1192 |
*/ |
| 1193 |
|
| 1194 |
/** |
| 1195 |
* Method for accessing textual representation of the current event; |
| 1196 |
* if no current event (before first call to {@link #nextToken}, or |
| 1197 |
* after encountering end-of-input), returns null. |
| 1198 |
* Method can be called for any event. |
| 1199 |
*/ |
| 1200 |
@Override |
| 1201 |
public String getText() throws IOException |
| 1202 |
{ |
| 1203 |
if (_tokenIncomplete) { |
| 1204 |
_tokenIncomplete = false; |
| 1205 |
// Let's inline part of "_finishToken", common case |
| 1206 |
int tb = _typeAsInt; |
| 1207 |
int type = (tb >> 5); |
| 1208 |
if (type == 2 || type == 3) { // tiny & short ASCII |
| 1209 |
return _decodeShortAsciiValue(1 + (tb & 0x3F)); |
| 1210 |
} |
| 1211 |
if (type == 4 || type == 5) { // tiny & short Unicode |
| 1212 |
// short unicode; note, lengths 2 - 65 (off-by-one compared to ASCII) |
| 1213 |
return _decodeShortUnicodeValue(2 + (tb & 0x3F)); |
| 1214 |
} |
| 1215 |
_finishToken(); |
| 1216 |
} |
| 1217 |
if (_currToken == JsonToken.VALUE_STRING) { |
| 1218 |
return _textBuffer.contentsAsString(); |
| 1219 |
} |
| 1220 |
JsonToken t = _currToken; |
| 1221 |
if (t == null) { // null only before/after document |
| 1222 |
return null; |
| 1223 |
} |
| 1224 |
if (t == JsonToken.FIELD_NAME) { |
| 1225 |
return _parsingContext.getCurrentName(); |
| 1226 |
} |
| 1227 |
if (t.isNumeric()) { // TODO: optimize? |
| 1228 |
return getNumberValue().toString(); |
| 1229 |
} |
| 1230 |
return _currToken.asString(); |
| 1231 |
} |
| 1232 |
|
| 1233 |
@Override |
| 1234 |
public char[] getTextCharacters() throws IOException |
| 1235 |
{ |
| 1236 |
if (_currToken != null) { // null only before/after document |
| 1237 |
if (_tokenIncomplete) { |
| 1238 |
_finishToken(); |
| 1239 |
} |
| 1240 |
if (_currToken == JsonToken.VALUE_STRING) { |
| 1241 |
return _textBuffer.getTextBuffer(); |
| 1242 |
} |
| 1243 |
if (_currToken == JsonToken.FIELD_NAME) { |
| 1244 |
if (!_nameCopied) { |
| 1245 |
String name = _parsingContext.getCurrentName(); |
| 1246 |
int nameLen = name.length(); |
| 1247 |
if (_nameCopyBuffer == null) { |
| 1248 |
_nameCopyBuffer = _ioContext.allocNameCopyBuffer(nameLen); |
| 1249 |
} else if (_nameCopyBuffer.length < nameLen) { |
| 1250 |
_nameCopyBuffer = new char[nameLen]; |
| 1251 |
} |
| 1252 |
name.getChars(0, nameLen, _nameCopyBuffer, 0); |
| 1253 |
_nameCopied = true; |
| 1254 |
} |
| 1255 |
return _nameCopyBuffer; |
| 1256 |
} |
| 1257 |
if (_currToken.isNumeric()) { // TODO: optimize? |
| 1258 |
return getNumberValue().toString().toCharArray(); |
| 1259 |
} |
| 1260 |
return _currToken.asCharArray(); |
| 1261 |
} |
| 1262 |
return null; |
| 1263 |
} |
| 1264 |
|
| 1265 |
@Override |
| 1266 |
public int getTextLength() throws IOException |
| 1267 |
{ |
| 1268 |
if (_currToken != null) { // null only before/after document |
| 1269 |
if (_tokenIncomplete) { |
| 1270 |
_finishToken(); |
| 1271 |
} |
| 1272 |
if (_currToken == JsonToken.VALUE_STRING) { |
| 1273 |
return _textBuffer.size(); |
| 1274 |
} |
| 1275 |
switch (_currToken) { |
| 1276 |
case FIELD_NAME: |
| 1277 |
return _parsingContext.getCurrentName().length(); |
| 1278 |
// fall through |
| 1279 |
case VALUE_NUMBER_INT: |
| 1280 |
case VALUE_NUMBER_FLOAT: |
| 1281 |
// TODO: optimize |
| 1282 |
return getNumberValue().toString().length(); |
| 1283 |
|
| 1284 |
default: |
| 1285 |
return _currToken.asCharArray().length; |
| 1286 |
} |
| 1287 |
} |
| 1288 |
return 0; |
| 1289 |
} |
| 1290 |
|
| 1291 |
@Override |
| 1292 |
public int getTextOffset() throws IOException { |
| 1293 |
return 0; |
| 1294 |
} |
| 1295 |
|
| 1296 |
@Override |
| 1297 |
public String getValueAsString() throws IOException |
| 1298 |
{ |
| 1299 |
// inlined 'getText()' for common case of having String |
| 1300 |
if (_tokenIncomplete) { |
| 1301 |
_tokenIncomplete = false; |
| 1302 |
int tb = _typeAsInt; |
| 1303 |
int type = (tb >> 5); |
| 1304 |
if (type == 2 || type == 3) { // tiny & short ASCII |
| 1305 |
return _decodeShortAsciiValue(1 + (tb & 0x3F)); |
| 1306 |
} |
| 1307 |
if (type == 4 || type == 5) { // tiny & short Unicode |
| 1308 |
return _decodeShortUnicodeValue(2 + (tb & 0x3F)); |
| 1309 |
} |
| 1310 |
_finishToken(); |
| 1311 |
} |
| 1312 |
if (_currToken == JsonToken.VALUE_STRING) { |
| 1313 |
return _textBuffer.contentsAsString(); |
| 1314 |
} |
| 1315 |
if (_currToken == null || _currToken == JsonToken.VALUE_NULL || !_currToken.isScalarValue()) { |
| 1316 |
return null; |
| 1317 |
} |
| 1318 |
return getText(); |
| 1319 |
} |
| 1320 |
|
| 1321 |
@Override |
| 1322 |
public String getValueAsString(String defaultValue) throws IOException |
| 1323 |
{ |
| 1324 |
if (_currToken != JsonToken.VALUE_STRING) { |
| 1325 |
if (_currToken == null || _currToken == JsonToken.VALUE_NULL || !_currToken.isScalarValue()) { |
| 1326 |
return defaultValue; |
| 1327 |
} |
| 1328 |
} |
| 1329 |
return getText(); |
| 1330 |
} |
| 1331 |
|
| 1332 |
/* |
| 1333 |
/********************************************************** |
| 1334 |
/* Public API, access to token information, binary |
| 1335 |
/********************************************************** |
| 1336 |
*/ |
| 1337 |
|
| 1338 |
@Override |
| 1339 |
public byte[] getBinaryValue(Base64Variant b64variant) throws IOException |
| 1340 |
{ |
| 1341 |
if (_tokenIncomplete) { |
| 1342 |
_finishToken(); |
| 1343 |
} |
| 1344 |
if (_currToken != JsonToken.VALUE_EMBEDDED_OBJECT) { |
| 1345 |
// Todo, maybe: support base64 for text? |
| 1346 |
_reportError("Current token ("+_currToken+") not VALUE_EMBEDDED_OBJECT, can not access as binary"); |
| 1347 |
} |
| 1348 |
return _binaryValue; |
| 1349 |
} |
| 1350 |
|
| 1351 |
@Override |
| 1352 |
public Object getEmbeddedObject() throws IOException |
| 1353 |
{ |
| 1354 |
if (_tokenIncomplete) { |
| 1355 |
_finishToken(); |
| 1356 |
} |
| 1357 |
if (_currToken == JsonToken.VALUE_EMBEDDED_OBJECT ) { |
| 1358 |
return _binaryValue; |
| 1359 |
} |
| 1360 |
return null; |
| 1361 |
} |
| 1362 |
|
| 1363 |
@Override |
| 1364 |
public int readBinaryValue(Base64Variant b64variant, OutputStream out) throws IOException |
| 1365 |
{ |
| 1366 |
if (_currToken != JsonToken.VALUE_EMBEDDED_OBJECT ) { |
| 1367 |
// Todo, maybe: support base64 for text? |
| 1368 |
_reportError("Current token ("+_currToken+") not VALUE_EMBEDDED_OBJECT, can not access as binary"); |
| 1369 |
} |
| 1370 |
// Ok, first, unlikely (but legal?) case where someone already requested binary data: |
| 1371 |
if (!_tokenIncomplete) { |
| 1372 |
if (_binaryValue == null) { // most likely already read... |
| 1373 |
return 0; |
| 1374 |
} |
| 1375 |
final int len = _binaryValue.length; |
| 1376 |
out.write(_binaryValue, 0, len); |
| 1377 |
return len; |
| 1378 |
} |
| 1379 |
|
| 1380 |
// otherwise, handle, mark as complete |
| 1381 |
// first, raw inlined binary data (simple) |
| 1382 |
if (_typeAsInt == SmileConstants.INT_MISC_BINARY_RAW) { |
| 1383 |
final int totalCount = _readUnsignedVInt(); |
| 1384 |
int left = totalCount; |
| 1385 |
while (left > 0) { |
| 1386 |
int avail = _inputEnd - _inputPtr; |
| 1387 |
if (_inputPtr >= _inputEnd) { |
| 1388 |
loadMoreGuaranteed(); |
| 1389 |
avail = _inputEnd - _inputPtr; |
| 1390 |
} |
| 1391 |
int count = Math.min(avail, left); |
| 1392 |
out.write(_inputBuffer, _inputPtr, count); |
| 1393 |
_inputPtr += count; |
| 1394 |
left -= count; |
| 1395 |
} |
| 1396 |
_tokenIncomplete = false; |
| 1397 |
return totalCount; |
| 1398 |
} |
| 1399 |
if (_typeAsInt != SmileConstants.INT_MISC_BINARY_7BIT) { |
| 1400 |
_throwInternal(); |
| 1401 |
} |
| 1402 |
// or, alternative, 7-bit encoded stuff: |
| 1403 |
final int totalCount = _readUnsignedVInt(); |
| 1404 |
byte[] encodingBuffer = _ioContext.allocBase64Buffer(); |
| 1405 |
try { |
| 1406 |
_readBinaryEncoded(out, totalCount, encodingBuffer); |
| 1407 |
} finally { |
| 1408 |
_ioContext.releaseBase64Buffer(encodingBuffer); |
| 1409 |
} |
| 1410 |
_tokenIncomplete = false; |
| 1411 |
return totalCount; |
| 1412 |
} |
| 1413 |
|
| 1414 |
private void _readBinaryEncoded(OutputStream out, int length, byte[] buffer) throws IOException |
| 1415 |
{ |
| 1416 |
int outPtr = 0; |
| 1417 |
final int lastSafeOut = buffer.length - 7; |
| 1418 |
// first handle all full 7/8 units |
| 1419 |
while (length > 7) { |
| 1420 |
if ((_inputEnd - _inputPtr) < 8) { |
| 1421 |
_loadToHaveAtLeast(8); |
| 1422 |
} |
| 1423 |
int i1 = (_inputBuffer[_inputPtr++] << 25) |
| 1424 |
+ (_inputBuffer[_inputPtr++] << 18) |
| 1425 |
+ (_inputBuffer[_inputPtr++] << 11) |
| 1426 |
+ (_inputBuffer[_inputPtr++] << 4); |
| 1427 |
int x = _inputBuffer[_inputPtr++]; |
| 1428 |
i1 += x >> 3; |
| 1429 |
int i2 = ((x & 0x7) << 21) |
| 1430 |
+ (_inputBuffer[_inputPtr++] << 14) |
| 1431 |
+ (_inputBuffer[_inputPtr++] << 7) |
| 1432 |
+ _inputBuffer[_inputPtr++]; |
| 1433 |
// Ok: got our 7 bytes, just need to split, copy |
| 1434 |
buffer[outPtr++] = (byte)(i1 >> 24); |
| 1435 |
buffer[outPtr++] = (byte)(i1 >> 16); |
| 1436 |
buffer[outPtr++] = (byte)(i1 >> 8); |
| 1437 |
buffer[outPtr++] = (byte)i1; |
| 1438 |
buffer[outPtr++] = (byte)(i2 >> 16); |
| 1439 |
buffer[outPtr++] = (byte)(i2 >> 8); |
| 1440 |
buffer[outPtr++] = (byte)i2; |
| 1441 |
length -= 7; |
| 1442 |
// ensure there's always room for at least 7 bytes more after looping: |
| 1443 |
if (outPtr > lastSafeOut) { |
| 1444 |
out.write(buffer, 0, outPtr); |
| 1445 |
outPtr = 0; |
| 1446 |
} |
| 1447 |
} |
| 1448 |
// and then leftovers: n+1 bytes to decode n bytes |
| 1449 |
if (length > 0) { |
| 1450 |
if ((_inputEnd - _inputPtr) < (length+1)) { |
| 1451 |
_loadToHaveAtLeast(length+1); |
| 1452 |
} |
| 1453 |
int value = _inputBuffer[_inputPtr++]; |
| 1454 |
for (int i = 1; i < length; ++i) { |
| 1455 |
value = (value << 7) + _inputBuffer[_inputPtr++]; |
| 1456 |
buffer[outPtr++] = (byte) (value >> (7 - i)); |
| 1457 |
} |
| 1458 |
// last byte is different, has remaining 1 - 6 bits, right-aligned |
| 1459 |
value <<= length; |
| 1460 |
buffer[outPtr++] = (byte) (value + _inputBuffer[_inputPtr++]); |
| 1461 |
} |
| 1462 |
if (outPtr > 0) { |
| 1463 |
out.write(buffer, 0, outPtr); |
| 1464 |
} |
| 1465 |
} |
| 1466 |
|
| 1467 |
/* |
| 1468 |
/********************************************************** |
| 1469 |
/* Internal methods, field name parsing |
| 1470 |
/********************************************************** |
| 1471 |
*/ |
| 1472 |
|
| 1473 |
/** |
| 1474 |
* Method that handles initial token type recognition for token |
| 1475 |
* that has to be either FIELD_NAME or END_OBJECT. |
| 1476 |
*/ |
| 1477 |
protected final JsonToken _handleFieldName() throws IOException |
| 1478 |
{ |
| 1479 |
if (_inputPtr >= _inputEnd) { |
| 1480 |
loadMoreGuaranteed(); |
| 1481 |
} |
| 1482 |
int ch = _inputBuffer[_inputPtr++] & 0xFF; |
| 1483 |
// is this needed? |
| 1484 |
_typeAsInt = ch; |
| 1485 |
switch (ch >> 6) { |
| 1486 |
case 0: // misc, including end marker |
| 1487 |
switch (ch) { |
| 1488 |
case 0x20: // empty String as name, legal if unusual |
| 1489 |
_parsingContext.setCurrentName(""); |
| 1490 |
return JsonToken.FIELD_NAME; |
| 1491 |
case 0x30: // long shared |
| 1492 |
case 0x31: |
| 1493 |
case 0x32: |
| 1494 |
case 0x33: |
| 1495 |
if (_inputPtr >= _inputEnd) { |
| 1496 |
loadMoreGuaranteed(); |
| 1497 |
} |
| 1498 |
{ |
| 1499 |
int index = ((ch & 0x3) << 8) + (_inputBuffer[_inputPtr++] & 0xFF); |
| 1500 |
if (index >= _seenNameCount) { |
| 1501 |
_reportInvalidSharedName(index); |
| 1502 |
} |
| 1503 |
_parsingContext.setCurrentName(_seenNames[index]); |
| 1504 |
} |
| 1505 |
return JsonToken.FIELD_NAME; |
| 1506 |
case 0x34: // long ASCII/Unicode name |
| 1507 |
_handleLongFieldName(); |
| 1508 |
return JsonToken.FIELD_NAME; |
| 1509 |
} |
| 1510 |
break; |
| 1511 |
case 1: // short shared, can fully process |
| 1512 |
{ |
| 1513 |
int index = (ch & 0x3F); |
| 1514 |
if (index >= _seenNameCount) { |
| 1515 |
_reportInvalidSharedName(index); |
| 1516 |
} |
| 1517 |
_parsingContext.setCurrentName(_seenNames[index]); |
| 1518 |
} |
| 1519 |
return JsonToken.FIELD_NAME; |
| 1520 |
case 2: // short ASCII |
| 1521 |
{ |
| 1522 |
int len = 1 + (ch & 0x3f); |
| 1523 |
String name = _findDecodedFromSymbols(len); |
| 1524 |
if (name != null) { |
| 1525 |
_inputPtr += len; |
| 1526 |
} else { |
| 1527 |
name = _decodeShortAsciiName(len); |
| 1528 |
name = _addDecodedToSymbols(len, name); |
| 1529 |
} |
| 1530 |
if (_seenNames != null) { |
| 1531 |
if (_seenNameCount >= _seenNames.length) { |
| 1532 |
_seenNames = _expandSeenNames(_seenNames); |
| 1533 |
} |
| 1534 |
_seenNames[_seenNameCount++] = name; |
| 1535 |
} |
| 1536 |
_parsingContext.setCurrentName(name); |
| 1537 |
} |
| 1538 |
return JsonToken.FIELD_NAME; |
| 1539 |
case 3: // short Unicode |
| 1540 |
// all valid, except for 0xFF |
| 1541 |
ch &= 0x3F; |
| 1542 |
{ |
| 1543 |
if (ch > 0x37) { |
| 1544 |
if (ch == 0x3B) { |
| 1545 |
if (!_parsingContext.inObject()) { |
| 1546 |
_reportMismatchedEndMarker('}', ']'); |
| 1547 |
} |
| 1548 |
_parsingContext = _parsingContext.getParent(); |
| 1549 |
return JsonToken.END_OBJECT; |
| 1550 |
} |
| 1551 |
} else { |
| 1552 |
final int len = ch + 2; // values from 2 to 57... |
| 1553 |
String name = _findDecodedFromSymbols(len); |
| 1554 |
if (name != null) { |
| 1555 |
_inputPtr += len; |
| 1556 |
} else { |
| 1557 |
name = _decodeShortUnicodeName(len); |
| 1558 |
name = _addDecodedToSymbols(len, name); |
| 1559 |
} |
| 1560 |
if (_seenNames != null) { |
| 1561 |
if (_seenNameCount >= _seenNames.length) { |
| 1562 |
_seenNames = _expandSeenNames(_seenNames); |
| 1563 |
} |
| 1564 |
_seenNames[_seenNameCount++] = name; |
| 1565 |
} |
| 1566 |
_parsingContext.setCurrentName(name); |
| 1567 |
return JsonToken.FIELD_NAME; |
| 1568 |
} |
| 1569 |
} |
| 1570 |
break; |
| 1571 |
} |
| 1572 |
// Other byte values are illegal |
| 1573 |
_reportError("Invalid type marker byte 0x"+Integer.toHexString(_typeAsInt)+" for expected field name (or END_OBJECT marker)"); |
| 1574 |
return null; |
| 1575 |
} |
| 1576 |
|
| 1577 |
/** |
| 1578 |
* Method called to try to expand shared name area to fit one more potentially |
| 1579 |
* shared String. If area is already at its biggest size, will just clear |
| 1580 |
* the area (by setting next-offset to 0) |
| 1581 |
*/ |
| 1582 |
private final String[] _expandSeenNames(String[] oldShared) |
| 1583 |
{ |
| 1584 |
int len = oldShared.length; |
| 1585 |
String[] newShared; |
| 1586 |
if (len == 0) { |
| 1587 |
newShared = _smileBufferRecycler.allocSeenNamesBuffer(); |
| 1588 |
if (newShared == null) { |
| 1589 |
newShared = new String[SmileBufferRecycler.DEFAULT_NAME_BUFFER_LENGTH]; |
| 1590 |
} |
| 1591 |
} else if (len == SmileConstants.MAX_SHARED_NAMES) { // too many? Just flush... |
| 1592 |
newShared = oldShared; |
| 1593 |
_seenNameCount = 0; // could also clear, but let's not yet bother |
| 1594 |
} else { |
| 1595 |
int newSize = (len == SmileBufferRecycler.DEFAULT_STRING_VALUE_BUFFER_LENGTH) ? 256 : SmileConstants.MAX_SHARED_NAMES; |
| 1596 |
newShared = Arrays.copyOf(oldShared, newSize); |
| 1597 |
} |
| 1598 |
return newShared; |
| 1599 |
} |
| 1600 |
|
| 1601 |
private final String _addDecodedToSymbols(int len, String name) |
| 1602 |
{ |
| 1603 |
if (len < 5) { |
| 1604 |
return _symbols.addName(name, _quad1); |
| 1605 |
} |
| 1606 |
if (len < 9) { |
| 1607 |
return _symbols.addName(name, _quad1, _quad2); |
| 1608 |
} |
| 1609 |
if (len < 13) { |
| 1610 |
return _symbols.addName(name, _quad1, _quad2, _quad3); |
| 1611 |
} |
| 1612 |
int qlen = (len + 3) >> 2; |
| 1613 |
return _symbols.addName(name, _quadBuffer, qlen); |
| 1614 |
} |
| 1615 |
|
| 1616 |
private final String _decodeShortAsciiName(int len) throws IOException |
| 1617 |
{ |
| 1618 |
// note: caller ensures we have enough bytes available |
| 1619 |
// also note that since it's a short name (64 bytes), segment WILL have enough space |
| 1620 |
char[] outBuf = _textBuffer.emptyAndGetCurrentSegment(); |
| 1621 |
int outPtr = 0; |
| 1622 |
final byte[] inBuf = _inputBuffer; |
| 1623 |
int inPtr = _inputPtr; |
| 1624 |
|
| 1625 |
/* 25-Jan-2014, tsaloranta: Micro-benchmarks suggest that unrolling |
| 1626 |
* does NOT speed up things on JDK 7, let's not do it. |
| 1627 |
*/ |
| 1628 |
// loop unrolling seems to help here: |
| 1629 |
/* |
| 1630 |
for (int inEnd = inPtr + len - 3; inPtr < inEnd; ) { |
| 1631 |
outBuf[outPtr++] = (char) inBuf[inPtr++]; |
| 1632 |
outBuf[outPtr++] = (char) inBuf[inPtr++]; |
| 1633 |
outBuf[outPtr++] = (char) inBuf[inPtr++]; |
| 1634 |
outBuf[outPtr++] = (char) inBuf[inPtr++]; |
| 1635 |
} |
| 1636 |
int left = (len & 3); |
| 1637 |
if (left > 0) { |
| 1638 |
outBuf[outPtr++] = (char) inBuf[inPtr++]; |
| 1639 |
if (left > 1) { |
| 1640 |
outBuf[outPtr++] = (char) inBuf[inPtr++]; |
| 1641 |
if (left > 2) { |
| 1642 |
outBuf[outPtr++] = (char) inBuf[inPtr++]; |
| 1643 |
} |
| 1644 |
} |
| 1645 |
} |
| 1646 |
*/ |
| 1647 |
for (int inEnd = inPtr + len; inPtr < inEnd; ++inPtr) { |
| 1648 |
outBuf[outPtr++] = (char) inBuf[inPtr]; |
| 1649 |
} |
| 1650 |
_inputPtr = inPtr; |
| 1651 |
return _textBuffer.setCurrentAndReturn(len); |
| 1652 |
} |
| 1653 |
|
| 1654 |
/** |
| 1655 |
* Helper method used to decode short Unicode string, length for which actual |
| 1656 |
* length (in bytes) is known |
| 1657 |
* |
| 1658 |
* @param len Length between 1 and 64 |
| 1659 |
*/ |
| 1660 |
private final String _decodeShortUnicodeName(int len) |
| 1661 |
throws IOException |
| 1662 |
{ |
| 1663 |
// note: caller ensures we have enough bytes available |
| 1664 |
int outPtr = 0; |
| 1665 |
char[] outBuf = _textBuffer.emptyAndGetCurrentSegment(); |
| 1666 |
int inPtr = _inputPtr; |
| 1667 |
_inputPtr += len; |
| 1668 |
final int[] codes = SmileConstants.sUtf8UnitLengths; |
| 1669 |
final byte[] inBuf = _inputBuffer; |
| 1670 |
for (int end = inPtr + len; inPtr < end; ) { |
| 1671 |
int i = inBuf[inPtr++] & 0xFF; |
| 1672 |
int code = codes[i]; |
| 1673 |
if (code != 0) { |
| 1674 |
// trickiest one, need surrogate handling |
| 1675 |
switch (code) { |
| 1676 |
case 1: |
| 1677 |
i = ((i & 0x1F) << 6) | (inBuf[inPtr++] & 0x3F); |
| 1678 |
break; |
| 1679 |
case 2: |
| 1680 |
i = ((i & 0x0F) << 12) |
| 1681 |
| ((inBuf[inPtr++] & 0x3F) << 6) |
| 1682 |
| (inBuf[inPtr++] & 0x3F); |
| 1683 |
break; |
| 1684 |
case 3: |
| 1685 |
i = ((i & 0x07) << 18) |
| 1686 |
| ((inBuf[inPtr++] & 0x3F) << 12) |
| 1687 |
| ((inBuf[inPtr++] & 0x3F) << 6) |
| 1688 |
| (inBuf[inPtr++] & 0x3F); |
| 1689 |
// note: this is the codepoint value; need to split, too |
| 1690 |
i -= 0x10000; |
| 1691 |
outBuf[outPtr++] = (char) (0xD800 | (i >> 10)); |
| 1692 |
i = 0xDC00 | (i & 0x3FF); |
| 1693 |
break; |
| 1694 |
default: // invalid |
| 1695 |
_reportError("Invalid byte "+Integer.toHexString(i)+" in short Unicode text block"); |
| 1696 |
} |
| 1697 |
} |
| 1698 |
outBuf[outPtr++] = (char) i; |
| 1699 |
} |
| 1700 |
return _textBuffer.setCurrentAndReturn(outPtr); |
| 1701 |
} |
| 1702 |
|
| 1703 |
// note: slightly edited copy of UTF8StreamParser.addName() |
| 1704 |
private final String _decodeLongUnicodeName(int[] quads, int byteLen, int quadLen) |
| 1705 |
throws IOException |
| 1706 |
{ |
| 1707 |
int lastQuadBytes = byteLen & 3; |
| 1708 |
// Ok: must decode UTF-8 chars. No other validation SHOULD be needed (except bounds checks?) |
| 1709 |
/* Note: last quad is not correctly aligned (leading zero bytes instead |
| 1710 |
* need to shift a bit, instead of trailing). Only need to shift it |
| 1711 |
* for UTF-8 decoding; need revert for storage (since key will not |
| 1712 |
* be aligned, to optimize lookup speed) |
| 1713 |
*/ |
| 1714 |
int lastQuad; |
| 1715 |
|
| 1716 |
if (lastQuadBytes < 4) { |
| 1717 |
lastQuad = quads[quadLen-1]; |
| 1718 |
// 8/16/24 bit left shift |
| 1719 |
quads[quadLen-1] = (lastQuad << ((4 - lastQuadBytes) << 3)); |
| 1720 |
} else { |
| 1721 |
lastQuad = 0; |
| 1722 |
} |
| 1723 |
|
| 1724 |
char[] cbuf = _textBuffer.emptyAndGetCurrentSegment(); |
| 1725 |
int cix = 0; |
| 1726 |
|
| 1727 |
for (int ix = 0; ix < byteLen; ) { |
| 1728 |
int ch = quads[ix >> 2]; // current quad, need to shift+mask |
| 1729 |
int byteIx = (ix & 3); |
| 1730 |
ch = (ch >> ((3 - byteIx) << 3)) & 0xFF; |
| 1731 |
++ix; |
| 1732 |
|
| 1733 |
if (ch > 127) { // multi-byte |
| 1734 |
int needed; |
| 1735 |
if ((ch & 0xE0) == 0xC0) { // 2 bytes (0x0080 - 0x07FF) |
| 1736 |
ch &= 0x1F; |
| 1737 |
needed = 1; |
| 1738 |
} else if ((ch & 0xF0) == 0xE0) { // 3 bytes (0x0800 - 0xFFFF) |
| 1739 |
ch &= 0x0F; |
| 1740 |
needed = 2; |
| 1741 |
} else if ((ch & 0xF8) == 0xF0) { // 4 bytes; double-char with surrogates and all... |
| 1742 |
ch &= 0x07; |
| 1743 |
needed = 3; |
| 1744 |
} else { // 5- and 6-byte chars not valid chars |
| 1745 |
_reportInvalidInitial(ch); |
| 1746 |
needed = ch = 1; // never really gets this far |
| 1747 |
} |
| 1748 |
if ((ix + needed) > byteLen) { |
| 1749 |
_reportInvalidEOF(" in long field name"); |
| 1750 |
} |
| 1751 |
|
| 1752 |
// Ok, always need at least one more: |
| 1753 |
int ch2 = quads[ix >> 2]; // current quad, need to shift+mask |
| 1754 |
byteIx = (ix & 3); |
| 1755 |
ch2 = (ch2 >> ((3 - byteIx) << 3)); |
| 1756 |
++ix; |
| 1757 |
|
| 1758 |
if ((ch2 & 0xC0) != 0x080) { |
| 1759 |
_reportInvalidOther(ch2); |
| 1760 |
} |
| 1761 |
ch = (ch << 6) | (ch2 & 0x3F); |
| 1762 |
if (needed > 1) { |
| 1763 |
ch2 = quads[ix >> 2]; |
| 1764 |
byteIx = (ix & 3); |
| 1765 |
ch2 = (ch2 >> ((3 - byteIx) << 3)); |
| 1766 |
++ix; |
| 1767 |
|
| 1768 |
if ((ch2 & 0xC0) != 0x080) { |
| 1769 |
_reportInvalidOther(ch2); |
| 1770 |
} |
| 1771 |
ch = (ch << 6) | (ch2 & 0x3F); |
| 1772 |
if (needed > 2) { // 4 bytes? (need surrogates on output) |
| 1773 |
ch2 = quads[ix >> 2]; |
| 1774 |
byteIx = (ix & 3); |
| 1775 |
ch2 = (ch2 >> ((3 - byteIx) << 3)); |
| 1776 |
++ix; |
| 1777 |
if ((ch2 & 0xC0) != 0x080) { |
| 1778 |
_reportInvalidOther(ch2 & 0xFF); |
| 1779 |
} |
| 1780 |
ch = (ch << 6) | (ch2 & 0x3F); |
| 1781 |
} |
| 1782 |
} |
| 1783 |
if (needed > 2) { // surrogate pair? once again, let's output one here, one later on |
| 1784 |
ch -= 0x10000; // to normalize it starting with 0x0 |
| 1785 |
if (cix >= cbuf.length) { |
| 1786 |
cbuf = _textBuffer.expandCurrentSegment(); |
| 1787 |
} |
| 1788 |
cbuf[cix++] = (char) (0xD800 + (ch >> 10)); |
| 1789 |
ch = 0xDC00 | (ch & 0x03FF); |
| 1790 |
} |
| 1791 |
} |
| 1792 |
if (cix >= cbuf.length) { |
| 1793 |
cbuf = _textBuffer.expandCurrentSegment(); |
| 1794 |
} |
| 1795 |
cbuf[cix++] = (char) ch; |
| 1796 |
} |
| 1797 |
|
| 1798 |
// Ok. Now we have the character array, and can construct the String |
| 1799 |
String baseName = new String(cbuf, 0, cix); |
| 1800 |
// And finally, un-align if necessary |
| 1801 |
if (lastQuadBytes < 4) { |
| 1802 |
quads[quadLen-1] = lastQuad; |
| 1803 |
} |
| 1804 |
return _symbols.addName(baseName, quads, quadLen); |
| 1805 |
} |
| 1806 |
|
| 1807 |
private final void _handleLongFieldName() throws IOException |
| 1808 |
{ |
| 1809 |
// First: gather quads we need, looking for end marker |
| 1810 |
final byte[] inBuf = _inputBuffer; |
| 1811 |
int quads = 0; |
| 1812 |
int bytes = 0; |
| 1813 |
int q = 0; |
| 1814 |
|
| 1815 |
while (true) { |
| 1816 |
if (_inputPtr >= _inputEnd) { |
| 1817 |
loadMoreGuaranteed(); |
| 1818 |
} |
| 1819 |
byte b = inBuf[_inputPtr++]; |
| 1820 |
if (BYTE_MARKER_END_OF_STRING == b) { |
| 1821 |
bytes = 0; |
| 1822 |
break; |
| 1823 |
} |
| 1824 |
q = ((int) b) & 0xFF; |
| 1825 |
if (_inputPtr >= _inputEnd) { |
| 1826 |
loadMoreGuaranteed(); |
| 1827 |
} |
| 1828 |
b = inBuf[_inputPtr++]; |
| 1829 |
if (BYTE_MARKER_END_OF_STRING == b) { |
| 1830 |
bytes = 1; |
| 1831 |
break; |
| 1832 |
} |
| 1833 |
q = (q << 8) | (b & 0xFF); |
| 1834 |
if (_inputPtr >= _inputEnd) { |
| 1835 |
loadMoreGuaranteed(); |
| 1836 |
} |
| 1837 |
b = inBuf[_inputPtr++]; |
| 1838 |
if (BYTE_MARKER_END_OF_STRING == b) { |
| 1839 |
bytes = 2; |
| 1840 |
break; |
| 1841 |
} |
| 1842 |
q = (q << 8) | (b & 0xFF); |
| 1843 |
if (_inputPtr >= _inputEnd) { |
| 1844 |
loadMoreGuaranteed(); |
| 1845 |
} |
| 1846 |
b = inBuf[_inputPtr++]; |
| 1847 |
if (BYTE_MARKER_END_OF_STRING == b) { |
| 1848 |
bytes = 3; |
| 1849 |
break; |
| 1850 |
} |
| 1851 |
q = (q << 8) | (b & 0xFF); |
| 1852 |
if (quads >= _quadBuffer.length) { |
| 1853 |
_quadBuffer = _growArrayTo(_quadBuffer, _quadBuffer.length + 256); // grow by 1k |
| 1854 |
} |
| 1855 |
_quadBuffer[quads++] = q; |
| 1856 |
} |
| 1857 |
// and if we have more bytes, append those too |
| 1858 |
int byteLen = (quads << 2); |
| 1859 |
if (bytes > 0) { |
| 1860 |
if (quads >= _quadBuffer.length) { |
| 1861 |
_quadBuffer = _growArrayTo(_quadBuffer, _quadBuffer.length + 256); |
| 1862 |
} |
| 1863 |
_quadBuffer[quads++] = q; |
| 1864 |
byteLen += bytes; |
| 1865 |
} |
| 1866 |
|
| 1867 |
// Know this name already? |
| 1868 |
String name = _symbols.findName(_quadBuffer, quads); |
| 1869 |
if (name == null) { |
| 1870 |
name = _decodeLongUnicodeName(_quadBuffer, byteLen, quads); |
| 1871 |
} |
| 1872 |
if (_seenNames != null) { |
| 1873 |
if (_seenNameCount >= _seenNames.length) { |
| 1874 |
_seenNames = _expandSeenNames(_seenNames); |
| 1875 |
} |
| 1876 |
_seenNames[_seenNameCount++] = name; |
| 1877 |
} |
| 1878 |
_parsingContext.setCurrentName(name); |
| 1879 |
} |
| 1880 |
|
| 1881 |
/** |
| 1882 |
* Helper method for trying to find specified encoded UTF-8 byte sequence |
| 1883 |
* from symbol table; if successful avoids actual decoding to String |
| 1884 |
*/ |
| 1885 |
private final String _findDecodedFromSymbols(final int len) throws IOException |
| 1886 |
{ |
| 1887 |
if ((_inputEnd - _inputPtr) < len) { |
| 1888 |
_loadToHaveAtLeast(len); |
| 1889 |
} |
| 1890 |
// First: maybe we already have this name decoded? |
| 1891 |
if (len < 5) { |
| 1892 |
int inPtr = _inputPtr; |
| 1893 |
final byte[] inBuf = _inputBuffer; |
| 1894 |
int q = inBuf[inPtr] & 0xFF; |
| 1895 |
if (len > 1) { |
| 1896 |
q = (q << 8) + (inBuf[++inPtr] & 0xFF); |
| 1897 |
if (len > 2) { |
| 1898 |
q = (q << 8) + (inBuf[++inPtr] & 0xFF); |
| 1899 |
if (len > 3) { |
| 1900 |
q = (q << 8) + (inBuf[++inPtr] & 0xFF); |
| 1901 |
} |
| 1902 |
} |
| 1903 |
} |
| 1904 |
_quad1 = q; |
| 1905 |
return _symbols.findName(q); |
| 1906 |
} |
| 1907 |
|
| 1908 |
final byte[] inBuf = _inputBuffer; |
| 1909 |
int inPtr = _inputPtr; |
| 1910 |
|
| 1911 |
// First quadbyte is easy |
| 1912 |
int q1 = (inBuf[inPtr++] & 0xFF); |
| 1913 |
q1 = (q1 << 8) | (inBuf[inPtr++] & 0xFF); |
| 1914 |
q1 = (q1 << 8) | (inBuf[inPtr++] & 0xFF); |
| 1915 |
q1 = (q1 << 8) | (inBuf[inPtr++] & 0xFF); |
| 1916 |
|
| 1917 |
if (len < 9) { |
| 1918 |
int q2 = (inBuf[inPtr++] & 0xFF); |
| 1919 |
int left = len - 5; |
| 1920 |
if (left > 0) { |
| 1921 |
q2 = (q2 << 8) + (inBuf[inPtr++] & 0xFF); |
| 1922 |
if (left > 1) { |
| 1923 |
q2 = (q2 << 8) + (inBuf[inPtr++] & 0xFF); |
| 1924 |
if (left > 2) { |
| 1925 |
q2 = (q2 << 8) + (inBuf[inPtr++] & 0xFF); |
| 1926 |
} |
| 1927 |
} |
| 1928 |
} |
| 1929 |
_quad1 = q1; |
| 1930 |
_quad2 = q2; |
| 1931 |
return _symbols.findName(q1, q2); |
| 1932 |
} |
| 1933 |
|
| 1934 |
int q2 = (inBuf[inPtr++] & 0xFF); |
| 1935 |
q2 = (q2 << 8) | (inBuf[inPtr++] & 0xFF); |
| 1936 |
q2 = (q2 << 8) | (inBuf[inPtr++] & 0xFF); |
| 1937 |
q2 = (q2 << 8) | (inBuf[inPtr++] & 0xFF); |
| 1938 |
|
| 1939 |
if (len < 13) { |
| 1940 |
int q3 = (inBuf[inPtr++] & 0xFF); |
| 1941 |
int left = len - 9; |
| 1942 |
if (left > 0) { |
| 1943 |
q3 = (q3 << 8) + (inBuf[inPtr++] & 0xFF); |
| 1944 |
if (left > 1) { |
| 1945 |
q3 = (q3 << 8) + (inBuf[inPtr++] & 0xFF); |
| 1946 |
if (left > 2) { |
| 1947 |
q3 = (q3 << 8) + (inBuf[inPtr++] & 0xFF); |
| 1948 |
} |
| 1949 |
} |
| 1950 |
} |
| 1951 |
_quad1 = q1; |
| 1952 |
_quad2 = q2; |
| 1953 |
_quad3 = q3; |
| 1954 |
return _symbols.findName(q1, q2, q3); |
| 1955 |
} |
| 1956 |
return _findDecodedLong(len, q1, q2); |
| 1957 |
} |
| 1958 |
|
| 1959 |
/** |
| 1960 |
* Method for locating names longer than 8 bytes (in UTF-8) |
| 1961 |
*/ |
| 1962 |
private final String _findDecodedLong(int len, int q1, int q2) throws IOException |
| 1963 |
{ |
| 1964 |
// first, need enough buffer to store bytes as ints: |
| 1965 |
{ |
| 1966 |
int bufLen = (len + 3) >> 2; |
| 1967 |
if (bufLen > _quadBuffer.length) { |
| 1968 |
_quadBuffer = _growArrayTo(_quadBuffer, bufLen); |
| 1969 |
} |
| 1970 |
} |
| 1971 |
_quadBuffer[0] = q1; |
| 1972 |
_quadBuffer[1] = q2; |
| 1973 |
|
| 1974 |
// then decode, full quads first |
| 1975 |
int offset = 2; |
| 1976 |
int inPtr = _inputPtr+8; |
| 1977 |
len -= 8; |
| 1978 |
|
| 1979 |
final byte[] inBuf = _inputBuffer; |
| 1980 |
do { |
| 1981 |
int q = (inBuf[inPtr++] & 0xFF); |
| 1982 |
q = (q << 8) | inBuf[inPtr++] & 0xFF; |
| 1983 |
q = (q << 8) | inBuf[inPtr++] & 0xFF; |
| 1984 |
q = (q << 8) | inBuf[inPtr++] & 0xFF; |
| 1985 |
_quadBuffer[offset++] = q; |
| 1986 |
} while ((len -= 4) > 3); |
| 1987 |
// and then leftovers |
| 1988 |
if (len > 0) { |
| 1989 |
int q = inBuf[inPtr] & 0xFF; |
| 1990 |
if (len > 1) { |
| 1991 |
q = (q << 8) + (inBuf[++inPtr] & 0xFF); |
| 1992 |
if (len > 2) { |
| 1993 |
q = (q << 8) + (inBuf[++inPtr] & 0xFF); |
| 1994 |
} |
| 1995 |
} |
| 1996 |
_quadBuffer[offset++] = q; |
| 1997 |
} |
| 1998 |
return _symbols.findName(_quadBuffer, offset); |
| 1999 |
} |
| 2000 |
|
| 2001 |
private static int[] _growArrayTo(int[] arr, int minSize) { |
| 2002 |
final int size = minSize+4; |
| 2003 |
if (arr == null) { |
| 2004 |
return new int[size]; |
| 2005 |
} |
| 2006 |
return Arrays.copyOf(arr, size); |
| 2007 |
} |
| 2008 |
|
| 2009 |
/* |
| 2010 |
/********************************************************** |
| 2011 |
/* Internal methods, secondary parsing |
| 2012 |
/********************************************************** |
| 2013 |
*/ |
| 2014 |
|
| 2015 |
@Override |
| 2016 |
protected void _parseNumericValue(int expType) throws IOException |
| 2017 |
{ |
| 2018 |
if (_tokenIncomplete) { |
| 2019 |
int tb = _typeAsInt; |
| 2020 |
// ensure we got a numeric type with value that is lazily parsed |
| 2021 |
if ((tb >> 5) != 1) { |
| 2022 |
_reportError("Current token ("+_currToken+") not numeric, can not use numeric value accessors"); |
| 2023 |
} |
| 2024 |
_tokenIncomplete = false; |
| 2025 |
_finishNumberToken(tb); |
| 2026 |
} |
| 2027 |
} |
| 2028 |
|
| 2029 |
@Override // since 2.6 |
| 2030 |
protected int _parseIntValue() throws IOException |
| 2031 |
{ |
| 2032 |
// Inlined variant of: _parseNumericValue(NR_INT) |
| 2033 |
if (_tokenIncomplete) { |
| 2034 |
_tokenIncomplete = false; |
| 2035 |
if ((_typeAsInt & 0x1F) == 4) { |
| 2036 |
_finishInt(); // vint |
| 2037 |
return _numberInt; |
| 2038 |
} |
| 2039 |
_finishNumberToken(_typeAsInt); |
| 2040 |
} |
| 2041 |
if ((_numTypesValid & NR_INT) == 0) { |
| 2042 |
convertNumberToInt(); |
| 2043 |
} |
| 2044 |
return _numberInt; |
| 2045 |
} |
| 2046 |
|
| 2047 |
/** |
| 2048 |
* Method called to finish parsing of a token so that token contents |
| 2049 |
* are retrievable |
| 2050 |
*/ |
| 2051 |
protected final void _finishToken() throws IOException |
| 2052 |
{ |
| 2053 |
_tokenIncomplete = false; |
| 2054 |
int tb = _typeAsInt; |
| 2055 |
|
| 2056 |
int type = (tb >> 5); |
| 2057 |
if (type == 1) { // simple literals, numbers |
| 2058 |
_finishNumberToken(tb); |
| 2059 |
return; |
| 2060 |
} |
| 2061 |
if (type <= 3) { // tiny & short ASCII |
| 2062 |
_decodeShortAsciiValue(1 + (tb & 0x3F)); |
| 2063 |
return; |
| 2064 |
} |
| 2065 |
if (type <= 5) { // tiny & short Unicode |
| 2066 |
// short unicode; note, lengths 2 - 65 (off-by-one compared to ASCII) |
| 2067 |
_decodeShortUnicodeValue(2 + (tb & 0x3F)); |
| 2068 |
return; |
| 2069 |
} |
| 2070 |
if (type == 7) { |
| 2071 |
tb &= 0x1F; |
| 2072 |
// next 3 bytes define subtype |
| 2073 |
switch (tb >> 2) { |
| 2074 |
case 0: // long variable length ASCII |
| 2075 |
_decodeLongAscii(); |
| 2076 |
return; |
| 2077 |
case 1: // long variable length Unicode |
| 2078 |
_decodeLongUnicode(); |
| 2079 |
return; |
| 2080 |
case 2: // binary, 7-bit |
| 2081 |
_binaryValue = _read7BitBinaryWithLength(); |
| 2082 |
return; |
| 2083 |
case 7: // binary, raw |
| 2084 |
_finishRawBinary(); |
| 2085 |
return; |
| 2086 |
} |
| 2087 |
} |
| 2088 |
// sanity check |
| 2089 |
_throwInternal(); |
| 2090 |
} |
| 2091 |
|
| 2092 |
protected final void _finishNumberToken(int tb) throws IOException |
| 2093 |
{ |
| 2094 |
switch (tb & 0x1F) { |
| 2095 |
case 4: |
| 2096 |
_finishInt(); // vint |
| 2097 |
return; |
| 2098 |
case 5: // vlong |
| 2099 |
_finishLong(); |
| 2100 |
return; |
| 2101 |
case 6: |
| 2102 |
_finishBigInteger(); |
| 2103 |
return; |
| 2104 |
case 8: // float |
| 2105 |
_finishFloat(); |
| 2106 |
return; |
| 2107 |
case 9: // double |
| 2108 |
_finishDouble(); |
| 2109 |
return; |
| 2110 |
case 10: // big-decimal |
| 2111 |
_finishBigDecimal(); |
| 2112 |
return; |
| 2113 |
} |
| 2114 |
_throwInternal(); |
| 2115 |
} |
| 2116 |
|
| 2117 |
/* |
| 2118 |
/********************************************************** |
| 2119 |
/* Internal methods, secondary Number parsing |
| 2120 |
/********************************************************** |
| 2121 |
*/ |
| 2122 |
|
| 2123 |
private final void _finishInt() throws IOException |
| 2124 |
{ |
| 2125 |
int ptr = _inputPtr; |
| 2126 |
if ((ptr + 5) >= _inputEnd) { |
| 2127 |
_finishIntSlow(); |
| 2128 |
return; |
| 2129 |
} |
| 2130 |
int value = _inputBuffer[ptr++]; |
| 2131 |
int i; |
| 2132 |
if (value < 0) { // 6 bits |
| 2133 |
value &= 0x3F; |
| 2134 |
} else { |
| 2135 |
i = _inputBuffer[ptr++]; |
| 2136 |
if (i >= 0) { // 13 bits |
| 2137 |
value = (value << 7) + i; |
| 2138 |
i = _inputBuffer[ptr++]; |
| 2139 |
if (i >= 0) { |
| 2140 |
value = (value << 7) + i; |
| 2141 |
i = _inputBuffer[ptr++]; |
| 2142 |
if (i >= 0) { |
| 2143 |
value = (value << 7) + i; |
| 2144 |
// and then we must get negative |
| 2145 |
i = _inputBuffer[ptr++]; |
| 2146 |
if (i >= 0) { |
| 2147 |
_reportError("Corrupt input; 32-bit VInt extends beyond 5 data bytes"); |
| 2148 |
} |
| 2149 |
} |
| 2150 |
} |
| 2151 |
} |
| 2152 |
value = (value << 6) + (i & 0x3F); |
| 2153 |
} |
| 2154 |
_inputPtr = ptr; |
| 2155 |
_numberInt = SmileUtil.zigzagDecode(value); |
| 2156 |
_numTypesValid = NR_INT; |
| 2157 |
} |
| 2158 |
|
| 2159 |
private final void _finishIntSlow() throws IOException |
| 2160 |
{ |
| 2161 |
if (_inputPtr >= _inputEnd) { |
| 2162 |
loadMoreGuaranteed(); |
| 2163 |
} |
| 2164 |
int value = _inputBuffer[_inputPtr++]; |
| 2165 |
int i; |
| 2166 |
if (value < 0) { // 6 bits |
| 2167 |
value &= 0x3F; |
| 2168 |
} else { |
| 2169 |
if (_inputPtr >= _inputEnd) { |
| 2170 |
loadMoreGuaranteed(); |
| 2171 |
} |
| 2172 |
i = _inputBuffer[_inputPtr++]; |
| 2173 |
if (i >= 0) { // 13 bits |
| 2174 |
value = (value << 7) + i; |
| 2175 |
if (_inputPtr >= _inputEnd) { |
| 2176 |
loadMoreGuaranteed(); |
| 2177 |
} |
| 2178 |
i = _inputBuffer[_inputPtr++]; |
| 2179 |
if (i >= 0) { |
| 2180 |
value = (value << 7) + i; |
| 2181 |
if (_inputPtr >= _inputEnd) { |
| 2182 |
loadMoreGuaranteed(); |
| 2183 |
} |
| 2184 |
i = _inputBuffer[_inputPtr++]; |
| 2185 |
if (i >= 0) { |
| 2186 |
value = (value << 7) + i; |
| 2187 |
// and then we must get negative |
| 2188 |
if (_inputPtr >= _inputEnd) { |
| 2189 |
loadMoreGuaranteed(); |
| 2190 |
} |
| 2191 |
i = _inputBuffer[_inputPtr++]; |
| 2192 |
if (i >= 0) { |
| 2193 |
_reportError("Corrupt input; 32-bit VInt extends beyond 5 data bytes"); |
| 2194 |
} |
| 2195 |
} |
| 2196 |
} |
| 2197 |
} |
| 2198 |
value = (value << 6) + (i & 0x3F); |
| 2199 |
} |
| 2200 |
_numberInt = SmileUtil.zigzagDecode(value); |
| 2201 |
_numTypesValid = NR_INT; |
| 2202 |
} |
| 2203 |
|
| 2204 |
private final void _finishLong() throws IOException |
| 2205 |
{ |
| 2206 |
int ptr = _inputPtr; |
| 2207 |
final int maxEnd = ptr+11; |
| 2208 |
if (maxEnd >= _inputEnd) { |
| 2209 |
_finishLongSlow(); |
| 2210 |
return; |
| 2211 |
} |
| 2212 |
int i = _inputBuffer[ptr++]; // first 7 bits |
| 2213 |
i = (i << 7) + _inputBuffer[ptr++]; // 14 bits |
| 2214 |
i = (i << 7) + _inputBuffer[ptr++]; // 21 |
| 2215 |
i = (i << 7) + _inputBuffer[ptr++]; |
| 2216 |
|
| 2217 |
// Ok: couple of bytes more |
| 2218 |
long l = i; |
| 2219 |
do { |
| 2220 |
int value = _inputBuffer[ptr++]; |
| 2221 |
if (value < 0) { |
| 2222 |
l = (l << 6) + (value & 0x3F); |
| 2223 |
_inputPtr = ptr; |
| 2224 |
_numberLong = SmileUtil.zigzagDecode(l); |
| 2225 |
_numTypesValid = NR_LONG; |
| 2226 |
return; |
| 2227 |
} |
| 2228 |
l = (l << 7) + value; |
| 2229 |
} while (ptr < maxEnd); |
| 2230 |
_reportError("Corrupt input; 32-bit VInt extends beyond 5 data bytes"); |
| 2231 |
} |
| 2232 |
|
| 2233 |
private final void _finishLongSlow() throws IOException |
| 2234 |
{ |
| 2235 |
// Ok, first, will always get 4 full data bytes first; 1 was already passed |
| 2236 |
long l = (long) _fourBytesToInt(); |
| 2237 |
// and loop for the rest |
| 2238 |
while (true) { |
| 2239 |
if (_inputPtr >= _inputEnd) { |
| 2240 |
loadMoreGuaranteed(); |
| 2241 |
} |
| 2242 |
int value = _inputBuffer[_inputPtr++]; |
| 2243 |
if (value < 0) { |
| 2244 |
l = (l << 6) + (value & 0x3F); |
| 2245 |
_numberLong = SmileUtil.zigzagDecode(l); |
| 2246 |
_numTypesValid = NR_LONG; |
| 2247 |
return; |
| 2248 |
} |
| 2249 |
l = (l << 7) + value; |
| 2250 |
} |
| 2251 |
} |
| 2252 |
|
| 2253 |
private final int _fourBytesToInt() throws IOException |
| 2254 |
{ |
| 2255 |
int ptr = _inputPtr; |
| 2256 |
if ((ptr + 3) >= _inputEnd) { |
| 2257 |
return _fourBytesToIntSlow(); |
| 2258 |
} |
| 2259 |
int i = _inputBuffer[ptr++]; // first 7 bits |
| 2260 |
i = (i << 7) + _inputBuffer[ptr++]; // 14 bits |
| 2261 |
i = (i << 7) + _inputBuffer[ptr++]; // 21 |
| 2262 |
i = (i << 7) + _inputBuffer[ptr++]; |
| 2263 |
_inputPtr = ptr; |
| 2264 |
return i; |
| 2265 |
} |
| 2266 |
|
| 2267 |
private final int _fourBytesToIntSlow() throws IOException |
| 2268 |
{ |
| 2269 |
if (_inputPtr >= _inputEnd) { |
| 2270 |
loadMoreGuaranteed(); |
| 2271 |
} |
| 2272 |
int i = _inputBuffer[_inputPtr++]; // first 7 bits |
| 2273 |
if (_inputPtr >= _inputEnd) { |
| 2274 |
loadMoreGuaranteed(); |
| 2275 |
} |
| 2276 |
i = (i << 7) + _inputBuffer[_inputPtr++]; // 14 bits |
| 2277 |
if (_inputPtr >= _inputEnd) { |
| 2278 |
loadMoreGuaranteed(); |
| 2279 |
} |
| 2280 |
i = (i << 7) + _inputBuffer[_inputPtr++]; // 21 |
| 2281 |
if (_inputPtr >= _inputEnd) { |
| 2282 |
loadMoreGuaranteed(); |
| 2283 |
} |
| 2284 |
return (i << 7) + _inputBuffer[_inputPtr++]; |
| 2285 |
} |
| 2286 |
|
| 2287 |
private final void _finishBigInteger() throws IOException |
| 2288 |
{ |
| 2289 |
byte[] raw = _read7BitBinaryWithLength(); |
| 2290 |
_numberBigInt = new BigInteger(raw); |
| 2291 |
_numTypesValid = NR_BIGINT; |
| 2292 |
} |
| 2293 |
|
| 2294 |
private final void _finishFloat() throws IOException |
| 2295 |
{ |
| 2296 |
// just need 5 bytes to get int32 first; all are unsigned |
| 2297 |
int i = _fourBytesToInt(); |
| 2298 |
if (_inputPtr >= _inputEnd) { |
| 2299 |
loadMoreGuaranteed(); |
| 2300 |
} |
| 2301 |
i = (i << 7) + _inputBuffer[_inputPtr++]; |
| 2302 |
float f = Float.intBitsToFloat(i); |
| 2303 |
_numberDouble = (double) f; |
| 2304 |
_numTypesValid = NR_DOUBLE; |
| 2305 |
} |
| 2306 |
|
| 2307 |
private final void _finishDouble() throws IOException |
| 2308 |
{ |
| 2309 |
// ok; let's take two sets of 4 bytes (each is int) |
| 2310 |
long hi = _fourBytesToInt(); |
| 2311 |
long value = (hi << 28) + (long) _fourBytesToInt(); |
| 2312 |
// and then remaining 2 bytes |
| 2313 |
if (_inputPtr >= _inputEnd) { |
| 2314 |
loadMoreGuaranteed(); |
| 2315 |
} |
| 2316 |
value = (value << 7) + _inputBuffer[_inputPtr++]; |
| 2317 |
if (_inputPtr >= _inputEnd) { |
| 2318 |
loadMoreGuaranteed(); |
| 2319 |
} |
| 2320 |
value = (value << 7) + _inputBuffer[_inputPtr++]; |
| 2321 |
_numberDouble = Double.longBitsToDouble(value); |
| 2322 |
_numTypesValid = NR_DOUBLE; |
| 2323 |
} |
| 2324 |
|
| 2325 |
private final void _finishBigDecimal() throws IOException |
| 2326 |
{ |
| 2327 |
int scale = SmileUtil.zigzagDecode(_readUnsignedVInt()); |
| 2328 |
byte[] raw = _read7BitBinaryWithLength(); |
| 2329 |
_numberBigDecimal = new BigDecimal(new BigInteger(raw), scale); |
| 2330 |
_numTypesValid = NR_BIGDECIMAL; |
| 2331 |
} |
| 2332 |
|
| 2333 |
private final int _readUnsignedVInt() |
| 2334 |
throws IOException |
| 2335 |
{ |
| 2336 |
int value = 0; |
| 2337 |
while (true) { |
| 2338 |
if (_inputPtr >= _inputEnd) { |
| 2339 |
loadMoreGuaranteed(); |
| 2340 |
} |
| 2341 |
int i = _inputBuffer[_inputPtr++]; |
| 2342 |
if (i < 0) { // last byte |
| 2343 |
value = (value << 6) + (i & 0x3F); |
| 2344 |
return value; |
| 2345 |
} |
| 2346 |
value = (value << 7) + i; |
| 2347 |
} |
| 2348 |
} |
| 2349 |
|
| 2350 |
private final byte[] _read7BitBinaryWithLength() |
| 2351 |
throws IOException |
| 2352 |
{ |
| 2353 |
int byteLen = _readUnsignedVInt(); |
| 2354 |
byte[] result = new byte[byteLen]; |
| 2355 |
int ptr = 0; |
| 2356 |
int lastOkPtr = byteLen - 7; |
| 2357 |
|
| 2358 |
// first, read all 7-by-8 byte chunks |
| 2359 |
while (ptr <= lastOkPtr) { |
| 2360 |
if ((_inputEnd - _inputPtr) < 8) { |
| 2361 |
_loadToHaveAtLeast(8); |
| 2362 |
} |
| 2363 |
int i1 = (_inputBuffer[_inputPtr++] << 25) |
| 2364 |
+ (_inputBuffer[_inputPtr++] << 18) |
| 2365 |
+ (_inputBuffer[_inputPtr++] << 11) |
| 2366 |
+ (_inputBuffer[_inputPtr++] << 4); |
| 2367 |
int x = _inputBuffer[_inputPtr++]; |
| 2368 |
i1 += x >> 3; |
| 2369 |
int i2 = ((x & 0x7) << 21) |
| 2370 |
+ (_inputBuffer[_inputPtr++] << 14) |
| 2371 |
+ (_inputBuffer[_inputPtr++] << 7) |
| 2372 |
+ _inputBuffer[_inputPtr++]; |
| 2373 |
// Ok: got our 7 bytes, just need to split, copy |
| 2374 |
result[ptr++] = (byte)(i1 >> 24); |
| 2375 |
result[ptr++] = (byte)(i1 >> 16); |
| 2376 |
result[ptr++] = (byte)(i1 >> 8); |
| 2377 |
result[ptr++] = (byte)i1; |
| 2378 |
result[ptr++] = (byte)(i2 >> 16); |
| 2379 |
result[ptr++] = (byte)(i2 >> 8); |
| 2380 |
result[ptr++] = (byte)i2; |
| 2381 |
} |
| 2382 |
// and then leftovers: n+1 bytes to decode n bytes |
| 2383 |
int toDecode = (result.length - ptr); |
| 2384 |
if (toDecode > 0) { |
| 2385 |
if ((_inputEnd - _inputPtr) < (toDecode+1)) { |
| 2386 |
_loadToHaveAtLeast(toDecode+1); |
| 2387 |
} |
| 2388 |
int value = _inputBuffer[_inputPtr++]; |
| 2389 |
for (int i = 1; i < toDecode; ++i) { |
| 2390 |
value = (value << 7) + _inputBuffer[_inputPtr++]; |
| 2391 |
result[ptr++] = (byte) (value >> (7 - i)); |
| 2392 |
} |
| 2393 |
// last byte is different, has remaining 1 - 6 bits, right-aligned |
| 2394 |
value <<= toDecode; |
| 2395 |
result[ptr] = (byte) (value + _inputBuffer[_inputPtr++]); |
| 2396 |
} |
| 2397 |
return result; |
| 2398 |
} |
| 2399 |
|
| 2400 |
/* |
| 2401 |
/********************************************************** |
| 2402 |
/* Internal methods, secondary String parsing |
| 2403 |
/********************************************************** |
| 2404 |
*/ |
| 2405 |
|
| 2406 |
protected final String _decodeShortAsciiValue(int len) throws IOException |
| 2407 |
{ |
| 2408 |
if ((_inputEnd - _inputPtr) < len) { |
| 2409 |
_loadToHaveAtLeast(len); |
| 2410 |
} |
| 2411 |
// Note: we count on fact that buffer must have at least 'len' (<= 64) empty char slots |
| 2412 |
final char[] outBuf = _textBuffer.emptyAndGetCurrentSegment(); |
| 2413 |
int outPtr = 0; |
| 2414 |
final byte[] inBuf = _inputBuffer; |
| 2415 |
int inPtr = _inputPtr; |
| 2416 |
|
| 2417 |
// as with _decodeShortAsciiName, no unrolling |
| 2418 |
for (final int end = inPtr + len; inPtr < end; ++inPtr) { |
| 2419 |
outBuf[outPtr++] = (char) inBuf[inPtr]; |
| 2420 |
} |
| 2421 |
_inputPtr = inPtr; |
| 2422 |
return _textBuffer.setCurrentAndReturn(len); |
| 2423 |
} |
| 2424 |
|
| 2425 |
protected final String _decodeShortUnicodeValue(int len) throws IOException |
| 2426 |
{ |
| 2427 |
if ((_inputEnd - _inputPtr) < len) { |
| 2428 |
_loadToHaveAtLeast(len); |
| 2429 |
} |
| 2430 |
int outPtr = 0; |
| 2431 |
char[] outBuf = _textBuffer.emptyAndGetCurrentSegment(); |
| 2432 |
int inPtr = _inputPtr; |
| 2433 |
_inputPtr += len; |
| 2434 |
final int[] codes = SmileConstants.sUtf8UnitLengths; |
| 2435 |
final byte[] inputBuf = _inputBuffer; |
| 2436 |
for (int end = inPtr + len; inPtr < end; ) { |
| 2437 |
int i = inputBuf[inPtr++] & 0xFF; |
| 2438 |
int code = codes[i]; |
| 2439 |
if (code != 0) { |
| 2440 |
// trickiest one, need surrogate handling |
| 2441 |
switch (code) { |
| 2442 |
case 1: |
| 2443 |
i = ((i & 0x1F) << 6) | (inputBuf[inPtr++] & 0x3F); |
| 2444 |
break; |
| 2445 |
case 2: |
| 2446 |
i = ((i & 0x0F) << 12) |
| 2447 |
| ((inputBuf[inPtr++] & 0x3F) << 6) |
| 2448 |
| (inputBuf[inPtr++] & 0x3F); |
| 2449 |
break; |
| 2450 |
case 3: |
| 2451 |
i = ((i & 0x07) << 18) |
| 2452 |
| ((inputBuf[inPtr++] & 0x3F) << 12) |
| 2453 |
| ((inputBuf[inPtr++] & 0x3F) << 6) |
| 2454 |
| (inputBuf[inPtr++] & 0x3F); |
| 2455 |
// note: this is the codepoint value; need to split, too |
| 2456 |
i -= 0x10000; |
| 2457 |
outBuf[outPtr++] = (char) (0xD800 | (i >> 10)); |
| 2458 |
i = 0xDC00 | (i & 0x3FF); |
| 2459 |
break; |
| 2460 |
default: // invalid |
| 2461 |
_reportError("Invalid byte "+Integer.toHexString(i)+" in short Unicode text block"); |
| 2462 |
} |
| 2463 |
} |
| 2464 |
outBuf[outPtr++] = (char) i; |
| 2465 |
} |
| 2466 |
return _textBuffer.setCurrentAndReturn(outPtr); |
| 2467 |
} |
| 2468 |
|
| 2469 |
private final void _decodeLongAscii() throws IOException |
| 2470 |
{ |
| 2471 |
int outPtr = 0; |
| 2472 |
char[] outBuf = _textBuffer.emptyAndGetCurrentSegment(); |
| 2473 |
main_loop: |
| 2474 |
while (true) { |
| 2475 |
if (_inputPtr >= _inputEnd) { |
| 2476 |
loadMoreGuaranteed(); |
| 2477 |
} |
| 2478 |
int inPtr = _inputPtr; |
| 2479 |
int left = _inputEnd - inPtr; |
| 2480 |
if (outPtr >= outBuf.length) { |
| 2481 |
outBuf = _textBuffer.finishCurrentSegment(); |
| 2482 |
outPtr = 0; |
| 2483 |
} |
| 2484 |
left = Math.min(left, outBuf.length - outPtr); |
| 2485 |
do { |
| 2486 |
byte b = _inputBuffer[inPtr++]; |
| 2487 |
if (b == SmileConstants.BYTE_MARKER_END_OF_STRING) { |
| 2488 |
_inputPtr = inPtr; |
| 2489 |
break main_loop; |
| 2490 |
} |
| 2491 |
outBuf[outPtr++] = (char) b; |
| 2492 |
} while (--left > 0); |
| 2493 |
_inputPtr = inPtr; |
| 2494 |
} |
| 2495 |
_textBuffer.setCurrentLength(outPtr); |
| 2496 |
} |
| 2497 |
|
| 2498 |
private final void _decodeLongUnicode() throws IOException |
| 2499 |
{ |
| 2500 |
int outPtr = 0; |
| 2501 |
char[] outBuf = _textBuffer.emptyAndGetCurrentSegment(); |
| 2502 |
final int[] codes = SmileConstants.sUtf8UnitLengths; |
| 2503 |
int c; |
| 2504 |
final byte[] inputBuffer = _inputBuffer; |
| 2505 |
|
| 2506 |
main_loop: |
| 2507 |
while (true) { |
| 2508 |
// First the tight ASCII loop: |
| 2509 |
ascii_loop: |
| 2510 |
while (true) { |
| 2511 |
int ptr = _inputPtr; |
| 2512 |
if (ptr >= _inputEnd) { |
| 2513 |
loadMoreGuaranteed(); |
| 2514 |
ptr = _inputPtr; |
| 2515 |
} |
| 2516 |
if (outPtr >= outBuf.length) { |
| 2517 |
outBuf = _textBuffer.finishCurrentSegment(); |
| 2518 |
outPtr = 0; |
| 2519 |
} |
| 2520 |
int max = _inputEnd; |
| 2521 |
{ |
| 2522 |
int max2 = ptr + (outBuf.length - outPtr); |
| 2523 |
if (max2 < max) { |
| 2524 |
max = max2; |
| 2525 |
} |
| 2526 |
} |
| 2527 |
while (ptr < max) { |
| 2528 |
c = (int) inputBuffer[ptr++] & 0xFF; |
| 2529 |
if (codes[c] != 0) { |
| 2530 |
_inputPtr = ptr; |
| 2531 |
break ascii_loop; |
| 2532 |
} |
| 2533 |
outBuf[outPtr++] = (char) c; |
| 2534 |
} |
| 2535 |
_inputPtr = ptr; |
| 2536 |
} |
| 2537 |
// Ok: end marker, escape or multi-byte? |
| 2538 |
if (c == SmileConstants.INT_MARKER_END_OF_STRING) { |
| 2539 |
break main_loop; |
| 2540 |
} |
| 2541 |
|
| 2542 |
switch (codes[c]) { |
| 2543 |
case 1: // 2-byte UTF |
| 2544 |
c = _decodeUtf8_2(c); |
| 2545 |
break; |
| 2546 |
case 2: // 3-byte UTF |
| 2547 |
if ((_inputEnd - _inputPtr) >= 2) { |
| 2548 |
c = _decodeUtf8_3fast(c); |
| 2549 |
} else { |
| 2550 |
c = _decodeUtf8_3(c); |
| 2551 |
} |
| 2552 |
break; |
| 2553 |
case 3: // 4-byte UTF |
| 2554 |
c = _decodeUtf8_4(c); |
| 2555 |
// Let's add first part right away: |
| 2556 |
outBuf[outPtr++] = (char) (0xD800 | (c >> 10)); |
| 2557 |
if (outPtr >= outBuf.length) { |
| 2558 |
outBuf = _textBuffer.finishCurrentSegment(); |
| 2559 |
outPtr = 0; |
| 2560 |
} |
| 2561 |
c = 0xDC00 | (c & 0x3FF); |
| 2562 |
// And let the other char output down below |
| 2563 |
break; |
| 2564 |
default: |
| 2565 |
// Is this good enough error message? |
| 2566 |
_reportInvalidChar(c); |
| 2567 |
} |
| 2568 |
// Need more room? |
| 2569 |
if (outPtr >= outBuf.length) { |
| 2570 |
outBuf = _textBuffer.finishCurrentSegment(); |
| 2571 |
outPtr = 0; |
| 2572 |
} |
| 2573 |
// Ok, let's add char to output: |
| 2574 |
outBuf[outPtr++] = (char) c; |
| 2575 |
} |
| 2576 |
_textBuffer.setCurrentLength(outPtr); |
| 2577 |
} |
| 2578 |
|
| 2579 |
private final void _finishRawBinary() throws IOException |
| 2580 |
{ |
| 2581 |
int byteLen = _readUnsignedVInt(); |
| 2582 |
_binaryValue = new byte[byteLen]; |
| 2583 |
if (_inputPtr >= _inputEnd) { |
| 2584 |
loadMoreGuaranteed(); |
| 2585 |
} |
| 2586 |
int ptr = 0; |
| 2587 |
while (true) { |
| 2588 |
int toAdd = Math.min(byteLen, _inputEnd - _inputPtr); |
| 2589 |
System.arraycopy(_inputBuffer, _inputPtr, _binaryValue, ptr, toAdd); |
| 2590 |
_inputPtr += toAdd; |
| 2591 |
ptr += toAdd; |
| 2592 |
byteLen -= toAdd; |
| 2593 |
if (byteLen <= 0) { |
| 2594 |
return; |
| 2595 |
} |
| 2596 |
loadMoreGuaranteed(); |
| 2597 |
} |
| 2598 |
} |
| 2599 |
|
| 2600 |
/* |
| 2601 |
/********************************************************** |
| 2602 |
/* Internal methods, skipping |
| 2603 |
/********************************************************** |
| 2604 |
*/ |
| 2605 |
|
| 2606 |
/** |
| 2607 |
* Method called to skip remainders of an incomplete token, when |
| 2608 |
* contents themselves will not be needed any more |
| 2609 |
*/ |
| 2610 |
protected void _skipIncomplete() throws IOException |
| 2611 |
{ |
| 2612 |
_tokenIncomplete = false; |
| 2613 |
int tb = _typeAsInt; |
| 2614 |
switch (tb >> 5) { |
| 2615 |
case 1: // simple literals, numbers |
| 2616 |
tb &= 0x1F; |
| 2617 |
// next 3 bytes define subtype |
| 2618 |
switch (tb >> 2) { |
| 2619 |
case 1: // VInt (zigzag) |
| 2620 |
// easy, just skip until we see sign bit... (should we try to limit damage?) |
| 2621 |
switch (tb & 0x3) { |
| 2622 |
case 1: // vlong |
| 2623 |
_skipBytes(4); // min 5 bytes |
| 2624 |
// fall through |
| 2625 |
case 0: // vint |
| 2626 |
while (true) { |
| 2627 |
final int end = _inputEnd; |
| 2628 |
final byte[] buf = _inputBuffer; |
| 2629 |
while (_inputPtr < end) { |
| 2630 |
if (buf[_inputPtr++] < 0) { |
| 2631 |
return; |
| 2632 |
} |
| 2633 |
} |
| 2634 |
loadMoreGuaranteed(); |
| 2635 |
} |
| 2636 |
case 2: // big-int |
| 2637 |
// just has binary data |
| 2638 |
_skip7BitBinary(); |
| 2639 |
return; |
| 2640 |
} |
| 2641 |
break; |
| 2642 |
case 2: // other numbers |
| 2643 |
switch (tb & 0x3) { |
| 2644 |
case 0: // float |
| 2645 |
_skipBytes(5); |
| 2646 |
return; |
| 2647 |
case 1: // double |
| 2648 |
_skipBytes(10); |
| 2649 |
return; |
| 2650 |
case 2: // big-decimal |
| 2651 |
// first, skip scale |
| 2652 |
_readUnsignedVInt(); |
| 2653 |
// then length-prefixed binary serialization |
| 2654 |
_skip7BitBinary(); |
| 2655 |
return; |
| 2656 |
} |
| 2657 |
break; |
| 2658 |
} |
| 2659 |
break; |
| 2660 |
case 2: // tiny ASCII |
| 2661 |
// fall through |
| 2662 |
case 3: // short ASCII |
| 2663 |
_skipBytes(1 + (tb & 0x3F)); |
| 2664 |
return; |
| 2665 |
case 4: // tiny unicode |
| 2666 |
// fall through |
| 2667 |
case 5: // short unicode |
| 2668 |
_skipBytes(2 + (tb & 0x3F)); |
| 2669 |
return; |
| 2670 |
case 7: |
| 2671 |
tb &= 0x1F; |
| 2672 |
// next 3 bytes define subtype |
| 2673 |
switch (tb >> 2) { |
| 2674 |
case 0: // long variable length ASCII |
| 2675 |
case 1: // long variable length unicode |
| 2676 |
/* Doesn't matter which one, just need to find the end marker |
| 2677 |
* (note: can potentially skip invalid UTF-8 too) |
| 2678 |
*/ |
| 2679 |
while (true) { |
| 2680 |
final int end = _inputEnd; |
| 2681 |
final byte[] buf = _inputBuffer; |
| 2682 |
while (_inputPtr < end) { |
| 2683 |
if (buf[_inputPtr++] == BYTE_MARKER_END_OF_STRING) { |
| 2684 |
return; |
| 2685 |
} |
| 2686 |
} |
| 2687 |
loadMoreGuaranteed(); |
| 2688 |
} |
| 2689 |
// never gets here |
| 2690 |
case 2: // binary, 7-bit |
| 2691 |
_skip7BitBinary(); |
| 2692 |
return; |
| 2693 |
case 7: // binary, raw |
| 2694 |
_skipBytes(_readUnsignedVInt()); |
| 2695 |
return; |
| 2696 |
} |
| 2697 |
} |
| 2698 |
_throwInternal(); |
| 2699 |
} |
| 2700 |
|
| 2701 |
protected void _skipBytes(int len) throws IOException |
| 2702 |
{ |
| 2703 |
while (true) { |
| 2704 |
int toAdd = Math.min(len, _inputEnd - _inputPtr); |
| 2705 |
_inputPtr += toAdd; |
| 2706 |
len -= toAdd; |
| 2707 |
if (len <= 0) { |
| 2708 |
return; |
| 2709 |
} |
| 2710 |
loadMoreGuaranteed(); |
| 2711 |
} |
| 2712 |
} |
| 2713 |
|
| 2714 |
/** |
| 2715 |
* Helper method for skipping length-prefixed binary data |
| 2716 |
* section |
| 2717 |
*/ |
| 2718 |
protected void _skip7BitBinary() throws IOException |
| 2719 |
{ |
| 2720 |
int origBytes = _readUnsignedVInt(); |
| 2721 |
// Ok; 8 encoded bytes for 7 payload bytes first |
| 2722 |
int chunks = origBytes / 7; |
| 2723 |
int encBytes = chunks * 8; |
| 2724 |
// and for last 0 - 6 bytes, last+1 (except none if no leftovers) |
| 2725 |
origBytes -= 7 * chunks; |
| 2726 |
if (origBytes > 0) { |
| 2727 |
encBytes += 1 + origBytes; |
| 2728 |
} |
| 2729 |
_skipBytes(encBytes); |
| 2730 |
} |
| 2731 |
|
| 2732 |
/* |
| 2733 |
/********************************************************** |
| 2734 |
/* Internal methods, UTF8 decoding |
| 2735 |
/********************************************************** |
| 2736 |
*/ |
| 2737 |
|
| 2738 |
private final int _decodeUtf8_2(int c) throws IOException |
| 2739 |
{ |
| 2740 |
if (_inputPtr >= _inputEnd) { |
| 2741 |
loadMoreGuaranteed(); |
| 2742 |
} |
| 2743 |
int d = (int) _inputBuffer[_inputPtr++]; |
| 2744 |
if ((d & 0xC0) != 0x080) { |
| 2745 |
_reportInvalidOther(d & 0xFF, _inputPtr); |
| 2746 |
} |
| 2747 |
return ((c & 0x1F) << 6) | (d & 0x3F); |
| 2748 |
} |
| 2749 |
|
| 2750 |
private final int _decodeUtf8_3(int c1) throws IOException |
| 2751 |
{ |
| 2752 |
if (_inputPtr >= _inputEnd) { |
| 2753 |
loadMoreGuaranteed(); |
| 2754 |
} |
| 2755 |
c1 &= 0x0F; |
| 2756 |
int d = (int) _inputBuffer[_inputPtr++]; |
| 2757 |
if ((d & 0xC0) != 0x080) { |
| 2758 |
_reportInvalidOther(d & 0xFF, _inputPtr); |
| 2759 |
} |
| 2760 |
int c = (c1 << 6) | (d & 0x3F); |
| 2761 |
if (_inputPtr >= _inputEnd) { |
| 2762 |
loadMoreGuaranteed(); |
| 2763 |
} |
| 2764 |
d = (int) _inputBuffer[_inputPtr++]; |
| 2765 |
if ((d & 0xC0) != 0x080) { |
| 2766 |
_reportInvalidOther(d & 0xFF, _inputPtr); |
| 2767 |
} |
| 2768 |
c = (c << 6) | (d & 0x3F); |
| 2769 |
return c; |
| 2770 |
} |
| 2771 |
|
| 2772 |
private final int _decodeUtf8_3fast(int c1) throws IOException |
| 2773 |
{ |
| 2774 |
c1 &= 0x0F; |
| 2775 |
int d = (int) _inputBuffer[_inputPtr++]; |
| 2776 |
if ((d & 0xC0) != 0x080) { |
| 2777 |
_reportInvalidOther(d & 0xFF, _inputPtr); |
| 2778 |
} |
| 2779 |
int c = (c1 << 6) | (d & 0x3F); |
| 2780 |
d = (int) _inputBuffer[_inputPtr++]; |
| 2781 |
if ((d & 0xC0) != 0x080) { |
| 2782 |
_reportInvalidOther(d & 0xFF, _inputPtr); |
| 2783 |
} |
| 2784 |
c = (c << 6) | (d & 0x3F); |
| 2785 |
return c; |
| 2786 |
} |
| 2787 |
|
| 2788 |
/** |
| 2789 |
* @return Character value <b>minus 0x10000</c>; this so that caller |
| 2790 |
* can readily expand it to actual surrogates |
| 2791 |
*/ |
| 2792 |
private final int _decodeUtf8_4(int c) throws IOException |
| 2793 |
{ |
| 2794 |
if (_inputPtr >= _inputEnd) { |
| 2795 |
loadMoreGuaranteed(); |
| 2796 |
} |
| 2797 |
int d = (int) _inputBuffer[_inputPtr++]; |
| 2798 |
if ((d & 0xC0) != 0x080) { |
| 2799 |
_reportInvalidOther(d & 0xFF, _inputPtr); |
| 2800 |
} |
| 2801 |
c = ((c & 0x07) << 6) | (d & 0x3F); |
| 2802 |
|
| 2803 |
if (_inputPtr >= _inputEnd) { |
| 2804 |
loadMoreGuaranteed(); |
| 2805 |
} |
| 2806 |
d = (int) _inputBuffer[_inputPtr++]; |
| 2807 |
if ((d & 0xC0) != 0x080) { |
| 2808 |
_reportInvalidOther(d & 0xFF, _inputPtr); |
| 2809 |
} |
| 2810 |
c = (c << 6) | (d & 0x3F); |
| 2811 |
if (_inputPtr >= _inputEnd) { |
| 2812 |
loadMoreGuaranteed(); |
| 2813 |
} |
| 2814 |
d = (int) _inputBuffer[_inputPtr++]; |
| 2815 |
if ((d & 0xC0) != 0x080) { |
| 2816 |
_reportInvalidOther(d & 0xFF, _inputPtr); |
| 2817 |
} |
| 2818 |
|
| 2819 |
/* note: won't change it to negative here, since caller |
| 2820 |
* already knows it'll need a surrogate |
| 2821 |
*/ |
| 2822 |
return ((c << 6) | (d & 0x3F)) - 0x10000; |
| 2823 |
} |
| 2824 |
|
| 2825 |
/* |
| 2826 |
/********************************************************** |
| 2827 |
/* Internal methods, error reporting |
| 2828 |
/********************************************************** |
| 2829 |
*/ |
| 2830 |
|
| 2831 |
protected void _reportInvalidSharedName(int index) throws IOException |
| 2832 |
{ |
| 2833 |
if (_seenNames == null) { |
| 2834 |
_reportError("Encountered shared name reference, even though document header explicitly declared no shared name references are included"); |
| 2835 |
} |
| 2836 |
_reportError("Invalid shared name reference "+index+"; only got "+_seenNameCount+" names in buffer (invalid content)"); |
| 2837 |
} |
| 2838 |
|
| 2839 |
protected void _reportInvalidSharedStringValue(int index) throws IOException |
| 2840 |
{ |
| 2841 |
if (_seenStringValues == null) { |
| 2842 |
_reportError("Encountered shared text value reference, even though document header did not declared shared text value references may be included"); |
| 2843 |
} |
| 2844 |
_reportError("Invalid shared text value reference "+index+"; only got "+_seenStringValueCount+" names in buffer (invalid content)"); |
| 2845 |
} |
| 2846 |
|
| 2847 |
protected void _reportInvalidChar(int c) throws JsonParseException |
| 2848 |
{ |
| 2849 |
// Either invalid WS or illegal UTF-8 start char |
| 2850 |
if (c < ' ') { |
| 2851 |
_throwInvalidSpace(c); |
| 2852 |
} |
| 2853 |
_reportInvalidInitial(c); |
| 2854 |
} |
| 2855 |
|
| 2856 |
protected void _reportInvalidInitial(int mask) throws JsonParseException { |
| 2857 |
_reportError("Invalid UTF-8 start byte 0x"+Integer.toHexString(mask)); |
| 2858 |
} |
| 2859 |
|
| 2860 |
protected void _reportInvalidOther(int mask) throws JsonParseException { |
| 2861 |
_reportError("Invalid UTF-8 middle byte 0x"+Integer.toHexString(mask)); |
| 2862 |
} |
| 2863 |
|
| 2864 |
protected void _reportInvalidOther(int mask, int ptr) throws JsonParseException { |
| 2865 |
_inputPtr = ptr; |
| 2866 |
_reportInvalidOther(mask); |
| 2867 |
} |
| 2868 |
|
| 2869 |
/* |
| 2870 |
/********************************************************** |
| 2871 |
/* Internal methods, other |
| 2872 |
/********************************************************** |
| 2873 |
*/ |
| 2874 |
|
| 2875 |
private final JsonToken _eofAsNextToken() throws IOException { |
| 2876 |
if (!_parsingContext.inRoot()) { |
| 2877 |
_handleEOF(); |
| 2878 |
} |
| 2879 |
close(); |
| 2880 |
return (_currToken = null); |
| 2881 |
} |
| 2882 |
} |
| 2883 |
|