I have this method that takes a json file from storage and turns it into a bunch of java objects. How do I add bufferred output/input streams in order to speed it up? Or is there another way to optimize the speed?
EDIT: Im not only going to use this for reading from JSON files, so I dont need json to java parsers, I actually need to speed up file oprations using buffers :)
public static ArrayList<String> convertJSONtoArrayList(File jsonStrings) {
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(jsonStrings);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
return convertJSONtoArrayList(fileInputStream);
}
public static ArrayList<String> convertJSONtoArrayList(InputStream fileInputStream) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ArrayList<String> arrayListString = new ArrayList<String>();
int ctr;
try {
if (fileInputStream != null) {
ctr = fileInputStream.read();
while (ctr != -1) {
byteArrayOutputStream.write(ctr);
ctr = fileInputStream.read();
}
}
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
// Parse the data into jsonobject to get original data in form of
// json.
JSONArray jsonArray = new JSONArray(byteArrayOutputStream.toString());
int arrayLength = jsonArray.length();
for (int i = 0; i < arrayLength; i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
arrayListString.add(jsonObject.getString(Tags.VALUE));
}
} catch (Exception e) {
e.printStackTrace();
}
return arrayListString;
}
My idea for this being possible comes from here: How to speed up unzipping time in Java / Android? - in the answers becomes clear that if BufferedInputStream is added, the operation is sped up really good.