Android java.io.IOException:写入失败:EBADF(文件编号错误)

Android java.io.IOException: write failed: EBADF (Bad file number)

提问人:Hendré 提问时间:11/27/2018 最后编辑:Hendré 更新时间:12/3/2018 访问量:2514

问:

我已经阅读了几篇关于 EBADF 错误的帖子,这些错误无法解决我的问题。我的情况的独特之处在于,我正在尝试写入 getFilesDir,它应该可以由应用程序写入。

我在执行加密方法时收到 EBADF 错误。文件参数是使用以下方法创建的:

new File(mContext.getFilesDir(), "file.dat")

其中 是加密方法的列表:

public static void encrypt(File file, String password, List<Object> objects) {

    byte[] salt = generateSalt();
    byte[] iv = generateIV();
    Cipher c = createCipher(password, salt, Cipher.ENCRYPT_MODE, iv);

    try (FileOutputStream fos = new FileOutputStream(file)) {
        fos.write(salt);
        fos.write(iv);
        try (CipherOutputStream cos = new CipherOutputStream(fos, c);
        ObjectOutputStream oos = new ObjectOutputStream(cos)) {
            for (Object o : objects) {
                oos.writeObject(o);
            }
        }

    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

谁能看出为什么我会得到异常?

以下是异常日志:

 Caused by: java.lang.RuntimeException: java.io.IOException: write failed: EBADF (Bad file number)
    at za.co.lot24media.password.util.EncryptUtil.encrypt(EncryptUtil.java:69)
    at za.co.lot24media.password.store.Store.save(Store.java:94)
    at za.co.lot24media.password.store.Store.createSamples(Store.java:179)
    at za.co.lot24media.password.store.Store.load(Store.java:76)
    at za.co.lot24media.password.activity.login.LoginAction$2.doInBackground(LoginAction.java:62)
    at za.co.lot24media.password.activity.login.LoginAction$2.doInBackground(LoginAction.java:55)
    at android.os.AsyncTask$2.call(AsyncTask.java:292)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
    at java.lang.Thread.run(Thread.java:818) 
 Caused by: java.io.IOException: write failed: EBADF (Bad file number)
    at libcore.io.IoBridge.write(IoBridge.java:502)
    at java.io.FileOutputStream.write(FileOutputStream.java:186)
    at java.io.OutputStream.write(OutputStream.java:82)
    at javax.crypto.CipherOutputStream.close(CipherOutputStream.java:129)
    at za.co.lot24media.password.util.EncryptUtil.encrypt(EncryptUtil.java:64)

**编辑**

问题似乎出在 ObjectOutputStream 写入 CipherOutputStream 上。当我从 encrypt() 方法中删除 ObjectOutputStream 时,该方法成功了。下面的代码有效:

    public static void encrypt(File file, String password, StoreDataRecord storeDataRecord) {

    byte[] salt = generateSalt();
    byte[] iv = generateIV();
    Cipher c = createCipher(password, salt, Cipher.ENCRYPT_MODE, iv);

    try (FileOutputStream fos = new FileOutputStream(file)) {
        fos.write(salt);
        fos.write(iv);

        try (CipherOutputStream cos = new CipherOutputStream(fos, c)) {
            cos.write(new byte[10]);
        }

    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

我使用 byte[10] 只是将任意数据写入流。

** 编辑 2 **

以下解决方案也有效,首先将数据写入 ByteArrayOutputStream:

public static void encrypt(File file, String password, StoreDataRecord storeDataRecord) {

    byte[] salt = generateSalt();
    byte[] iv = generateIV();
    Cipher c = createCipher(password, salt, Cipher.ENCRYPT_MODE, iv);

    try (FileOutputStream fos = new FileOutputStream(file)) {
        fos.write(salt);
        fos.write(iv);

        try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
             ObjectOutputStream oos = new ObjectOutputStream(bos);
             CipherOutputStream cos = new CipherOutputStream(fos, c)) {
            oos.writeObject(storeDataRecord.getVersion());
            oos.writeObject(storeDataRecord.getItems());
            cos.write(bos.toByteArray());
        }
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Java Android 文件 加密

评论

0赞 Themelis 11/27/2018
发布异常日志
1赞 user207421 11/27/2018
您无需遍历对象。将 .List
0赞 Martin Zeitler 11/28/2018
如何知道写入流是否会导致 java.io.IOException: write failed: EBADF(文件编号错误)的可能重复项
0赞 Martin Zeitler 11/28/2018
它不是一个精确的副本,而是一个(许多)的副本,这可能最有帮助。
0赞 user207421 11/29/2018
最里面的 try-with-resources 是 ,它将在块退出时关闭,它将关闭底层流和文件,这将关闭 ,这将在关闭时导致此异常如果它需要刷新,它显然是这样做的。在(冗余)循环后添加,以确保在关闭时不需要刷新。ObjectOutputStreamFileOutputStreamCipherOutputStreamoos.flush()CipherOutputStream

答:

0赞 Radesh 12/1/2018 #1

正如我看到您的方法调用的那样,它可能会产生一些复杂的情况,例如移动到另一个片段或创建两个片段实例,这使 android 感到困惑。最好的方法是在调用时忽略方法。encryptdoInBackgroundencryptonDestroy()

并确保你的流在你使用之前没有关闭。

希望对您有所帮助