Debian Codegraph @

src/main/java/com/fasterxml/jackson/dataformat/smile/Tool.java

raw
1 package com.fasterxml.jackson.dataformat.smile;
2
3 import java.io.*;
4
5 import com.fasterxml.jackson.core.*;
6 import com.fasterxml.jackson.dataformat.smile.SmileFactory;
7
8 /**
9 * Simple command-line utility that can be used to encode JSON as Smile, or
10 * decode JSON from Smile: direction is indicated by single command-line
11 * option of either "-e" (encode) or "-d" (decode).
12 */
13 public class Tool
14 {
15 public final JsonFactory jsonFactory;
16 public final SmileFactory smileFactory;
17
18 public Tool()
19 {
20 jsonFactory = new JsonFactory();
21 smileFactory = new SmileFactory();
22 // check all shared refs (-> small size); add header, not trailing marker; do not use raw binary
23 smileFactory.configure(SmileGenerator.Feature.CHECK_SHARED_NAMES, true);
24 smileFactory.configure(SmileGenerator.Feature.CHECK_SHARED_STRING_VALUES, true);
25 smileFactory.configure(SmileGenerator.Feature.ENCODE_BINARY_AS_7BIT, true);
26 smileFactory.configure(SmileGenerator.Feature.WRITE_HEADER, true);
27 smileFactory.configure(SmileGenerator.Feature.WRITE_END_MARKER, false);
28 // also: do not require header
29 smileFactory.configure(SmileParser.Feature.REQUIRE_HEADER, false);
30 }
31
32 private void process(String[] args) throws IOException
33 {
34 String oper = null;
35 String filename = null;
36
37 if (args.length == 2) {
38 oper = args[0];
39 filename = args[1];
40 } else if (args.length == 1) {
41 oper = args[0];
42 } else {
43 showUsage();
44 }
45
46 boolean encode = "-e".equals(oper);
47 if (encode) {
48 encode(inputStream(filename));
49 } else if ("-d".equals(oper)) {
50 decode(inputStream(filename));
51 } else if ("-v".equals(oper)) {
52 // need to read twice (encode, verify/compare)
53 verify(inputStream(filename), inputStream(filename));
54 } else {
55 showUsage();
56 }
57 }
58
59 private InputStream inputStream(String filename) throws IOException
60 {
61 // if no argument given, read from stdin
62 if (filename == null) {
63 return System.in;
64 }
65 File src = new File(filename);
66 if (!src.exists()) {
67 System.err.println("File '"+filename+"' does not exist.");
68 System.exit(1);
69 }
70 return new FileInputStream(src);
71 }
72
73 private void decode(InputStream in) throws IOException
74 {
75 JsonParser jp = smileFactory.createParser(in);
76 JsonGenerator jg = jsonFactory.createGenerator(System.out, JsonEncoding.UTF8);
77
78 while (true) {
79 /* Just one trick: since Smile can have segments (multiple 'documents' in output
80 * stream), we should not stop at first end marker, only bail out if two are seen
81 */
82 if (jp.nextToken() == null) {
83 if (jp.nextToken() == null) {
84 break;
85 }
86 }
87 jg.copyCurrentEvent(jp);
88 }
89 jp.close();
90 jg.close();
91 }
92
93 private void encode(InputStream in) throws IOException
94 {
95 JsonParser jp = jsonFactory.createParser(in);
96 JsonGenerator jg = smileFactory.createGenerator(System.out, JsonEncoding.UTF8);
97 while ((jp.nextToken()) != null) {
98 jg.copyCurrentEvent(jp);
99 }
100 jp.close();
101 jg.close();
102 }
103
104 @SuppressWarnings("resource")
105 private void verify(InputStream in, InputStream in2) throws IOException
106 {
107 JsonParser jp = jsonFactory.createParser(in);
108 ByteArrayOutputStream bytes = new ByteArrayOutputStream(4000);
109 JsonGenerator jg = smileFactory.createGenerator(bytes, JsonEncoding.UTF8);
110
111 // First, read, encode in memory buffer
112 while ((jp.nextToken()) != null) {
113 jg.copyCurrentEvent(jp);
114 }
115 jp.close();
116 jg.close();
117
118 // and then re-read both, verify
119 jp = jsonFactory.createParser(in2);
120 byte[] smile = bytes.toByteArray();
121 JsonParser jp2 = smileFactory.createParser(smile);
122
123 JsonToken t;
124 int count = 0;
125 while ((t = jp.nextToken()) != null) {
126 JsonToken t2 = jp2.nextToken();
127 ++count;
128 if (t != t2) {
129 throw new IOException("Input and encoded differ, token #"+count+"; expected "+t+", got "+t2);
130 }
131 // also, need to have same texts...
132 String text1 = jp.getText();
133 String text2 = jp2.getText();
134 if (!text1.equals(text2)) {
135 throw new IOException("Input and encoded differ, token #"+count+"; expected text '"+text1+"', got '"+text2+"'");
136 }
137 }
138
139 System.out.println("OK: verified "+count+" tokens (from "+smile.length+" bytes of Smile encoded data), input and encoded contents are identical");
140 }
141
142 protected void showUsage()
143 {
144 System.err.println("Usage: java "+getClass().getName()+" -e/-d [file]");
145 System.err.println(" (if no file given, reads from stdin -- always writes to stdout)");
146 System.err.println(" -d: decode Smile encoded input as JSON");
147 System.err.println(" -e: encode JSON (text) input as Smile");
148 System.err.println(" -v: encode JSON (text) input as Smile; read back, verify, do not write out");
149 System.exit(1);
150 }
151
152 public static void main(String[] args) throws IOException {
153 new Tool().process(args);
154 }
155
156 }