FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java/src/main/java/com/jsoniter/output/JsonStream.java at master · iCodeIN/java · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
iCodeIN
/
java
Public
forked from
json-iterator/java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
java
/
src
/
main
/
java
/
com
/
jsoniter
/
output
/
JsonStream.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
523 lines (452 loc) · 14.6 KB
Breadcrumbs
java
/
src
/
main
/
java
/
com
/
jsoniter
/
output
/
JsonStream.java
Copy path
File metadata and controls
523 lines (452 loc) · 14.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
package
com
.
jsoniter
.
output
;
import
com
.
jsoniter
.
any
.
Any
;
import
com
.
jsoniter
.
spi
.*;
import
java
.
io
.
IOException
;
import
java
.
io
.
OutputStream
;
import
java
.
lang
.
reflect
.
Type
;
public
class
JsonStream
extends
OutputStream
{
public
Config
configCache
;
int
indention
=
0
;
private
OutputStream
out
;
byte
buf
[];
int
count
;
public
JsonStream
(
OutputStream
out
,
int
bufSize
) {
if
(
bufSize
<
32
) {
throw
new
JsonException
(
"buffer size must be larger than 32: "
+
bufSize
);
}
this
.
out
=
out
;
this
.
buf
=
new
byte
[
bufSize
];
}
public
void
reset
(
OutputStream
out
) {
this
.
out
=
out
;
this
.
count
=
0
;
}
final
void
ensure
(
int
minimal
)
throws
IOException
{
int
available
=
buf
.
length
-
count
;
if
(
available
<
minimal
) {
if
(
count
>
1024
) {
flushBuffer
();
}
growAtLeast
(
minimal
);
}
}
private
final
void
growAtLeast
(
int
minimal
) {
int
toGrow
=
buf
.
length
;
if
(
toGrow
<
minimal
) {
toGrow
=
minimal
;
}
byte
[]
newBuf
=
new
byte
[
buf
.
length
+
toGrow
];
System
.
arraycopy
(
buf
,
0
,
newBuf
,
0
,
buf
.
length
);
buf
=
newBuf
;
}
public
final
void
write
(
int
b
)
throws
IOException
{
ensure
(
1
);
buf
[
count
++] = (
byte
)
b
;
}
public
final
void
write
(
byte
b1
,
byte
b2
)
throws
IOException
{
ensure
(
2
);
buf
[
count
++] =
b1
;
buf
[
count
++] =
b2
;
}
public
final
void
write
(
byte
b1
,
byte
b2
,
byte
b3
)
throws
IOException
{
ensure
(
3
);
buf
[
count
++] =
b1
;
buf
[
count
++] =
b2
;
buf
[
count
++] =
b3
;
}
public
final
void
write
(
byte
b1
,
byte
b2
,
byte
b3
,
byte
b4
)
throws
IOException
{
ensure
(
4
);
buf
[
count
++] =
b1
;
buf
[
count
++] =
b2
;
buf
[
count
++] =
b3
;
buf
[
count
++] =
b4
;
}
public
final
void
write
(
byte
b1
,
byte
b2
,
byte
b3
,
byte
b4
,
byte
b5
)
throws
IOException
{
ensure
(
5
);
buf
[
count
++] =
b1
;
buf
[
count
++] =
b2
;
buf
[
count
++] =
b3
;
buf
[
count
++] =
b4
;
buf
[
count
++] =
b5
;
}
public
final
void
write
(
byte
b1
,
byte
b2
,
byte
b3
,
byte
b4
,
byte
b5
,
byte
b6
)
throws
IOException
{
ensure
(
6
);
buf
[
count
++] =
b1
;
buf
[
count
++] =
b2
;
buf
[
count
++] =
b3
;
buf
[
count
++] =
b4
;
buf
[
count
++] =
b5
;
buf
[
count
++] =
b6
;
}
public
final
void
write
(
byte
b
[],
int
off
,
int
len
)
throws
IOException
{
if
(
out
==
null
) {
ensure
(
len
);
}
else
{
if
(
len
>=
buf
.
length
-
count
) {
if
(
len
>=
buf
.
length
) {
/* If the request length exceeds the size of the output buffer,
flush the output buffer and then write the data directly.
In this way buffered streams will cascade harmlessly. */
flushBuffer
();
out
.
write
(
b
,
off
,
len
);
return
;
}
flushBuffer
();
}
}
System
.
arraycopy
(
b
,
off
,
buf
,
count
,
len
);
count
+=
len
;
}
public
void
flush
()
throws
IOException
{
flushBuffer
();
out
.
flush
();
}
@
Override
public
void
close
()
throws
IOException
{
if
(
out
==
null
) {
return
;
}
if
(
count
>
0
) {
flushBuffer
();
}
out
.
close
();
this
.
out
=
null
;
count
=
0
;
}
final
void
flushBuffer
()
throws
IOException
{
if
(
out
==
null
) {
return
;
}
out
.
write
(
buf
,
0
,
count
);
count
=
0
;
}
public
final
void
writeVal
(
String
val
)
throws
IOException
{
if
(
val
==
null
) {
writeNull
();
}
else
{
StreamImplString
.
writeString
(
this
,
val
);
}
}
public
final
void
writeRaw
(
String
val
)
throws
IOException
{
writeRaw
(
val
,
val
.
length
());
}
public
final
void
writeRaw
(
String
val
,
int
remaining
)
throws
IOException
{
if
(
out
==
null
) {
ensure
(
remaining
);
val
.
getBytes
(
0
,
remaining
,
buf
,
count
);
count
+=
remaining
;
return
;
}
int
i
=
0
;
for
(; ; ) {
int
available
=
buf
.
length
-
count
;
if
(
available
<
remaining
) {
remaining
-=
available
;
int
j
=
i
+
available
;
val
.
getBytes
(
i
,
j
,
buf
,
count
);
count
=
buf
.
length
;
flushBuffer
();
i
=
j
;
}
else
{
int
j
=
i
+
remaining
;
val
.
getBytes
(
i
,
j
,
buf
,
count
);
count
+=
remaining
;
return
;
}
}
}
public
final
void
writeVal
(
Boolean
val
)
throws
IOException
{
if
(
val
==
null
) {
writeNull
();
}
else
{
if
(
val
) {
writeTrue
();
}
else
{
writeFalse
();
}
}
}
public
final
void
writeVal
(
boolean
val
)
throws
IOException
{
if
(
val
) {
writeTrue
();
}
else
{
writeFalse
();
}
}
public
final
void
writeTrue
()
throws
IOException
{
write
((
byte
)
't'
, (
byte
)
'r'
, (
byte
)
'u'
, (
byte
)
'e'
);
}
public
final
void
writeFalse
()
throws
IOException
{
write
((
byte
)
'f'
, (
byte
)
'a'
, (
byte
)
'l'
, (
byte
)
's'
, (
byte
)
'e'
);
}
public
final
void
writeVal
(
Short
val
)
throws
IOException
{
if
(
val
==
null
) {
writeNull
();
}
else
{
writeVal
(
val
.
intValue
());
}
}
public
final
void
writeVal
(
short
val
)
throws
IOException
{
writeVal
((
int
)
val
);
}
public
final
void
writeVal
(
Integer
val
)
throws
IOException
{
if
(
val
==
null
) {
writeNull
();
}
else
{
writeVal
(
val
.
intValue
());
}
}
public
final
void
writeVal
(
int
val
)
throws
IOException
{
StreamImplNumber
.
writeInt
(
this
,
val
);
}
public
final
void
writeVal
(
Long
val
)
throws
IOException
{
if
(
val
==
null
) {
writeNull
();
}
else
{
writeVal
(
val
.
longValue
());
}
}
public
final
void
writeVal
(
long
val
)
throws
IOException
{
StreamImplNumber
.
writeLong
(
this
,
val
);
}
public
final
void
writeVal
(
Float
val
)
throws
IOException
{
if
(
val
==
null
) {
writeNull
();
}
else
{
writeVal
(
val
.
floatValue
());
}
}
public
final
void
writeVal
(
float
val
)
throws
IOException
{
StreamImplNumber
.
writeFloat
(
this
,
val
);
}
public
final
void
writeVal
(
Double
val
)
throws
IOException
{
if
(
val
==
null
) {
writeNull
();
}
else
{
writeVal
(
val
.
doubleValue
());
}
}
public
final
void
writeVal
(
double
val
)
throws
IOException
{
StreamImplNumber
.
writeDouble
(
this
,
val
);
}
public
final
void
writeVal
(
Any
val
)
throws
IOException
{
val
.
writeTo
(
this
);
}
public
final
void
writeNull
()
throws
IOException
{
write
((
byte
)
'n'
, (
byte
)
'u'
, (
byte
)
'l'
, (
byte
)
'l'
);
}
public
final
void
writeEmptyObject
()
throws
IOException
{
write
((
byte
)
'{'
, (
byte
)
'}'
);
}
public
final
void
writeEmptyArray
()
throws
IOException
{
write
((
byte
)
'['
, (
byte
)
']'
);
}
public
final
void
writeArrayStart
()
throws
IOException
{
indention
+=
currentConfig
().
indentionStep
();
write
(
'['
);
}
public
final
void
writeMore
()
throws
IOException
{
write
(
','
);
writeIndention
();
}
public
void
writeIndention
()
throws
IOException
{
writeIndention
(
0
);
}
private
void
writeIndention
(
int
delta
)
throws
IOException
{
if
(
indention
==
0
) {
return
;
}
write
(
'\n'
);
int
toWrite
=
indention
-
delta
;
ensure
(
toWrite
);
for
(
int
i
=
0
;
i
<
toWrite
&&
count
<
buf
.
length
;
i
++) {
buf
[
count
++] =
' '
;
}
}
public
final
void
writeArrayEnd
()
throws
IOException
{
int
indentionStep
=
currentConfig
().
indentionStep
();
writeIndention
(
indentionStep
);
indention
-=
indentionStep
;
write
(
']'
);
}
public
final
void
writeObjectStart
()
throws
IOException
{
int
indentionStep
=
currentConfig
().
indentionStep
();
indention
+=
indentionStep
;
write
(
'{'
);
}
public
final
void
writeObjectField
(
String
field
)
throws
IOException
{
writeVal
(
field
);
if
(
indention
>
0
) {
write
((
byte
)
':'
, (
byte
)
' '
);
}
else
{
write
(
':'
);
}
}
public
final
void
writeObjectField
(
Object
key
)
throws
IOException
{
Encoder
encoder
=
MapKeyEncoders
.
registerOrGetExisting
(
key
.
getClass
());
writeObjectField
(
key
,
encoder
);
}
public
final
void
writeObjectField
(
Object
key
,
Encoder
keyEncoder
)
throws
IOException
{
keyEncoder
.
encode
(
key
,
this
);
if
(
indention
>
0
) {
write
((
byte
)
':'
, (
byte
)
' '
);
}
else
{
write
(
':'
);
}
}
public
final
void
writeObjectEnd
()
throws
IOException
{
int
indentionStep
=
currentConfig
().
indentionStep
();
writeIndention
(
indentionStep
);
indention
-=
indentionStep
;
write
(
'}'
);
}
public
final
void
writeVal
(
Object
obj
)
throws
IOException
{
if
(
obj
==
null
) {
writeNull
();
return
;
}
Class
<?>
clazz
=
obj
.
getClass
();
String
cacheKey
=
currentConfig
().
getEncoderCacheKey
(
clazz
);
Codegen
.
getEncoder
(
cacheKey
,
clazz
).
encode
(
obj
,
this
);
}
public
final
<
T
>
void
writeVal
(
TypeLiteral
<
T
>
typeLiteral
,
T
obj
)
throws
IOException
{
if
(
null
==
obj
) {
writeNull
();
}
else
{
Config
config
=
currentConfig
();
String
cacheKey
=
config
.
getEncoderCacheKey
(
typeLiteral
.
getType
());
Codegen
.
getEncoder
(
cacheKey
,
typeLiteral
.
getType
()).
encode
(
obj
,
this
);
}
}
public
final
<
T
>
void
writeVal
(
Type
type
,
T
obj
)
throws
IOException
{
if
(
null
==
obj
) {
writeNull
();
}
else
{
Config
config
=
currentConfig
();
String
cacheKey
=
config
.
getEncoderCacheKey
(
type
);
Codegen
.
getEncoder
(
cacheKey
,
type
).
encode
(
obj
,
this
);
}
}
public
Config
currentConfig
() {
if
(
configCache
!=
null
) {
return
configCache
;
}
configCache
=
JsoniterSpi
.
getCurrentConfig
();
return
configCache
;
}
public
static
void
serialize
(
Config
config
,
Object
obj
,
OutputStream
out
) {
JsoniterSpi
.
setCurrentConfig
(
config
);
try
{
serialize
(
obj
,
out
);
}
finally
{
JsoniterSpi
.
clearCurrentConfig
();
}
}
public
static
void
serialize
(
Object
obj
,
OutputStream
out
) {
JsonStream
stream
=
JsonStreamPool
.
borrowJsonStream
();
try
{
try
{
stream
.
reset
(
out
);
stream
.
writeVal
(
obj
);
}
finally
{
stream
.
close
();
}
}
catch
(
IOException
e
) {
throw
new
JsonException
(
e
);
}
finally
{
JsonStreamPool
.
returnJsonStream
(
stream
);
}
}
public
static
void
serialize
(
Config
config
,
TypeLiteral
typeLiteral
,
Object
obj
,
OutputStream
out
) {
JsoniterSpi
.
setCurrentConfig
(
config
);
try
{
serialize
(
typeLiteral
,
obj
,
out
);
}
finally
{
JsoniterSpi
.
clearCurrentConfig
();
}
}
public
static
void
serialize
(
TypeLiteral
typeLiteral
,
Object
obj
,
OutputStream
out
) {
JsonStream
stream
=
JsonStreamPool
.
borrowJsonStream
();
try
{
try
{
stream
.
reset
(
out
);
stream
.
writeVal
(
typeLiteral
,
obj
);
}
finally
{
stream
.
close
();
}
}
catch
(
IOException
e
) {
throw
new
JsonException
(
e
);
}
finally
{
JsonStreamPool
.
returnJsonStream
(
stream
);
}
}
public
static
void
serialize
(
Type
type
,
Object
obj
,
OutputStream
out
) {
JsonStream
stream
=
JsonStreamPool
.
borrowJsonStream
();
try
{
try
{
stream
.
reset
(
out
);
stream
.
writeVal
(
type
,
obj
);
}
finally
{
stream
.
close
();
}
}
catch
(
IOException
e
) {
throw
new
JsonException
(
e
);
}
finally
{
JsonStreamPool
.
returnJsonStream
(
stream
);
}
}
public
static
String
serialize
(
Config
config
,
Object
obj
) {
JsoniterSpi
.
setCurrentConfig
(
config
);
try
{
return
serialize
(
config
.
escapeUnicode
(),
obj
.
getClass
(),
obj
);
}
finally
{
JsoniterSpi
.
clearCurrentConfig
();
}
}
public
static
String
serialize
(
Object
obj
) {
return
serialize
(
JsoniterSpi
.
getCurrentConfig
().
escapeUnicode
(),
obj
.
getClass
(),
obj
);
}
public
static
String
serialize
(
Config
config
,
TypeLiteral
typeLiteral
,
Object
obj
) {
JsoniterSpi
.
setCurrentConfig
(
config
);
try
{
return
serialize
(
config
.
escapeUnicode
(),
typeLiteral
.
getType
(),
obj
);
}
finally
{
JsoniterSpi
.
clearCurrentConfig
();
}
}
public
static
String
serialize
(
TypeLiteral
typeLiteral
,
Object
obj
) {
return
serialize
(
JsoniterSpi
.
getCurrentConfig
().
escapeUnicode
(),
typeLiteral
.
getType
(),
obj
);
}
public
static
String
serialize
(
boolean
escapeUnicode
,
Type
type
,
Object
obj
) {
JsonStream
stream
=
JsonStreamPool
.
borrowJsonStream
();
try
{
stream
.
reset
(
null
);
stream
.
writeVal
(
type
,
obj
);
if
(
escapeUnicode
) {
return
new
String
(
stream
.
buf
,
0
,
stream
.
count
);
}
else
{
return
new
String
(
stream
.
buf
,
0
,
stream
.
count
,
"UTF8"
);
}
}
catch
(
IOException
e
) {
throw
new
JsonException
(
e
);
}
finally
{
JsonStreamPool
.
returnJsonStream
(
stream
);
}
}
public
static
void
setMode
(
EncodingMode
mode
) {
Config
newConfig
=
JsoniterSpi
.
getDefaultConfig
().
copyBuilder
().
encodingMode
(
mode
).
build
();
JsoniterSpi
.
setDefaultConfig
(
newConfig
);
JsoniterSpi
.
setCurrentConfig
(
newConfig
);
}
public
static
void
setIndentionStep
(
int
indentionStep
) {
Config
newConfig
=
JsoniterSpi
.
getDefaultConfig
().
copyBuilder
().
indentionStep
(
indentionStep
).
build
();
JsoniterSpi
.
setDefaultConfig
(
newConfig
);
JsoniterSpi
.
setCurrentConfig
(
newConfig
);
}
public
static
void
registerNativeEncoder
(
Class
clazz
,
Encoder
.
ReflectionEncoder
encoder
) {
CodegenImplNative
.
NATIVE_ENCODERS
.
put
(
clazz
,
encoder
);
}
public
Slice
buffer
() {
return
new
Slice
(
buf
,
0
,
count
);
}
}
Back
|
FazBrowse Home
|
New Git URL