Skip to content

Commit 4b716ce

Browse files
Fix SpotBugs DM_DEFAULT_ENCODING by using Util helper methods for explicit UTF-8 encoding
1 parent dd34253 commit 4b716ce

26 files changed

+126
-44
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.io.Util.newString(req.getResponseData());
192192
token = token.substring(token.indexOf('=') + 1);
193193
}
194194
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ private int read1(byte[] b, int off, int len) throws IOException {
357357
actualAvailable = -1;
358358
} else {
359359
if (printInput) {
360-
System.out.print(new String(b, off, val));
360+
System.out.print(Util.newString(b, off, val));
361361
}
362362
}
363363
return val;
@@ -371,7 +371,7 @@ private int read1(byte[] b, int off, int len) throws IOException {
371371
int cnt = (avail < len) ? avail : len;
372372
System.arraycopy(getBufIfOpen(), pos, b, off, cnt);
373373
if (printInput) {
374-
System.out.print(new String(b, off, cnt));
374+
System.out.print(Util.newString(b, off, cnt));
375375
}
376376
pos += cnt;
377377
return cnt;
@@ -438,7 +438,7 @@ public synchronized int read(byte[] b, int off, int len)
438438
int v = getInIfOpen().read(b, off, len);
439439
if (v > -1) {
440440
if (printInput) {
441-
System.out.print(new String(b, off, v));
441+
System.out.print(Util.newString(b, off, v));
442442
}
443443

444444
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(Util.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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,11 @@ 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) {
339-
text += new String(buffer, 0, size);
339+
text += Util.newString(buffer, 0, size);
340340
size = r.read(buffer);
341341
}
342342
r.close();
@@ -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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ protected long calculateContentLength() {
270270
try {
271271
length += value.toString().getBytes("UTF-8").length;
272272
} catch (UnsupportedEncodingException ex) {
273-
length += value.toString().getBytes().length;
273+
length += Util.getBytes(value.toString()).length;
274274
}
275275
} else {
276276
if (base64Binaries) {
@@ -288,7 +288,7 @@ protected long calculateContentLength() {
288288
try {
289289
length += s.toString().getBytes("UTF-8").length;
290290
} catch (UnsupportedEncodingException ex) {
291-
length += value.toString().getBytes().length;
291+
length += Util.getBytes(value.toString()).length;
292292
}
293293
} else {
294294
if (base64Binaries) {
@@ -304,7 +304,7 @@ protected long calculateContentLength() {
304304
try {
305305
length += ((String) filenames.get(key)).getBytes("UTF-8").length;
306306
} catch (UnsupportedEncodingException ex) {
307-
length += ((String) filenames.get(key)).getBytes().length;
307+
length += Util.getBytes((String) filenames.get(key)).length;
308308
}
309309
length += ((String) mimeTypes.get(key)).length();
310310
length += Long.parseLong((String) filesizes.get(key));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ class TokenRequest extends ConnectionRequest {
541541

542542
protected void readResponse(InputStream input) throws IOException {
543543
byte[] tok = Util.readInputStream(input);
544-
String t = new String(tok);
544+
String t = Util.newString(tok);
545545
boolean expiresRelative = true;
546546
if (t.startsWith("{")) {
547547
JSONParser p = new JSONParser();

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

Lines changed: 75 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,78 @@ public static void setIgnorCharsWhileEncoding(String s) {
107110
ignoreCharsWhenEncoding = s;
108111
}
109112

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);
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);
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);
154+
}
155+
}
156+
157+
/**
158+
* Helper to get a reader from an input stream with UTF-8 encoding
159+
* @param in the input stream
160+
* @return the reader
161+
*/
162+
public static Reader getReader(InputStream in) {
163+
try {
164+
return new InputStreamReader(in, "UTF-8");
165+
} catch(UnsupportedEncodingException e) {
166+
// never happens
167+
throw new RuntimeException(e);
168+
}
169+
}
170+
171+
/**
172+
* Helper to get a writer from an output stream with UTF-8 encoding
173+
* @param out the output stream
174+
* @return the writer
175+
*/
176+
public static Writer getWriter(OutputStream out) {
177+
try {
178+
return new OutputStreamWriter(out, "UTF-8");
179+
} catch(UnsupportedEncodingException e) {
180+
// never happens
181+
throw new RuntimeException(e);
182+
}
183+
}
184+
110185
/**
111186
* Copy the input stream into the output stream, closes both streams when finishing or in
112187
* 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 = com.codename1.io.Util.getBytes("");
8080
inflater.setOutput(empty, 0, 0);
8181
inflater.setInput(empty, 0, 0, false);
8282

0 commit comments

Comments
 (0)