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

nats-io / json.java / #34

03 Jun 2025 08:10PM UTC coverage: 97.001% (-0.1%) from 97.139%
#34

push

github

web-flow
Merge pull request #13 from nats-io/java21-2

Working on build

744 of 767 relevant lines covered (97.0%)

0.97 hits per line

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

95.77
/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
import java.util.Base64;
19

20
/**
21
 * Utilities for encoding, i.e., Base64, URI and JSON
22
 */
23
public abstract class Encoding {
24
    private Encoding() {}  /* ensures cannot be constructed */
25

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

© 2025 Coveralls, Inc