I have a simple servlet class returns CSV file to a client (browser). When I tried to write string to stream. Initially I have followed example A) however I noticed that subset of expected data are appended.
Let's say I expect exact 100 records to be captured in a csv file. Each record represent "first name/last name/age". In the example A) I got 120. First 100 records are correct but subset of 100 in this case 20 (duplicates) are appended.
example A)
public void returnCSV(HttpServletResponse resp, String data) {
ServletOutputStream out = resp.getOutputStream();
InputStream in = new ByteArrayInputStream(data.getBytes("UTF-8"));
byte[] bytes = new byte[4096];
while (in.read(bytes,0,4096) != -1) {
out.write(bytes,0,4096);
}
in.close();
out.flush();
out.close();
}
After reading more threads in regards to converting string to stream. I decided to follow example B) which produced correct output 100 records. But I do not understand why first example A) would add duplicate data.
example B)
public void returnCSV(HttpServletResponse resp, String data) {
ServletOutputStream out = resp.getOutputStream();
out.write(data.getBytes("UTF-8"));
out.flush();
out.close();
}