Skip to content

Commit 8936bf9

Browse files
Fix SpotBugs DM_DEFAULT_ENCODING violations (#4352)
* Fix SpotBugs DM_DEFAULT_ENCODING by using Util helper methods for explicit UTF-8 encoding * Fix SpotBugs DM_DEFAULT_ENCODING by using Util helper methods for explicit UTF-8 encoding * Fix SpotBugs DM_DEFAULT_ENCODING by using Util helper methods for explicit UTF-8 encoding * Fix SpotBugs DM_DEFAULT_ENCODING by using Util helper methods for explicit UTF-8 encoding * Fix SpotBugs DM_DEFAULT_ENCODING by using Util helper methods for explicit UTF-8 encoding * Fix SpotBugs DM_DEFAULT_ENCODING by moving helper methods to StringUtil --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent dd34253 commit 8936bf9

28 files changed

+323
-236
lines changed

.github/scripts/generate-quality-report.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,8 @@ def main() -> None:
761761
"RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE",
762762
"RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE",
763763
"UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR",
764-
"SF_SWITCH_NO_DEFAULT"
764+
"SF_SWITCH_NO_DEFAULT",
765+
"DM_DEFAULT_ENCODING"
765766
}
766767
violations = [
767768
f for f in spotbugs.findings

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 = new String(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(new String(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(new String(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(new String(b, off, v));
442+
System.out.print(StringUtil.newString(b, off, v));
442443
}
443444

444445
totalBytesRead += v;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public CSVParser(char separatorChar) {
7070
* @return array of rows and columns
7171
*/
7272
public String[][] parse(InputStream r) throws IOException {
73-
return parse(new InputStreamReader(r));
73+
return parse(Util.getReader(r));
7474
}
7575

7676
/**

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(createRequestURL().getBytes()).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/Log.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ public static String getLogContent() {
332332
if (instance.getFileURL() == null) {
333333
instance.setFileURL("file:///" + FileSystemStorage.getInstance().getRoots()[0] + "/codenameOne.log");
334334
}
335-
Reader r = new InputStreamReader(FileSystemStorage.getInstance().openInputStream(instance.getFileURL()));
335+
Reader r = Util.getReader(FileSystemStorage.getInstance().openInputStream(instance.getFileURL()));
336336
char[] buffer = new char[1024];
337337
int size = r.read(buffer);
338338
while (size > -1) {
@@ -508,18 +508,18 @@ protected void print(String text, int level) {
508508
protected Writer createWriter() throws IOException {
509509
try {
510510
if (getFileURL() == null) {
511-
return new OutputStreamWriter(Storage.getInstance().createOutputStream("CN1Log__$"));
511+
return Util.getWriter(Storage.getInstance().createOutputStream("CN1Log__$"));
512512
}
513513
if (FileSystemStorage.getInstance().exists(getFileURL())) {
514-
return new OutputStreamWriter(FileSystemStorage.getInstance().openOutputStream(getFileURL(),
514+
return Util.getWriter(FileSystemStorage.getInstance().openOutputStream(getFileURL(),
515515
(int) FileSystemStorage.getInstance().getLength(getFileURL())));
516516
} else {
517-
return new OutputStreamWriter(FileSystemStorage.getInstance().openOutputStream(getFileURL()));
517+
return Util.getWriter(FileSystemStorage.getInstance().openOutputStream(getFileURL()));
518518
}
519519
} catch (Exception err) {
520520
setFileWriteEnabled(false);
521521
// currently return a "dummy" writer so we won't fail on device
522-
return new OutputStreamWriter(new ByteArrayOutputStream());
522+
return Util.getWriter(new ByteArrayOutputStream());
523523
}
524524
}
525525

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 += value.toString().getBytes().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 += value.toString().getBytes().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 += ((String) filenames.get(key)).getBytes().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 = new String(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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@
5050
import java.io.EOFException;
5151
import java.io.IOException;
5252
import java.io.InputStream;
53+
import java.io.InputStreamReader;
5354
import java.io.OutputStream;
55+
import java.io.OutputStreamWriter;
5456
import java.io.Reader;
57+
import java.io.Writer;
5558
import java.io.UnsupportedEncodingException;
5659
import java.util.ArrayList;
5760
import java.util.Collection;
@@ -107,6 +110,35 @@ public static void setIgnorCharsWhileEncoding(String s) {
107110
ignoreCharsWhenEncoding = s;
108111
}
109112

113+
114+
/**
115+
* Helper to get a reader from an input stream with UTF-8 encoding
116+
* @param in the input stream
117+
* @return the reader
118+
*/
119+
public static InputStreamReader getReader(InputStream in) {
120+
try {
121+
return new InputStreamReader(in, "UTF-8");
122+
} catch(UnsupportedEncodingException e) {
123+
// never happens
124+
throw new RuntimeException(e.toString());
125+
}
126+
}
127+
128+
/**
129+
* Helper to get a writer from an output stream with UTF-8 encoding
130+
* @param out the output stream
131+
* @return the writer
132+
*/
133+
public static OutputStreamWriter getWriter(OutputStream out) {
134+
try {
135+
return new OutputStreamWriter(out, "UTF-8");
136+
} catch(UnsupportedEncodingException e) {
137+
// never happens
138+
throw new RuntimeException(e.toString());
139+
}
140+
}
141+
110142
/**
111143
* Copy the input stream into the output stream, closes both streams when finishing or in
112144
* a case of an exception

CodenameOne/src/com/codename1/io/gzip/GZIPInputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public long getCRC() throws GZIPException {
7676

7777
public void readHeader() throws IOException {
7878

79-
byte[] empty = "".getBytes();
79+
byte[] empty = new byte[0];
8080
inflater.setOutput(empty, 0, 0);
8181
inflater.setInput(empty, 0, 0, false);
8282

0 commit comments

Comments
 (0)