java - 安卓java.io.IOException : write failed: EBADF (Bad file number)

标签 java android file encryption

我已经阅读了几篇关于 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);
    }
}

最佳答案

我看到你的encrypt 方法调用doInBackground 所以它会产生一些复杂的情况,比如移动到另一个 fragment 或创建 fragment 的两个实例,这让 android 感到困惑。最好的方法是在调用 onDestroy() 时忽略 encrypt 方法。

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

希望对你有帮助

关于java - 安卓java.io.IOException : write failed: EBADF (Bad file number),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53496171/

相关文章:

java - 如何有效地将未压缩的 InputStream 转换为 gzip 压缩的 InputStream?

Java - 使用 IO 流打开现有文件或创建一个(如果不存在)

android - 如何读取 Android (ARM) 中的 L2 缓存命中/未命中率?

javascript - 出现错误 -> 意外的 token }

python - 在服务器上找不到文件时阻止代码崩溃的方法?

java - 为什么我的程序找不到 Peg Solitaire 的解决方案并一直运行?

java - Intellij Idea 不断将编译器重置为 'ajc'

java - 运行应用程序时出错 : java. lang.exceptionininitializererror

c - 从文件 (.txt) 中读取并保存到 char* C

python - 运行时创建文件和写入内容