Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

How to read a file or stream in Most Robust way using Java?

Avatar

Avatar
Ignite 1
Level 1
karthickv998656
Level 1

Likes

0 likes

Total Posts

2 posts

Correct Reply

0 solutions
Top badges earned
Ignite 1
Give Back
View profile

Avatar
Ignite 1
Level 1
karthickv998656
Level 1

Likes

0 likes

Total Posts

2 posts

Correct Reply

0 solutions
Top badges earned
Ignite 1
Give Back
View profile
karthickv998656
Level 1

26-08-2019

protected void doPost(SlingHttpServletRequest req, SlingHttpServletResponse resp) throws IOException {

BufferedReader reader = req.getReader();

try{

String line = reader != null ? reader.readLine() : null;

}finally{

reader.close();

}

The above code went for a review with the Security team and the following comments were received:

  1. BufferedReader.readLine is susceptible to DOS (Denial of Service) attacks (line of infinite length, huge file containing no line feed/carriage return)
  2. Resource exhaustion for the StringBuilder variable (cases when a file containing data greater than the available memory).

How to resolve the above issue?

Replies

Avatar

Avatar
Ignite 1
Level 3
rampai
Level 3

Likes

19 likes

Total Posts

51 posts

Correct Reply

7 solutions
Top badges earned
Ignite 1
Validate 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Ignite 1
Level 3
rampai
Level 3

Likes

19 likes

Total Posts

51 posts

Correct Reply

7 solutions
Top badges earned
Ignite 1
Validate 1
Give Back 5
Give Back 3
Give Back 10
View profile
rampai
Level 3

26-08-2019

Hi karthickv99865601​,

Can you please check if this is of any help since this query is not AEM specific?

The suggestion is to leverage StringBuffer to split the stream and read it in chunks rather than one whole stream.

https://stackoverflow.com/questions/17084657/most-robust-way-of-reading-a-file-or-stream-using-java-...

Regards,

Ram

Avatar

Avatar
Ignite 1
Level 1
karthickv998656
Level 1

Likes

0 likes

Total Posts

2 posts

Correct Reply

0 solutions
Top badges earned
Ignite 1
Give Back
View profile

Avatar
Ignite 1
Level 1
karthickv998656
Level 1

Likes

0 likes

Total Posts

2 posts

Correct Reply

0 solutions
Top badges earned
Ignite 1
Give Back
View profile
karthickv998656
Level 1

26-08-2019

Hi rampai,

I have tried that too, but its throwing error.