11package com .apicatalog .cli .command ;
22
33import java .io .FileOutputStream ;
4- import java .io .IOException ;
54import java .net .URI ;
5+ import java .nio .charset .StandardCharsets ;
66import java .util .concurrent .Callable ;
77
88import com .apicatalog .base .Base16 ;
@@ -41,7 +41,7 @@ public final class CompressCmd implements Callable<Integer> {
4141 @ Option (names = { "-d" , "--dictionary" }, description = "Custom dictionary location (JSON)." , paramLabel = "<uri|file>" )
4242 URI dictionary = null ;
4343
44- @ Option (names = { "-x" , "--hex" }, description = "Output result as hexadecimal-encoded." )
44+ @ Option (names = { "-x" , "--hex" }, description = "Output result as hexadecimal-encoded. Automatically enabled for stdout. " )
4545 boolean hex = false ;
4646
4747 @ Mixin
@@ -85,22 +85,21 @@ public Integer call() throws Exception {
8585 .encode (json .asJsonObject ());
8686
8787 if (output == null ) {
88- spec .commandLine ().getOut ().print (encode (encoded , hex ));
88+ spec .commandLine ().getOut ().print (Base16 . encode (encoded , Base16 . ALPHABET_LOWER ));
8989 spec .commandLine ().getOut ().flush ();
9090
9191 } else {
9292 try (var os = new FileOutputStream (output )) {
93- os .write (encode (encoded , hex ));
93+ if (hex ) {
94+ os .write (Base16 .encode (encoded , Base16 .ALPHABET_LOWER ).getBytes (StandardCharsets .UTF_8 ));
95+ } else {
96+ os .write (encoded );
97+ }
98+
9499 os .flush ();
95100 }
96101 }
97102
98103 return spec .exitCodeOnSuccess ();
99104 }
100-
101- static byte [] encode (byte [] encoded , boolean hex ) throws IOException {
102- return hex
103- ? Base16 .encode (encoded , Base16 .ALPHABET_LOWER ).getBytes ()
104- : encoded ;
105- }
106105}
0 commit comments