I have the following code inside a loop statement. In the loop, strings are appended to sb(StringBuilder) and checked whether the size of sb has reached 5MB.
Consider using a ByteArrayOutputStream and an OutputStreamWriter instead of the StringBuilder. Use ByteArrayOutputStream.size() to test the size.