-
Notifications
You must be signed in to change notification settings - Fork 724
Expand file tree
/
Copy pathStreamReadResultCastCheckSample.java
More file actions
110 lines (89 loc) · 2.72 KB
/
Copy pathStreamReadResultCastCheckSample.java
File metadata and controls
110 lines (89 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package checks;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
class StreamReadResultCastCheckSample {
void byteCastInWhileLoop(FileInputStream fis) throws IOException {
byte b;
while ((b = (byte) fis.read()) != -1) { // Noncompliant {{Store the return value of "read()" in an "int" variable and check for -1 before casting.}}
process(b);
}
}
void byteCastAssignment(InputStream is) throws IOException {
byte value = (byte) is.read(); // Noncompliant
}
void charCastFromInputStream(InputStream is) throws IOException {
char c;
while ((c = (char) is.read()) != -1) { // Noncompliant
process(c);
}
}
void byteCastInDoWhile(FileInputStream fis) throws IOException {
byte b;
do {
b = (byte) fis.read(); // Noncompliant
} while (b != -1);
}
void charCastFromReader(FileReader reader) throws IOException {
char c;
while ((c = (char) reader.read()) != -1) { // Noncompliant
process(c);
}
}
void charCastAssignmentFromReader(Reader reader) throws IOException {
char c = (char) reader.read(); // Noncompliant
}
void byteCastWithParentheses(InputStream is) throws IOException {
byte b = (byte) (is.read()); // Noncompliant
}
void byteCastFromSubtype(BufferedInputStream bis) throws IOException {
byte b = (byte) bis.read(); // Noncompliant
}
// Compliant cases
void correctPatternWithIntVariable(FileInputStream fis) throws IOException {
int data;
while ((data = fis.read()) != -1) {
byte b = (byte) data;
process(b);
}
}
void multiArgReadByteArray(InputStream is) throws IOException {
byte[] buffer = new byte[1024];
int bytesRead = is.read(buffer);
}
void multiArgReadByteArrayWithOffset(InputStream is) throws IOException {
byte[] buffer = new byte[1024];
int bytesRead = is.read(buffer, 0, 1024);
}
void noCastAtAll(InputStream is) throws IOException {
int value = is.read();
}
void multiArgReadCharArray(Reader reader) throws IOException {
char[] buffer = new char[1024];
int charsRead = reader.read(buffer);
}
void correctPatternWithReader(Reader reader) throws IOException {
int data;
while ((data = reader.read()) != -1) {
char c = (char) data;
process(c);
}
}
void wideningCast(InputStream is) throws IOException {
long value = (long) is.read();
}
void customReadMethod() {
CustomReader custom = new CustomReader();
byte b = (byte) custom.read();
}
private void process(byte b) {}
private void process(char c) {}
static class CustomReader {
int read() {
return 0;
}
}
}