Skip to content

Commit a47b14a

Browse files
Fix SpotBugs DM_DEFAULT_ENCODING by moving helper methods to StringUtil
1 parent 1a2ac2e commit a47b14a

19 files changed

+74
-72
lines changed

CodenameOne/src/com/codename1/facebook/FaceBookAccess.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public static void anonymousLogin(String appid, String clientSecret) {
188188
req.addArgument("grant_type", "client_credentials");
189189
NetworkManager.getInstance().addToQueueAndWait(req);
190190
if (req.getResponseData() != null) {
191-
token = com.codename1.io.Util.newString(req.getResponseData());
191+
token = com.codename1.util.StringUtil.newString(req.getResponseData());
192192
token = token.substring(token.indexOf('=') + 1);
193193
}
194194
}

CodenameOne/src/com/codename1/io/BufferedInputStream.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package com.codename1.io;
2525

2626
import com.codename1.ui.Display;
27+
import com.codename1.util.StringUtil;
2728

2829
import java.io.IOException;
2930
import java.io.InputStream;
@@ -357,7 +358,7 @@ private int read1(byte[] b, int off, int len) throws IOException {
357358
actualAvailable = -1;
358359
} else {
359360
if (printInput) {
360-
System.out.print(Util.newString(b, off, val));
361+
System.out.print(StringUtil.newString(b, off, val));
361362
}
362363
}
363364
return val;
@@ -371,7 +372,7 @@ private int read1(byte[] b, int off, int len) throws IOException {
371372
int cnt = (avail < len) ? avail : len;
372373
System.arraycopy(getBufIfOpen(), pos, b, off, cnt);
373374
if (printInput) {
374-
System.out.print(Util.newString(b, off, cnt));
375+
System.out.print(StringUtil.newString(b, off, cnt));
375376
}
376377
pos += cnt;
377378
return cnt;
@@ -438,7 +439,7 @@ public synchronized int read(byte[] b, int off, int len)
438439
int v = getInIfOpen().read(b, off, len);
439440
if (v > -1) {
440441
if (printInput) {
441-
System.out.print(Util.newString(b, off, v));
442+
System.out.print(StringUtil.newString(b, off, v));
442443
}
443444

444445
totalBytesRead += v;

CodenameOne/src/com/codename1/io/ConnectionRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ private String getCacheFileName() {
826826
root = FileSystemStorage.getInstance().getAppHomePath() + "cn1ConCache/";
827827
}
828828
FileSystemStorage.getInstance().mkdir(root);
829-
String fileName = Base64.encodeNoNewline(Util.getBytes(createRequestURL())).replace('/', '-').replace('+', '_');
829+
String fileName = Base64.encodeNoNewline(StringUtil.getBytes(createRequestURL())).replace('/', '-').replace('+', '_');
830830

831831
// limit file name length for portability: https://stackoverflow.com/questions/54644088/why-is-codenameone-rest-giving-me-file-name-too-long-error
832832
if (fileName.length() > 255) {

CodenameOne/src/com/codename1/io/MultipartRequest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.io.OutputStreamWriter;
3030
import java.io.UnsupportedEncodingException;
3131
import java.io.Writer;
32+
import com.codename1.util.StringUtil;
3233
import java.util.Hashtable;
3334
import java.util.Iterator;
3435
import java.util.LinkedHashMap;
@@ -270,7 +271,7 @@ protected long calculateContentLength() {
270271
try {
271272
length += value.toString().getBytes("UTF-8").length;
272273
} catch (UnsupportedEncodingException ex) {
273-
length += Util.getBytes(value.toString()).length;
274+
length += StringUtil.getBytes(value.toString()).length;
274275
}
275276
} else {
276277
if (base64Binaries) {
@@ -288,7 +289,7 @@ protected long calculateContentLength() {
288289
try {
289290
length += s.toString().getBytes("UTF-8").length;
290291
} catch (UnsupportedEncodingException ex) {
291-
length += Util.getBytes(value.toString()).length;
292+
length += StringUtil.getBytes(value.toString()).length;
292293
}
293294
} else {
294295
if (base64Binaries) {
@@ -304,7 +305,7 @@ protected long calculateContentLength() {
304305
try {
305306
length += ((String) filenames.get(key)).getBytes("UTF-8").length;
306307
} catch (UnsupportedEncodingException ex) {
307-
length += Util.getBytes((String) filenames.get(key)).length;
308+
length += StringUtil.getBytes((String) filenames.get(key)).length;
308309
}
309310
length += ((String) mimeTypes.get(key)).length();
310311
length += Long.parseLong((String) filesizes.get(key));

CodenameOne/src/com/codename1/io/Oauth2.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.codename1.ui.html.DocumentInfo;
3939
import com.codename1.ui.layouts.BorderLayout;
4040
import com.codename1.util.AsyncResource;
41+
import com.codename1.util.StringUtil;
4142
import com.codename1.util.regex.StringReader;
4243

4344
import java.io.IOException;
@@ -541,7 +542,7 @@ class TokenRequest extends ConnectionRequest {
541542

542543
protected void readResponse(InputStream input) throws IOException {
543544
byte[] tok = Util.readInputStream(input);
544-
String t = Util.newString(tok);
545+
String t = StringUtil.newString(tok);
545546
boolean expiresRelative = true;
546547
if (t.startsWith("{")) {
547548
JSONParser p = new JSONParser();

CodenameOne/src/com/codename1/io/Util.java

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -110,49 +110,6 @@ public static void setIgnorCharsWhileEncoding(String s) {
110110
ignoreCharsWhenEncoding = s;
111111
}
112112

113-
/**
114-
* Helper to get bytes from string with UTF-8 encoding
115-
* @param s the string
116-
* @return the bytes
117-
*/
118-
public static byte[] getBytes(String s) {
119-
try {
120-
return s.getBytes("UTF-8");
121-
} catch(UnsupportedEncodingException e) {
122-
// never happens
123-
throw new RuntimeException(e.toString());
124-
}
125-
}
126-
127-
/**
128-
* Helper to get string from bytes with UTF-8 encoding
129-
* @param b the bytes
130-
* @return the string
131-
*/
132-
public static String newString(byte[] b) {
133-
try {
134-
return new String(b, "UTF-8");
135-
} catch(UnsupportedEncodingException e) {
136-
// never happens
137-
throw new RuntimeException(e.toString());
138-
}
139-
}
140-
141-
/**
142-
* Helper to get string from bytes with UTF-8 encoding
143-
* @param b the bytes
144-
* @param offset the offset
145-
* @param length the length
146-
* @return the string
147-
*/
148-
public static String newString(byte[] b, int offset, int length) {
149-
try {
150-
return new String(b, offset, length, "UTF-8");
151-
} catch(UnsupportedEncodingException e) {
152-
// never happens
153-
throw new RuntimeException(e.toString());
154-
}
155-
}
156113

157114
/**
158115
* Helper to get a reader from an input stream with UTF-8 encoding

CodenameOne/src/com/codename1/io/rest/RequestBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.codename1.util.Callback;
3737
import com.codename1.util.FailureCallback;
3838
import com.codename1.util.OnComplete;
39+
import com.codename1.util.StringUtil;
3940
import com.codename1.util.SuccessCallback;
4041

4142
import java.io.IOException;
@@ -446,7 +447,7 @@ public RequestBuilder jsonContent() {
446447
* @return RequestBuilder instance
447448
*/
448449
public RequestBuilder basicAuth(String username, String password) {
449-
header("Authorization", "Basic " + Base64.encodeNoNewline(com.codename1.io.Util.getBytes(username + ":" + password)));
450+
header("Authorization", "Basic " + Base64.encodeNoNewline(StringUtil.getBytes(username + ":" + password)));
450451
return this;
451452
}
452453

CodenameOne/src/com/codename1/io/services/TwitterRESTService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ protected void readResponse(InputStream input) throws IOException {
105105
auth.setUrl("https://api.twitter.com/oauth2/token");
106106

107107
// YOU MUST CHANGE THIS IF YOU BUILD YOUR OWN APP
108-
String encoded = Base64.encodeNoNewline(com.codename1.io.Util.getBytes(consumerKey + ":" + consumerSecret));
108+
String encoded = Base64.encodeNoNewline(com.codename1.util.StringUtil.getBytes(consumerKey + ":" + consumerSecret));
109109
auth.addRequestHeader("Authorization", "Basic " + encoded);
110110
auth.setContentType("application/x-www-form-urlencoded;charset=UTF-8");
111111
auth.addArgument("grant_type", "client_credentials");

CodenameOne/src/com/codename1/processing/HashtableContent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public MapContent(Map content) {
6565
* @throws IOException on error reading/parsing the string
6666
*/
6767
public MapContent(String content) throws IOException {
68-
this(com.codename1.io.Util.getReader(new ByteArrayInputStream(com.codename1.io.Util.getBytes(content))));
68+
this(com.codename1.io.Util.getReader(new ByteArrayInputStream(com.codename1.util.StringUtil.getBytes(content))));
6969
}
7070

7171
/**

CodenameOne/src/com/codename1/processing/JSONContent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public JSONContent(Map content) {
5656
* @throws IOException on error reading/parsing string
5757
*/
5858
public JSONContent(String content) throws IOException {
59-
this(com.codename1.io.Util.getReader(new ByteArrayInputStream(com.codename1.io.Util.getBytes(content))));
59+
this(com.codename1.io.Util.getReader(new ByteArrayInputStream(com.codename1.util.StringUtil.getBytes(content))));
6060
}
6161

6262
/**

0 commit comments

Comments
 (0)