Search code examples
javaandroidfileoutputstream

ArrayIndexOutOfBoundsException in Android 9.0


I have strange issue in production only in Android 9.0, the app in production very long time and this issue starts in Android 9.0

Process: com.ae.paris10, PID: 18804 java.lang.ArrayIndexOutOfBoundsException: length=984; regionStart=0; regionLength=1024
at java.util.Arrays.checkOffsetAndCount(Arrays.java:1719)
at libcore.io.IoBridge.write(IoBridge.java:487)
at java.io.FileOutputStream.write(FileOutputStream.java:186)

   private void copyStyleWithNewTilesPath() throws IOException {

        InputStream myinput = getContext()
                .getAssets().open("styleParis.json");


        String outfilename = "/data/data/" + Config.APPLICATION_ID + "/databases/styleParis.json";


        OutputStream myoutput = new FileOutputStream(outfilename);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = myinput.read(buffer)) > 0) {

            String str = new String(buffer, "UTF-8");

            if (str.contains("file://mnt/obb/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/{z}/{x}/{y}.pbf")) {
  buffer = str.replace("file://mnt/obb/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/{z}/{x}/{y}.pbf", "file://" + obbPath + "/{z}/{x}/{y}.pbf").getBytes("UTF-8");
            }

            myoutput.write(buffer, 0, length); <<--- EXCEPTION HERE <<-----------
        }

        //Close the streams
        myoutput.flush();
        myoutput.close();
        myinput.close();
    }

I don't really understand what happens since the in FileOutputStream is Java and Android framework.

Someone have any idea?


Solution

  • I think this will resolve your problem:

    if (str.contains("file://mnt/obb/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/{z}/{x}/{y}.pbf"))
        myoutput.write(str.replace("file://mnt/obb/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/{z}/{x}/{y}.pbf", "file://" + obbPath + "/{z}/{x}/{y}.pbf").getBytes("UTF-8"));
    else
        myoutput.write(buffer, 0, length);