Android how to speed up reading from a file to create Java objects using Buffered Input/Output?

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.

Answers


public static List<String> convertJSONtoArrayList(File f) {
    final StringBuilder jsonString = new StringBuilder(500);
    try {
        final BufferedReader reader = new BufferedReader(new FileReader(f));
        String s;
        while ((s = reader.readLine()) != null) {
            jsonString.append(s);
        }
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
        return Collections.emptyList();
    }

    try {
        JSONArray jsonArray = new JSONArray(jsonString.toString());
        int arrayLength = jsonArray.length();
        final List<String> result = new ArrayList<String>(arrayLength);
        for (int i = 0; i < arrayLength; i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            result.add(jsonObject.getString(Tags.VALUE));
        }
        return result;
    } catch (Exception e) {
        e.printStackTrace();
        return Collections.emptyList();
    }
}

or try this (shoule be faster):

public static List<String> convertJSONtoArrayList(File f) {
    ByteBuffer buffer = ByteBuffer.allocate(f.length());
    try {
        ReadableByteChannel channel = Channels.newChannel(new FileInputStream(f));
        channel.read(buffer);
        channel.close();
    } catch (Exception e) {
        e.printStackTrace();
        return Collections.emptyList();
    }

    final String jsonString = new String(buffer.array());

    try {
        JSONArray jsonArray = new JSONArray(jsonString);
        int arrayLength = jsonArray.length();
        final List<String> result = new ArrayList<String>(arrayLength);
        for (int i = 0; i < arrayLength; i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            result.add(jsonObject.getString(Tags.VALUE));
        }
        return result;
    } catch (Exception e) {
        e.printStackTrace();
        return Collections.emptyList();
    }
}

Use the same code but wrap a BufferedInputStream around the FileInputStream.


Need Your Help

Change currency symbol based on county dynamically iPhone?

iphone dynamic currency symbols

I want to change the currency symbols in iPhone app based on user's localization. If my app is working in US i need to change the currency symbol "$". If the app is working in Eroupe countries, we ...

Socket.io events not firing once connected after previous failed attempt

javascript node.js websocket socket.io

Here's the scenario, I have a client side application, served by PHP on a different server to the node.js + socket.io application. It can connect and receive broadcasts sent from the server. If the

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.