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

nats-io / json.java / #51

20 Jun 2025 03:37PM UTC coverage: 97.139% (-2.4%) from 99.519%
#51

push

github

web-flow
Merge pull request #18 from nats-io/revert

Revert back to Java 8 version

296 of 309 new or added lines in 5 files covered. (95.79%)

5 existing lines in 3 files now uncovered.

747 of 769 relevant lines covered (97.14%)

0.97 hits per line

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

93.15
/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.io.UnsupportedEncodingException;
17
import java.net.URLDecoder;
18
import java.nio.charset.StandardCharsets;
19
import java.util.Base64;
20

21
public abstract class Encoding {
22
    private Encoding() {}  /* ensures cannot be constructed */
23

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

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

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

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

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

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

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

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

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

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

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

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

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

182
    public static String jsonEncode(String s) {
183
        return jsonEncode(new StringBuilder(), s).toString();
1✔
184
    }
185

186
    public static StringBuilder jsonEncode(StringBuilder sb, String s) {
187
        int len = s.length();
1✔
188
        for (int x = 0; x < len; x++) {
1✔
189
            char ch = s.charAt(x);
1✔
190
            switch (ch) {
1✔
191
                case '"':
192
                    sb.append("\\\"");
1✔
193
                    break;
1✔
194
                case '\\':
195
                    sb.append("\\\\");
1✔
196
                    break;
1✔
197
                case '\b':
198
                    sb.append("\\b");
1✔
199
                    break;
1✔
200
                case '\f':
201
                    sb.append("\\f");
1✔
202
                    break;
1✔
203
                case '\n':
204
                    sb.append("\\n");
1✔
205
                    break;
1✔
206
                case '\r':
207
                    sb.append("\\r");
1✔
208
                    break;
1✔
209
                case '\t':
210
                    sb.append("\\t");
1✔
211
                    break;
1✔
212
                case '/':
213
                    sb.append("\\/");
1✔
214
                    break;
1✔
215
                default:
216
                    if (ch < ' ') {
1✔
217
                        sb.append(String.format("\\u%04x", (int) ch));
1✔
218
                    }
219
                    else {
220
                        sb.append(ch);
1✔
221
                    }
222
                    break;
223
            }
224
        }
225
        return sb;
1✔
226
    }
227

228
    public static String uriDecode(String source) {
229
        try {
230
            return URLDecoder.decode(source.replace("+", "%2B"), "UTF-8");
1✔
NEW
231
        } catch (UnsupportedEncodingException e) {
×
NEW
232
            return source;
×
233
        }
234
    }
235
}
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