• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nats-io / json.java / #44

03 Jun 2025 10:53PM UTC coverage: 96.99% (-0.01%) from 97.001%
#44

push

github

web-flow
Merge pull request #14 from nats-io/switch-to-apache-encoder

Switch to Apache Encoder for Base 64

11 of 12 new or added lines in 1 file covered. (91.67%)

741 of 764 relevant lines covered (96.99%)

0.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.59
/src/main/java/io/nats/json/Encoding.java
1
// Copyright 2020-2024 The NATS Authors
2
// Licensed under the Apache License, Version 2.0 (the "License");
3
// you may not use this file except in compliance with the License.
4
// You may obtain a copy of the License at:
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software
9
// distributed under the License is distributed on an "AS IS" BASIS,
10
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
// See the License for the specific language governing permissions and
12
// limitations under the License.
13

14
package io.nats.json;
15

16
import java.net.URLDecoder;
17
import java.nio.charset.StandardCharsets;
18

19
import org.apache.commons.codec.binary.Base64;
20

21
/**
22
 * Utilities for encoding, i.e., Base64, URI and JSON
23
 */
24
public abstract class Encoding {
25

26
    private Encoding() {}  /* ensures cannot be constructed */
27

28
    /**
29
     * base64 encode a byte array to a byte array
30
     * @param input the input byte array to encode
31
     * @return the encoded byte array
32
     */
33
    public static byte[] base64BasicEncode(byte[] input) {
34
        return Base64.encodeBase64(input);
1✔
35
    }
36

37
    /**
38
     * base64 encode a byte array to a byte array
39
     * @param input the input byte array to encode
40
     * @return the encoded byte array
41
     */
42
    public static String base64BasicEncodeToString(byte[] input) {
43
        return Base64.encodeBase64String(input);
1✔
44
    }
45

46
    /**
47
     * base64 url encode a byte array to a byte array
48
     * @param input the input byte array to encode
49
     * @return the encoded byte array
50
     */
51
    public static String base64BasicEncodeToString(String input) {
52
        return Base64.encodeBase64String(input.getBytes(StandardCharsets.UTF_8));
1✔
53
    }
54

55
    /**
56
     * base64 url encode a byte array to a byte array
57
     * @param input the input byte array to encode
58
     * @return the encoded byte array
59
     */
60
    public static byte[] base64UrlEncode(byte[] input) {
61
        return Base64.encodeBase64URLSafe(input);
1✔
62
    }
63

64
    /**
65
     * base64 url encode a byte array to a byte array
66
     * @param input the input byte array to encode
67
     * @return the encoded byte array
68
     */
69
    public static String base64UrlEncodeToString(byte[] input) {
70
        return Base64.encodeBase64URLSafeString(input);
1✔
71
    }
72

73
    /**
74
     * base64 url encode a byte array to a byte array
75
     * @param input the input byte array to encode
76
     * @return the encoded byte array
77
     */
78
    public static String base64UrlEncodeToString(String input) {
79
        return Base64.encodeBase64URLSafeString(input.getBytes(StandardCharsets.UTF_8));
1✔
80
    }
81

82
    /**
83
     * base64 decode a byte array
84
     * @param input the input byte array to decode
85
     * @return the decoded byte array
86
     */
87
    public static byte[] base64BasicDecode(byte[] input) {
88
        return Base64.decodeBase64(input);
1✔
89
    }
90

91
    /**
92
     * base64 decode a base64 encoded string
93
     * @param input the input string to decode
94
     * @return the decoded byte array
95
     */
96
    public static byte[] base64BasicDecode(String input) {
97
        return Base64.decodeBase64(input);
1✔
98
    }
99

100
    /**
101
     * base64 decode a base64 encoded string
102
     * @param input the input string to decode
103
     * @return the decoded string
104
     */
105
    public static String base64BasicDecodeToString(String input) {
106
        return new String(Base64.decodeBase64(input));
1✔
107
    }
108

109
    /**
110
     * base64 url decode a byte array
111
     * @param input the input byte array to decode
112
     * @return the decoded byte array
113
     */
114
    public static byte[] base64UrlDecode(byte[] input) {
115
        return Base64.decodeBase64(input);
1✔
116
    }
117

118
    /**
119
     * base64 url decode a base64 url encoded string
120
     * @param input the input string to decode
121
     * @return the decoded byte array
122
     */
123
    public static byte[] base64UrlDecode(String input) {
124
        return Base64.decodeBase64(input);
1✔
125
    }
126

127
    /**
128
     * base64 url decode a base64 url encoded string
129
     * @param input the input string to decode
130
     * @return the decoded string
131
     */
132
    public static String base64UrlDecodeToString(String input) {
NEW
133
        return new String(Base64.decodeBase64(input));
×
134
    }
135

136
    /**
137
     * Decode a JSON string
138
     * @param s the input string
139
     * @return the decoded string
140
     */
141
    public static String jsonDecode(String s) {
142
        int len = s.length();
1✔
143
        StringBuilder sb = new StringBuilder(len);
1✔
144
        for (int x = 0; x < len; x++) {
1✔
145
            char ch = s.charAt(x);
1✔
146
            if (ch == '\\') {
1✔
147
                char nextChar = (x == len - 1) ? '\\' : s.charAt(x + 1);
1✔
148
                switch (nextChar) {
1✔
149
                    case '\\':
150
                        break;
1✔
151
                    case 'b':
152
                        ch = '\b';
1✔
153
                        break;
1✔
154
                    case 'f':
155
                        ch = '\f';
1✔
156
                        break;
1✔
157
                    case 'n':
158
                        ch = '\n';
1✔
159
                        break;
1✔
160
                    case 'r':
161
                        ch = '\r';
1✔
162
                        break;
1✔
163
                    case 't':
164
                        ch = '\t';
1✔
165
                        break;
1✔
166
                    // Hex Unicode: u????
167
                    case 'u':
168
                        if (x >= len - 5) {
1✔
169
                            ch = 'u';
×
170
                            break;
×
171
                        }
172
                        int code = Integer.parseInt(
1✔
173
                            "" + s.charAt(x + 2) + s.charAt(x + 3) + s.charAt(x + 4) + s.charAt(x + 5), 16);
1✔
174
                        sb.append(Character.toChars(code));
1✔
175
                        x += 5;
1✔
176
                        continue;
1✔
177
                    default:
178
                        ch = nextChar;
1✔
179
                        break;
180
                }
181
                x++;
1✔
182
            }
183
            sb.append(ch);
1✔
184
        }
185
        return sb.toString();
1✔
186
    }
187

188
    /**
189
     * Encode a JSON string
190
     * @param s the input string
191
     * @return the encoded string
192
     */
193
    public static String jsonEncode(String s) {
194
        return jsonEncode(new StringBuilder(), s).toString();
1✔
195
    }
196

197
    /**
198
     * Encode a JSON string into a StringBuilder
199
     * @param sb the target StringBuilder
200
     * @param s the input string
201
     * @return the encoded string as StringBuilder
202
     */
203
    public static StringBuilder jsonEncode(StringBuilder sb, String s) {
204
        int len = s.length();
1✔
205
        for (int x = 0; x < len; x++) {
1✔
206
            char ch = s.charAt(x);
1✔
207
            switch (ch) {
1✔
208
                case '"':
209
                    sb.append("\\\"");
1✔
210
                    break;
1✔
211
                case '\\':
212
                    sb.append("\\\\");
1✔
213
                    break;
1✔
214
                case '\b':
215
                    sb.append("\\b");
1✔
216
                    break;
1✔
217
                case '\f':
218
                    sb.append("\\f");
1✔
219
                    break;
1✔
220
                case '\n':
221
                    sb.append("\\n");
1✔
222
                    break;
1✔
223
                case '\r':
224
                    sb.append("\\r");
1✔
225
                    break;
1✔
226
                case '\t':
227
                    sb.append("\\t");
1✔
228
                    break;
1✔
229
                case '/':
230
                    sb.append("\\/");
1✔
231
                    break;
1✔
232
                default:
233
                    if (ch < ' ') {
1✔
234
                        sb.append(String.format("\\u%04x", (int) ch));
1✔
235
                    }
236
                    else {
237
                        sb.append(ch);
1✔
238
                    }
239
                    break;
240
            }
241
        }
242
        return sb;
1✔
243
    }
244

245
    /**
246
     * Decode a URI
247
     * @param source the input
248
     * @return the decoded URI
249
     */
250
    public static String uriDecode(String source) {
251
        return URLDecoder.decode(source.replace("+", "%2B"), StandardCharsets.UTF_8);
1✔
252
    }
253
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc