FileOutputStream write non-existent bytes

一曲冷凌霜 提交于 2019-12-12 06:42:36

问题


I have a problem with FileOutputStream.

I use this simple code:

byte[] data1 = BD25Writer.getData1();
byte[] data2 = BD25Writer.getData2();

FileOutputStream dbf = new FileOutputStream(new File(file.getPath(), "file1.dbf"));
dbf.write(data1);
dbf.close();

FileOutputStream dbt = new FileOutputStream(new File(file.getPath(), "file2.dbt"));
dbt.write(data2);
dbt.close();

File is written correctly on most platforms (Windows, Android). In HEX-editor it looks like this:

But on only one device (YotaPhone2, model:YD201, android 6.0.1) this code with the same bytes is not working properly. Non-existent bytes written to the file:

Methods .getData1() and .getData2() works correctly. These methods simply write float data into an array of bytes. Сode looks like this:

private static byte[] getData1(){
    int key = ...;
    float[] buffer = new float[]{...};
    byte[] array = new byte[buffer.length * 4];
    byte[] bytes = new byte[4];

    for (int i = 0; i < buffer.length; i++) {
        bytes = ByteBuffer.allocate(4).putFloat(buffer[i]).array();
        array[key + i * 4] = bytes[3];
        array[key + i * 4 + 1] = bytes[2];
        array[key + i * 4 + 2] = bytes[1];
        array[key + i * 4 + 3] = bytes[0];
    }
    return array;
} 

Before sending array to FileOutputStream an array looks like this (get from debug):

How can this be and how can I fix this ?


回答1:


As it turned out, all code works correctly. Mail client broke the file during sending.



来源:https://stackoverflow.com/questions/43499255/fileoutputstream-write-non-existent-bytes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!