Android 9.0 中的 ArrayIndexOutOfBoundsException

ArrayIndexOutOfBoundsException in Android 9.0

我只在 Android 9.0 的生产中遇到奇怪的问题,应用程序在生产中很长时间,这个问题从 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();
    }

我真的不明白发生了什么,因为 FileOutputStream 是 Java 和 Android 框架。

有人知道吗?

我认为这将解决您的问题:

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);
if (str.contains("file://mnt/obb/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/{z}/{x}/{y}.pbf"))
    // write(byte[] b) :: Writes b.length bytes from the specified byte array to myoutput. 
    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);