I have Java program and I am reading from file of Size Approx 40GB and converting data on fly and streaming to DataOutputstream.
Below is my code
JSONObject jsonObj= new JSONObject();
JSONArray jsonArray = new JSONArray();
JSONObject properties = new JSONObject();
while((strLine = in.readLine()) != null)
{
if (jsonArray.length()%100 == 0) {
printJsonData(jsonArray);
// sendJsonData(jsonArray, output);
jsonArray = new JSONArray();
}
if (strLine.trim().contains("data")) {
jsonObj.put("properties",properties);
jsonArray.put(jsonObj);
if (strLine.trim().contains("data1")) {
String secondPart = strLine.split(":",2)[1];
properties.put("data1", secondPart);
continue;
}
if (strLine.trim().contains("id")) {
String secondPart = strLine.split(":",2)[1];
jsonObj.put("id", secondPart );
continue;
}
}
I have two methods one is printing JSON data and other is sending Json data over https.
private void printJsonData(JSONArray jsonArray) {
int count = jsonArray.length(); // get totalCount of all jsonObjects
for(int i=0 ; i< count; i++){ // iterate through jsonArray
JSONObject jsonObject = jsonArray.getJSONObject(i); // get jsonObject @ i position
System.out.println("jsonObject " + i + ": " + jsonObject);
}
}
private void sendJsonData(JSONObject jsonObj,DataOutputStream output) throws IOException {
int count = jsonArray.length();
for(int i=0 ; i< count; i++) { // iterate through jsonArray
JSONObject jsonObject = jsonArray.getJSONObject(i); // get jsonObject @ i position
System.out.println("Sending Data-->" + jsonObject.toString());
output.writeBytes(jsonObject.toString());
}
}
Whenver i call print method, every thing works fine. But when i call sendJsonData method. I am getting OutofMemoryerror. Wondering how to fix it?
sendJsonDatais called from