How to handle CWE-400-Resource exhaustion error - java

We are getting an IBM APPSCAN exception for the following code.
{
br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
}
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line+"\n");
}
br.close();
Can someone suggest a way to handle the same.

I myself have figured out the solution for this.
Just we need to limit the character read by readline().
there is no way to limit the same, so we need to use BoundedBufferedReader.
Try the below:
{
br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
}
StringBuilder sb = new StringBuilder();
String line;
BoundedBufferedReader boundedReader = new BoundedBufferedReader(br,204800,204800);
while (( line = boundedReader.readLine() ) != null) {
sb.append(line+"\n");
}
br.close();

Related

StringBuffer equal to string but not working

I'm trying to check the with an if-statement if two strings are the same(which they are), but it doesn't work. Here is my code and my first input value (https://imgur.com/a/3wpsssZ) . In the image at the bottom you see the first string value( "Ticket is valid")
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader in=new BufferedReader(new
InputStreamReader(
conn.getInputStream()));
StringBuffer sb = new StringBuffer("");
String line="";
while((line = in.readLine()) != null) {
sb.append(line);
break;
}
in.close();
StringBuffer sb1 = new StringBuffer("\"Ticket is valid\"");
if(sb.toString().equals(sb1.toString())){
textView.setText("SCANNED: "+sb1.toString());
}
return sb.toString();
}
Try to add a .trim() to the sb.toString()

How can I change multiple line from txt

I have a text file with letters up and I want to make them lower letters. It is the code but it becomes only the last line
BufferedReader in = new BufferedReader(new FileReader("C:\Users\xxx\Desktop\j.txt"));
String line;
while((line = in.readLine()) != null)
{
blig=line.toUpperCase();
}
try (BufferedWriter writer = new BufferedWriter(new FileWriter("C:\Users\xxx\Desktop\j.txt"))) {
writer.write(blig);
}
You are traversing through the whole file but just storing the last line. In order to get all the lines, you need to store all the lines you are reading, convert them to uppercase, then write them back to the file:
List<String> upper = new ArrayList<>();
try(BufferedReader in = new BufferedReader(new FileReader("C:\Users\xxx\Desktop\j.txt")))
{
String line;
while((line = in.readLine()) != null)
{
// upper.add(line.toUpperCase()); // if you need upper case
upper.add(line.toLowerCase()); // if you need lower case
}
}
try(BufferedWriter writer = new BufferedWriter(new FileWriter("C:\Users\xxx\Desktop\j.txt")))
{
for(String str : upper)
{
writer.write(str);
writer.newLine(); // remove if not needed.
}
}
BufferedReader in = new BufferedReader(new FileReader("C:\Users\xxx\Desktop\j.txt"));
BufferedWriter writer = new BufferedWriter(new FileWriter("C:\Users\xxx\Desktop\lowerJ.txt"));
String line;
while((line = in.readLine()) != null) {
writer.write(line.toLowerCase());
}
If you are trying to lower the case of characters, then why use to Uper?
blig=line.toUpperCase();
It sould be bling=line.toLowerCase();
Because you are writing only the last line while you are not concatenating all the result.
replace the while statement by
blig += line.toUpperCase() + "\\n";

How to display all lines of text from a file instead of stopping at the end of a line?

The code below only brings up the first line of code and stops. I would like to return each line of code until there are no more.
private String GetPhoneAddress() {
File directory = Environment.getExternalStorageDirectory();
File myFile = new File(directory, "mythoughtlog.txt");
//File file = new File(Environment.getExternalStorageDirectory() + "mythoughtlog.txt");
if (!myFile.exists()){
String line = "Need to add smth";
return line;
}
String line = null;
//Read text from file
//StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(myFile));
line = br.readLine();
}
catch (IOException e) {
//You'll need to add proper error handling here
}
return line;
}
You could loop over the results of readLine() and accumulate them until you get a null, indicating the end of the file (BTW, note that your snippet neglected to close the reader. A try-with-resource structure could handle that):
try (BufferedReader br = new BufferedReader(new FileReader(myFile))) {
String line = br.readLine();
if (line == null) {
return null;
}
StringBuilder retVal = new StringBuilder(line);
line = br.readLine();
while (line != null) {
retVal.append(System.lineSeparator()).append(line);
line = br.readLine();
}
return retVal.toString();
}
if you're using Java 8, you can save a lot of this boiler-plated code with the newly introduced lines() method:
try (BufferedReader br = new BufferedReader(new FileReader(myFile))) {
return br.lines().collect(Collectors.joining(System.lineSeparator()));
}
A considerably less verbose solution:
try (BufferedReader br = new BufferedReader(new FileReader(myFile))) {
StringBuilder retVal = new StringBuilder();
while ((line = br.readLine()) != null) {
retVal.append(line).append(System.lineSeparator());
}
return retVal.toString();
}

Reading an InputStream in Java

I'm new to Java thus the question,
I'm using the following class to read a file into a string.
public class Reader {
public static String readFile(String fileName) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(fileName));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append("\n");
line = br.readLine();
}
return sb.toString();
} finally {
br.close();
}
}
}
How can I modify the method signature of read to read a InputStream as opposed to a string.
Remove the String argument and create an argument of type InputStream. Pass this argument to the constructor of an InputStreamReader and this InputStreamReader can be passed to the constructor of your BufferedReader.
public static String readFile(InputStream is) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(is));
.
.
.
}
Maybee you want to try a try-with-resource statement. Then you can remove the final block. It looks like this.
public static String readFile(InputStream is) throws IOException
{
try (BufferedReader br = new BufferedReader(new InputStreamReader(is)))
{
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null)
{
sb.append(line);
sb.append("\n");
line = br.readLine();
}
return sb.toString();
}
}
If it is not for educational purposes, don't do this manually. E.g. you could use IOUtils.toString from Apache Commons.

How to read a file?

private void getInput() throws IOException {
InputStream resourceAsStream = this.getClass().getResourceAsStream(
"aaa.txt");
BufferedReader br = new BufferedReader(new FileReader(
resourceAsStream.toString()));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String everything = sb.toString();
System.out.println(everything);
} finally {
br.close();
}
}
The file aaa.txt is in the same project and it cannot be read, I have java.lang.NullPointerException when I execute that.
Only do this
BufferedReader br = new BufferedReader(new FileReader("aaa.txt"));
But put the file in the filereader instead.
Hope it helpt.

Categories