| 1 |
package com.fasterxml.jackson.dataformat.smile; |
| 2 |
|
| 3 |
/** |
| 4 |
* Constants used by {@link SmileGenerator} and {@link SmileParser} |
| 5 |
* |
| 6 |
* @author tatu |
| 7 |
*/ |
| 8 |
public final class SmileConstants |
| 9 |
{ |
| 10 |
/* |
| 11 |
/********************************************************** |
| 12 |
/* Thresholds |
| 13 |
/********************************************************** |
| 14 |
*/ |
| 15 |
|
| 16 |
/** |
| 17 |
* Encoding has special "short" forms for value Strings that can |
| 18 |
* be represented by 64 bytes of UTF-8 or less. |
| 19 |
*/ |
| 20 |
public final static int MAX_SHORT_VALUE_STRING_BYTES = 64; |
| 21 |
|
| 22 |
/** |
| 23 |
* Maximum byte length for short ASCII names is 64. |
| 24 |
*/ |
| 25 |
public final static int MAX_SHORT_NAME_ASCII_BYTES = 64; |
| 26 |
|
| 27 |
/** |
| 28 |
* Maximum byte length for short non-ASCII names is slightly |
| 29 |
* less due to having to reserve bytes 0xF8 and above (but |
| 30 |
* we get one more as values 0 and 1 are not valid) |
| 31 |
*/ |
| 32 |
public final static int MAX_SHORT_NAME_UNICODE_BYTES = 56; |
| 33 |
|
| 34 |
/** |
| 35 |
* Regardless of ASCII/non-ASCII aspect, maximum byte length for any |
| 36 |
* short name is then 64 bytes. |
| 37 |
*/ |
| 38 |
public final static int MAX_SHORT_NAME_ANY_BYTES = 64; |
| 39 |
|
| 40 |
/** |
| 41 |
* Longest back reference we use for field names is 10 bits; no point |
| 42 |
* in keeping much more around |
| 43 |
*/ |
| 44 |
public final static int MAX_SHARED_NAMES = 1024; |
| 45 |
|
| 46 |
/** |
| 47 |
* Longest back reference we use for short shared String values is 10 bits, |
| 48 |
* so up to (1 << 10) values to keep track of. |
| 49 |
*/ |
| 50 |
public final static int MAX_SHARED_STRING_VALUES = 1024; |
| 51 |
|
| 52 |
/** |
| 53 |
* Also: whereas we can refer to names of any length, we will only consider |
| 54 |
* text values that are considered "tiny" or "short" (ones encoded with |
| 55 |
* length prefix); this value thereby has to be maximum length of Strings |
| 56 |
* that can be encoded as such. |
| 57 |
*/ |
| 58 |
public final static int MAX_SHARED_STRING_LENGTH_BYTES = 65; |
| 59 |
|
| 60 |
/** |
| 61 |
* And to make encoding logic tight and simple, we can always |
| 62 |
* require that output buffer has this amount of space |
| 63 |
* available before encoding possibly short String (3 bytes since |
| 64 |
* longest UTF-8 encoded Java char is 3 bytes). |
| 65 |
* Two extra bytes need to be reserved as well; first for token indicator, |
| 66 |
* and second for terminating null byte (in case it's not a short String after all) |
| 67 |
*/ |
| 68 |
public final static int MIN_BUFFER_FOR_POSSIBLE_SHORT_STRING = 1 + (3 * 65); |
| 69 |
|
| 70 |
/* |
| 71 |
/********************************************************** |
| 72 |
/* Byte markers |
| 73 |
/********************************************************** |
| 74 |
*/ |
| 75 |
|
| 76 |
/** |
| 77 |
* We need a byte marker to denote end of variable-length Strings. Although |
| 78 |
* null byte is commonly used, let's try to avoid using it since it can't |
| 79 |
* be embedded in Web Sockets content (similarly, 0xFF can't). There are |
| 80 |
* multiple candidates for bytes UTF-8 can not have; 0xFC is chosen to |
| 81 |
* allow reasonable ordering (highest values meaning most significant |
| 82 |
* framing function; 0xFF being end-of-content and so on) |
| 83 |
*/ |
| 84 |
public final static int INT_MARKER_END_OF_STRING = 0xFC; |
| 85 |
|
| 86 |
public final static byte BYTE_MARKER_END_OF_STRING = (byte) INT_MARKER_END_OF_STRING; |
| 87 |
|
| 88 |
/** |
| 89 |
* In addition we can use a marker to allow simple framing; splitting |
| 90 |
* of physical data (like file) into distinct logical sections like |
| 91 |
* JSON documents. 0xFF makes sense here since it is also used |
| 92 |
* as end marker for Web Sockets. |
| 93 |
*/ |
| 94 |
public final static byte BYTE_MARKER_END_OF_CONTENT = (byte) 0xFF; |
| 95 |
|
| 96 |
/* |
| 97 |
/********************************************************** |
| 98 |
/* Format header: put smile on your data... |
| 99 |
/********************************************************** |
| 100 |
*/ |
| 101 |
|
| 102 |
/** |
| 103 |
* First byte of data header (0x3A) |
| 104 |
*/ |
| 105 |
public final static byte HEADER_BYTE_1 = (byte) ':'; |
| 106 |
|
| 107 |
/** |
| 108 |
* Second byte of data header (0x29) |
| 109 |
*/ |
| 110 |
public final static byte HEADER_BYTE_2 = (byte) ')'; |
| 111 |
|
| 112 |
/** |
| 113 |
* Third byte of data header |
| 114 |
*/ |
| 115 |
public final static byte HEADER_BYTE_3 = (byte) '\n'; |
| 116 |
|
| 117 |
/** |
| 118 |
* Current version consists of four zero bits (nibble) |
| 119 |
*/ |
| 120 |
public final static int HEADER_VERSION_0 = 0x0; |
| 121 |
|
| 122 |
/** |
| 123 |
* Fourth byte of data header; contains version nibble, may |
| 124 |
* have flags |
| 125 |
*/ |
| 126 |
public final static byte HEADER_BYTE_4 = (HEADER_VERSION_0 << 4); |
| 127 |
|
| 128 |
/** |
| 129 |
* Indicator bit that indicates whether encoded content may |
| 130 |
* have Shared names (back references to recently encoded field |
| 131 |
* names). If no header available, must be |
| 132 |
* processed as if this was set to true. |
| 133 |
* If (and only if) header exists, and value is 0, can parser |
| 134 |
* omit storing of seen names, as it is guaranteed that no back |
| 135 |
* references exist. |
| 136 |
*/ |
| 137 |
public final static int HEADER_BIT_HAS_SHARED_NAMES = 0x01; |
| 138 |
|
| 139 |
/** |
| 140 |
* Indicator bit that indicates whether encoded content may |
| 141 |
* have shared String values (back references to recently encoded |
| 142 |
* 'short' String values, where short is defined as 64 bytes or less). |
| 143 |
* If no header available, can be assumed to be 0 (false). |
| 144 |
* If header exists, and bit value is 1, parsers has to store up |
| 145 |
* to 1024 most recently seen distinct short String values. |
| 146 |
*/ |
| 147 |
public final static int HEADER_BIT_HAS_SHARED_STRING_VALUES = 0x02; |
| 148 |
|
| 149 |
/** |
| 150 |
* Indicator bit that indicates whether encoded content may |
| 151 |
* contain raw (unquoted) binary values. |
| 152 |
* If no header available, can be assumed to be 0 (false). |
| 153 |
* If header exists, and bit value is 1, parser can not assume that |
| 154 |
* specific byte values always have default meaning (specifically, |
| 155 |
* content end marker 0xFF and header signature can be contained |
| 156 |
* in binary values) |
| 157 |
*<p> |
| 158 |
* Note that this bit being true does not automatically mean that |
| 159 |
* such raw binary content indeed exists; just that it may exist. |
| 160 |
* This because header is written before any binary data may be |
| 161 |
* written. |
| 162 |
*/ |
| 163 |
public final static int HEADER_BIT_HAS_RAW_BINARY = 0x04; |
| 164 |
|
| 165 |
/* |
| 166 |
/********************************************************** |
| 167 |
/* Type prefixes: 3 MSB of token byte |
| 168 |
/********************************************************** |
| 169 |
*/ |
| 170 |
|
| 171 |
public final static int TOKEN_PREFIX_INTEGER = 0x24; |
| 172 |
|
| 173 |
public final static int TOKEN_PREFIX_FP = 0x28; |
| 174 |
|
| 175 |
// Shared strings are back references for last 63 short (< 64 byte) string values |
| 176 |
// NOTE: 0x00 is reserved, not used with current version (may be used in future) |
| 177 |
public final static int TOKEN_PREFIX_SHARED_STRING_SHORT = 0x00; |
| 178 |
// literals are put between 0x20 and 0x3F to reserve markers (smiley), along with ints/doubles |
| 179 |
//public final static int TOKEN_PREFIX_MISC_NUMBERS = 0x20; |
| 180 |
|
| 181 |
public final static int TOKEN_PREFIX_SHARED_STRING_LONG = 0xEC; |
| 182 |
|
| 183 |
public final static int TOKEN_PREFIX_TINY_ASCII = 0x40; |
| 184 |
public final static int TOKEN_PREFIX_SMALL_ASCII = 0x60; |
| 185 |
public final static int TOKEN_PREFIX_TINY_UNICODE = 0x80; |
| 186 |
public final static int TOKEN_PREFIX_SHORT_UNICODE = 0xA0; |
| 187 |
|
| 188 |
// Small ints are 4-bit (-16 to +15) integer constants |
| 189 |
public final static int TOKEN_PREFIX_SMALL_INT = 0xC0; |
| 190 |
|
| 191 |
// And misc types have empty at the end too, to reserve 0xF8 - 0xFF |
| 192 |
public final static int TOKEN_PREFIX_MISC_OTHER = 0xE0; |
| 193 |
|
| 194 |
/* |
| 195 |
/********************************************************** |
| 196 |
/* Token literals, normal mode |
| 197 |
/********************************************************** |
| 198 |
*/ |
| 199 |
|
| 200 |
// First, non-structured literals |
| 201 |
|
| 202 |
public final static byte TOKEN_LITERAL_EMPTY_STRING = 0x20; |
| 203 |
public final static byte TOKEN_LITERAL_NULL = 0x21; |
| 204 |
public final static byte TOKEN_LITERAL_FALSE = 0x22; |
| 205 |
public final static byte TOKEN_LITERAL_TRUE = 0x23; |
| 206 |
|
| 207 |
// And then structured literals |
| 208 |
|
| 209 |
public final static byte TOKEN_LITERAL_START_ARRAY = (byte) 0xF8; |
| 210 |
public final static byte TOKEN_LITERAL_END_ARRAY = (byte) 0xF9; |
| 211 |
public final static byte TOKEN_LITERAL_START_OBJECT = (byte) 0xFA; |
| 212 |
public final static byte TOKEN_LITERAL_END_OBJECT = (byte) 0xFB; |
| 213 |
|
| 214 |
/* |
| 215 |
/********************************************************** |
| 216 |
/* Subtype constants for misc text/binary types |
| 217 |
/********************************************************** |
| 218 |
*/ |
| 219 |
|
| 220 |
public final static int INT_MISC_BINARY_7BIT = 0xE8; |
| 221 |
|
| 222 |
public final static int INT_MISC_BINARY_RAW = 0xFD; |
| 223 |
|
| 224 |
/** |
| 225 |
* Type (for misc, other) used for |
| 226 |
* variable length UTF-8 encoded text, when it is known to only contain ASCII chars. |
| 227 |
* Note: 2 LSB are reserved for future use; must be zeroes for now |
| 228 |
*/ |
| 229 |
public final static byte TOKEN_MISC_LONG_TEXT_ASCII = (byte) 0xE0; |
| 230 |
|
| 231 |
/** |
| 232 |
* Type (for misc, other) used |
| 233 |
* for variable length UTF-8 encoded text, when it is NOT known to only contain ASCII chars |
| 234 |
* (which means it MAY have multi-byte characters) |
| 235 |
* Note: 2 LSB are reserved for future use; must be zeroes for now |
| 236 |
*/ |
| 237 |
public final static byte TOKEN_MISC_LONG_TEXT_UNICODE = (byte) 0xE4; |
| 238 |
|
| 239 |
/** |
| 240 |
* Type (for misc, other) used |
| 241 |
* for "safe" (encoded by only using 7 LSB, giving 8/7 expansion ratio). |
| 242 |
* This is usually done to ensure that certain bytes are never included |
| 243 |
* in encoded data (like 0xFF) |
| 244 |
* Note: 2 LSB are reserved for future use; must be zeroes for now |
| 245 |
*/ |
| 246 |
public final static byte TOKEN_MISC_BINARY_7BIT = (byte) INT_MISC_BINARY_7BIT; |
| 247 |
|
| 248 |
/** |
| 249 |
* Raw binary data marker is specifically chosen as separate from |
| 250 |
* other types, since it can have significant impact on framing |
| 251 |
* (or rather fast scanning based on structure and framing markers). |
| 252 |
*/ |
| 253 |
public final static byte TOKEN_MISC_BINARY_RAW = (byte) INT_MISC_BINARY_RAW; |
| 254 |
|
| 255 |
/* |
| 256 |
/********************************************************** |
| 257 |
/* Modifiers for numeric entries |
| 258 |
/********************************************************** |
| 259 |
*/ |
| 260 |
|
| 261 |
/** |
| 262 |
* Numeric subtype (2 LSB) |
| 263 |
* indicating 32-bit integer (int) |
| 264 |
*/ |
| 265 |
public final static int TOKEN_MISC_INTEGER_32 = 0x00; |
| 266 |
|
| 267 |
/** |
| 268 |
* Numeric subtype (2 LSB) |
| 269 |
* indicating 32-bit integer (long) |
| 270 |
*/ |
| 271 |
public final static int TOKEN_MISC_INTEGER_64 = 0x01; |
| 272 |
|
| 273 |
/** |
| 274 |
* Numeric subtype (2 LSB) for |
| 275 |
* indicating {@link java.math.BigInteger} type. |
| 276 |
*/ |
| 277 |
public final static int TOKEN_MISC_INTEGER_BIG = 0x02; |
| 278 |
|
| 279 |
// Note: type 3 (0xF3) reserved for future use |
| 280 |
|
| 281 |
/** |
| 282 |
* Numeric subtype (2 LSB) for |
| 283 |
* indicating 32-bit IEEE single precision floating point number. |
| 284 |
*/ |
| 285 |
public final static int TOKEN_MISC_FLOAT_32 = 0x00; |
| 286 |
|
| 287 |
/** |
| 288 |
* Numeric subtype (2 LSB) |
| 289 |
* indicating 64-bit IEEE double precision floating point number. |
| 290 |
*/ |
| 291 |
public final static int TOKEN_MISC_FLOAT_64 = 0x01; |
| 292 |
|
| 293 |
/** |
| 294 |
* Numeric subtype (2 LSB) for |
| 295 |
* indicating {@link java.math.BigDecimal} type. |
| 296 |
*/ |
| 297 |
public final static int TOKEN_MISC_FLOAT_BIG = 0x02; |
| 298 |
|
| 299 |
// Note: type 3 (0xF7) reserved for future use |
| 300 |
|
| 301 |
/* |
| 302 |
/********************************************************** |
| 303 |
/* Token types for keys |
| 304 |
/********************************************************** |
| 305 |
*/ |
| 306 |
|
| 307 |
/** |
| 308 |
* Let's use same code for empty key as for empty String value |
| 309 |
*/ |
| 310 |
public final static byte TOKEN_KEY_EMPTY_STRING = 0x20; |
| 311 |
|
| 312 |
public final static int TOKEN_PREFIX_KEY_SHARED_LONG = 0x30; |
| 313 |
|
| 314 |
public final static byte TOKEN_KEY_LONG_STRING = 0x34; |
| 315 |
|
| 316 |
public final static int TOKEN_PREFIX_KEY_SHARED_SHORT = 0x40; |
| 317 |
|
| 318 |
public final static int TOKEN_PREFIX_KEY_ASCII = 0x80; |
| 319 |
|
| 320 |
public final static int TOKEN_PREFIX_KEY_UNICODE = 0xC0; |
| 321 |
|
| 322 |
/* |
| 323 |
/********************************************************** |
| 324 |
/* Basic UTF-8 decode/encode table |
| 325 |
/********************************************************** |
| 326 |
*/ |
| 327 |
|
| 328 |
/** |
| 329 |
* Additionally we can combine UTF-8 decoding info into similar |
| 330 |
* data table. |
| 331 |
* Values indicate "byte length - 1"; meaning -1 is used for |
| 332 |
* invalid bytes, 0 for single-byte codes, 1 for 2-byte codes |
| 333 |
* and 2 for 3-byte codes. |
| 334 |
*/ |
| 335 |
public final static int[] sUtf8UnitLengths; |
| 336 |
static { |
| 337 |
int[] table = new int[256]; |
| 338 |
for (int c = 128; c < 256; ++c) { |
| 339 |
int code; |
| 340 |
|
| 341 |
// We'll add number of bytes needed for decoding |
| 342 |
if ((c & 0xE0) == 0xC0) { // 2 bytes (0x0080 - 0x07FF) |
| 343 |
code = 1; |
| 344 |
} else if ((c & 0xF0) == 0xE0) { // 3 bytes (0x0800 - 0xFFFF) |
| 345 |
code = 2; |
| 346 |
} else if ((c & 0xF8) == 0xF0) { |
| 347 |
// 4 bytes; double-char with surrogates and all... |
| 348 |
code = 3; |
| 349 |
} else { |
| 350 |
// And -1 seems like a good "universal" error marker... |
| 351 |
code = -1; |
| 352 |
} |
| 353 |
table[c] = code; |
| 354 |
} |
| 355 |
sUtf8UnitLengths = table; |
| 356 |
} |
| 357 |
} |
| 358 |
|