Search code examples
javaandroidarraysjsonjsonreader

Read cached Json file as ByteArray with JsonReader


I cached a Json file as byte array and want to read it afterwards with the JsonReader.

However, the JsonReader takes a Reader as input parameter.

How can I convert me byte array to a Reader and is it worth doing so, or is there a straighter way of caching the InputStream of the Json file and read it afterwards by the JsonReader?

Looking forward to your responses!


Solution

  • You can chain ByteArrayInputStream and InputStreamReader to get the Reader object, e.g.:

    byte[] array = new byte[50];//json file
    Reader reader = new InputStreamReader(new ByteArrayInputStream(array));