FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JSON-java/zip/Compressor.java at master · pradeepsimhanp/JSON-java · GitHub
pradeepsimhanp
/
JSON-java
Public
forked from
stleary/JSON-java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
JSON-java
/
zip
/
Compressor.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
575 lines (522 loc) · 16.4 KB
Breadcrumbs
JSON-java
/
zip
/
Compressor.java
Copy path
File metadata and controls
575 lines (522 loc) · 16.4 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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
package
org
.
json
.
zip
;
import
java
.
io
.
IOException
;
import
java
.
util
.
Collection
;
import
java
.
util
.
Iterator
;
import
java
.
util
.
Map
;
import
org
.
json
.
JSONArray
;
import
org
.
json
.
JSONException
;
import
org
.
json
.
JSONObject
;
import
org
.
json
.
Kim
;
/*
Copyright (c) 2013 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/**
* JSONzip is a compression scheme for JSON text.
*
* @author JSON.org
* @version 2013-04-18
*/
/**
* A compressor implements the compression behavior of JSONzip. It provides a
* zip method that takes a JSONObject or JSONArray and delivers a stream of
* bits to a BitWriter.
*
* FOR EVALUATION PURPOSES ONLY. THIS PACKAGE HAS NOT BEEN TESTED ADEQUATELY
* FOR PRODUCTION USE.
*/
public
class
Compressor
extends
JSONzip
{
/**
* A compressor outputs to a BitWriter.
*/
final
BitWriter
bitwriter
;
/**
* Create a new compressor. It may be used for an entire session or
* subsession.
*
* @param bitwriter
* The BitWriter this Compressor will output to. Don't forget to
* flush.
*/
public
Compressor
(
BitWriter
bitwriter
) {
super
();
this
.
bitwriter
=
bitwriter
;
}
/**
* Return a 4 bit code for a character in a JSON number. The digits '0' to
* '9' get the codes 0 to 9. '.' is 10, '-' is 11, '+' is 12, and 'E' or
* 'e' is 13.
*
* @param digit
* An ASCII character from a JSIN number.
* @return
*/
private
static
int
bcd
(
char
digit
) {
if
(
digit
>=
'0'
&&
digit
<=
'9'
) {
return
digit
-
'0'
;
}
switch
(
digit
) {
case
'.'
:
return
10
;
case
'-'
:
return
11
;
case
'+'
:
return
12
;
default
:
return
13
;
}
}
/**
* Finish the final byte and flush the bitwriter. This does the same thing
* as pad(8).
*
* @throws JSONException
*/
public
void
flush
()
throws
JSONException
{
pad
(
8
);
}
/**
* Output a one bit.
*
* @throws IOException
*/
private
void
one
()
throws
JSONException
{
if
(
probe
) {
log
(
1
);
}
write
(
1
,
1
);
}
/**
* Pad the output to fill an allotment of bits.
*
* @param factor
* The size of the bit allotment. A value of 8 will complete and
* flush the current byte. If you don't pad, then some of the
* last bits might not be sent to the Output Stream.
* @throws JSONException
*/
public
void
pad
(
int
factor
)
throws
JSONException
{
try
{
this
.
bitwriter
.
pad
(
factor
);
}
catch
(
Throwable
e
) {
throw
new
JSONException
(
e
);
}
}
/**
* Write a number, using the number of bits necessary to hold the number.
*
* @param integer
* The value to be encoded.
* @param width
* The number of bits to encode the value, between 0 and 32.
* @throws JSONException
*/
private
void
write
(
int
integer
,
int
width
)
throws
JSONException
{
try
{
this
.
bitwriter
.
write
(
integer
,
width
);
if
(
probe
) {
log
(
integer
,
width
);
}
}
catch
(
Throwable
e
) {
throw
new
JSONException
(
e
);
}
}
/**
* Write an integer with Huffman encoding. The bit pattern that is written
* will be determined by the Huffman encoder.
*
* @param integer
* The value to be written.
* @param huff
* The Huffman encoder.
* @throws JSONException
*/
private
void
write
(
int
integer
,
Huff
huff
)
throws
JSONException
{
huff
.
write
(
integer
,
this
.
bitwriter
);
}
/**
* Write each of the bytes in a kim with Huffman encoding.
*
* @param kim
* A kim containing the bytes to be written.
* @param huff
* The Huffman encoder.
* @throws JSONException
*/
private
void
write
(
Kim
kim
,
Huff
huff
)
throws
JSONException
{
write
(
kim
,
0
,
kim
.
length
,
huff
);
}
/**
* Write a range of bytes from a Kim with Huffman encoding.
*
* @param kim
* A Kim containing the bytes to be written.
* @param from
* The index of the first byte to write.
* @param thru
* The index after the last byte to write.
* @param huff
* The Huffman encoder.
* @throws JSONException
*/
private
void
write
(
Kim
kim
,
int
from
,
int
thru
,
Huff
huff
)
throws
JSONException
{
for
(
int
at
=
from
;
at
<
thru
;
at
+=
1
) {
write
(
kim
.
get
(
at
),
huff
);
}
}
/**
* Write an integer, using the number of bits necessary to hold the number
* as determined by its keep, and increment its usage count in the keep.
*
* @param integer
* The value to be encoded.
* @param keep
* The Keep that the integer is one of.
* @throws JSONException
*/
private
void
writeAndTick
(
int
integer
,
Keep
keep
)
throws
JSONException
{
int
width
=
keep
.
bitsize
();
keep
.
tick
(
integer
);
if
(
probe
) {
log
(
"
\"
"
+
keep
.
value
(
integer
) +
"
\"
"
);
}
write
(
integer
,
width
);
}
/**
* Write a JSON Array.
*
* @param jsonarray
* @throws JSONException
*/
private
void
writeArray
(
JSONArray
jsonarray
)
throws
JSONException
{
// JSONzip has three encodings for arrays:
// The array is empty (zipEmptyArray).
// First value in the array is a string (zipArrayString).
// First value in the array is not a string (zipArrayValue).
boolean
stringy
=
false
;
int
length
=
jsonarray
.
length
();
if
(
length
==
0
) {
write
(
zipEmptyArray
,
3
);
}
else
{
Object
value
=
jsonarray
.
get
(
0
);
if
(
value
==
null
) {
value
=
JSONObject
.
NULL
;
}
if
(
value
instanceof
String
) {
stringy
=
true
;
write
(
zipArrayString
,
3
);
writeString
((
String
)
value
);
}
else
{
write
(
zipArrayValue
,
3
);
writeValue
(
value
);
}
for
(
int
i
=
1
;
i
<
length
;
i
+=
1
) {
if
(
probe
) {
log
();
}
value
=
jsonarray
.
get
(
i
);
if
(
value
==
null
) {
value
=
JSONObject
.
NULL
;
}
if
(
value
instanceof
String
!=
stringy
) {
zero
();
}
one
();
if
(
value
instanceof
String
) {
writeString
((
String
)
value
);
}
else
{
writeValue
(
value
);
}
}
zero
();
zero
();
}
}
/**
* Write a JSON value.
*
* @param value
* One of these types: JSONObject, JSONArray (or Map or
* Collection or array), Number (or Integer or Long or Double),
* or String, or Boolean, or JSONObject.NULL, or null.
* @throws JSONException
*/
private
void
writeJSON
(
Object
value
)
throws
JSONException
{
if
(
JSONObject
.
NULL
.
equals
(
value
)) {
write
(
zipNull
,
3
);
}
else
if
(
Boolean
.
FALSE
.
equals
(
value
)) {
write
(
zipFalse
,
3
);
}
else
if
(
Boolean
.
TRUE
.
equals
(
value
)) {
write
(
zipTrue
,
3
);
}
else
{
if
(
value
instanceof
Map
) {
value
=
new
JSONObject
((
Map
)
value
);
}
else
if
(
value
instanceof
Collection
) {
value
=
new
JSONArray
((
Collection
)
value
);
}
else
if
(
value
.
getClass
().
isArray
()) {
value
=
new
JSONArray
(
value
);
}
if
(
value
instanceof
JSONObject
) {
writeObject
((
JSONObject
)
value
);
}
else
if
(
value
instanceof
JSONArray
) {
writeArray
((
JSONArray
)
value
);
}
else
{
throw
new
JSONException
(
"Unrecognized object"
);
}
}
}
/**
* Write the name of an object property. Names have their own Keep and
* Huffman encoder because they are expected to be a more restricted set.
*
* @param name
* @throws JSONException
*/
private
void
writeName
(
String
name
)
throws
JSONException
{
// If this name has already been registered, then emit its integer and
// increment its usage count.
Kim
kim
=
new
Kim
(
name
);
int
integer
=
this
.
namekeep
.
find
(
kim
);
if
(
integer
!=
none
) {
one
();
writeAndTick
(
integer
,
this
.
namekeep
);
}
else
{
// Otherwise, emit the string with Huffman encoding, and register it.
zero
();
write
(
kim
,
this
.
namehuff
);
write
(
end
,
namehuff
);
this
.
namekeep
.
register
(
kim
);
}
}
/**
* Write a JSON object.
*
* @param jsonobject
* @return
* @throws JSONException
*/
private
void
writeObject
(
JSONObject
jsonobject
)
throws
JSONException
{
// JSONzip has two encodings for objects: Empty Objects (zipEmptyObject) and
// non-empty objects (zipObject).
boolean
first
=
true
;
Iterator
keys
=
jsonobject
.
keys
();
while
(
keys
.
hasNext
()) {
if
(
probe
) {
log
(
"
\n
"
);
}
Object
key
=
keys
.
next
();
if
(
key
instanceof
String
) {
if
(
first
) {
first
=
false
;
write
(
zipObject
,
3
);
}
else
{
one
();
}
writeName
((
String
)
key
);
Object
value
=
jsonobject
.
get
((
String
)
key
);
if
(
value
instanceof
String
) {
zero
();
writeString
((
String
)
value
);
}
else
{
one
();
writeValue
(
value
);
}
}
}
if
(
first
) {
write
(
zipEmptyObject
,
3
);
}
else
{
zero
();
}
}
/**
* Write a string.
*
* @param string
* @throws JSONException
*/
private
void
writeString
(
String
string
)
throws
JSONException
{
// Special case for empty strings.
if
(
string
.
length
() ==
0
) {
zero
();
zero
();
write
(
end
,
this
.
substringhuff
);
zero
();
}
else
{
Kim
kim
=
new
Kim
(
string
);
// Look for the string in the strings keep. If it is found, emit its
// integer and count that as a use.
int
integer
=
this
.
stringkeep
.
find
(
kim
);
if
(
integer
!=
none
) {
one
();
writeAndTick
(
integer
,
this
.
stringkeep
);
}
else
{
// But if it is not found, emit the string's substrings. Register the string
// so that the next lookup will succeed.
writeSubstring
(
kim
);
this
.
stringkeep
.
register
(
kim
);
}
}
}
/**
* Write a string, attempting to match registered substrings.
*
* @param kim
* @throws JSONException
*/
private
void
writeSubstring
(
Kim
kim
)
throws
JSONException
{
this
.
substringkeep
.
reserve
();
zero
();
int
from
=
0
;
int
thru
=
kim
.
length
;
int
until
=
thru
-
JSONzip
.
minSubstringLength
;
int
previousFrom
=
none
;
int
previousThru
=
0
;
// Find a substring from the substring keep.
while
(
true
) {
int
at
;
int
integer
=
none
;
for
(
at
=
from
;
at
<=
until
;
at
+=
1
) {
integer
=
this
.
substringkeep
.
match
(
kim
,
at
,
thru
);
if
(
integer
!=
none
) {
break
;
}
}
if
(
integer
==
none
) {
break
;
}
// If a substring is found, emit any characters that were before the matched
// substring. Then emit the substring's integer and loop back to match the
// remainder with another substring.
if
(
from
!=
at
) {
zero
();
write
(
kim
,
from
,
at
,
this
.
substringhuff
);
write
(
end
,
this
.
substringhuff
);
if
(
previousFrom
!=
none
) {
this
.
substringkeep
.
registerOne
(
kim
,
previousFrom
,
previousThru
);
previousFrom
=
none
;
}
}
one
();
writeAndTick
(
integer
,
this
.
substringkeep
);
from
=
at
+
this
.
substringkeep
.
length
(
integer
);
if
(
previousFrom
!=
none
) {
this
.
substringkeep
.
registerOne
(
kim
,
previousFrom
,
previousThru
);
previousFrom
=
none
;
}
previousFrom
=
at
;
previousThru
=
from
+
1
;
}
// If a substring is not found, then emit the remaining characters.
zero
();
if
(
from
<
thru
) {
write
(
kim
,
from
,
thru
,
this
.
substringhuff
);
if
(
previousFrom
!=
none
) {
this
.
substringkeep
.
registerOne
(
kim
,
previousFrom
,
previousThru
);
}
}
write
(
end
,
this
.
substringhuff
);
zero
();
// Register the string's substrings in the trie in hopes of future substring
// matching.
substringkeep
.
registerMany
(
kim
);
}
/**
* Write a value.
*
* @param value
* One of these types: Boolean, Number, etc.
* @throws JSONException
*/
private
void
writeValue
(
Object
value
)
throws
JSONException
{
if
(
value
instanceof
Number
) {
String
string
=
JSONObject
.
numberToString
((
Number
)
value
);
int
integer
=
this
.
values
.
find
(
string
);
if
(
integer
!=
none
) {
write
(
2
,
2
);
writeAndTick
(
integer
,
this
.
values
);
return
;
}
if
(
value
instanceof
Integer
||
value
instanceof
Long
) {
long
longer
= ((
Number
)
value
).
longValue
();
if
(
longer
>=
0
&&
longer
<
int14
) {
write
(
0
,
2
);
if
(
longer
<
int4
) {
zero
();
write
((
int
)
longer
,
4
);
return
;
}
one
();
if
(
longer
<
int7
) {
zero
();
write
((
int
)
longer
,
7
);
return
;
}
one
();
write
((
int
)
longer
,
14
);
return
;
}
}
write
(
1
,
2
);
for
(
int
i
=
0
;
i
<
string
.
length
();
i
+=
1
) {
write
(
bcd
(
string
.
charAt
(
i
)),
4
);
}
write
(
endOfNumber
,
4
);
this
.
values
.
register
(
string
);
}
else
{
write
(
3
,
2
);
writeJSON
(
value
);
}
}
/**
* Output a zero bit.
*
* @throws JSONException
*
* @throws IOException
*/
private
void
zero
()
throws
JSONException
{
if
(
probe
) {
log
(
0
);
}
write
(
0
,
1
);
}
/**
* Compress a JSONObject.
*
* @param jsonobject
* @throws JSONException
*/
public
void
zip
(
JSONObject
jsonobject
)
throws
JSONException
{
begin
();
writeJSON
(
jsonobject
);
}
/**
* Compress a JSONArray.
*
* @param jsonarray
* @throws JSONException
*/
public
void
zip
(
JSONArray
jsonarray
)
throws
JSONException
{
begin
();
writeJSON
(
jsonarray
);
}
}
Back
|
FazBrowse Home
|
New Git URL