可以fileInputStream.阅读读取奇数字节吗?

发布于 2025-02-01 12:55:40 字数 1500 浏览 4 评论 0原文

我有一种读取16位波文件的方法:

FileInputStream in = new FileInputStream(file);
byte[] bytes = new byte[8*Number];
do {
    readSize = in.read(bytes, 0, 8*Number);
    ...
} while (...);

然后,我需要将bytes []中的每个字节转换为简短。也就是说,我最好确保readsize偶数!我知道readsize不一定等于8*数字,并且可以更小...

readsize以某种方式保证的这种奇偶校验通过读取方法,还是我必须在in.Read行之后检查?

edit

因为我还需要读取等于8*数字,而不仅仅是我使用此解决方法再一次,直到我得到所有我需要的数据:

        FileInputStream in = new FileInputStream(f);
        int i, readSize, chunkPerBar = 8*Number;
        byte[] bytes = new byte[chunkPerBar];
        double mean;
        do {
            // If I cannot read *exactly* chunkPerBar bytes, keep adding or exit
            readSize = in.read(bytes, 0, chunkPerBar);
            while ((readSize < chunkPerBar) && (in.available() > 0))
                readSize += in.read(bytes, readSize, chunkPerBar - readSize);
            if (readSize < chunkPerBar) break;  // Discard the last chunk if < chunkPerBar

            mean = 0;
            for (i = 0; i < chunkPerBar / 2; i++) {
                aux = (short)( ( bytes[2*i] & 0xff )|( bytes[2*i+1] << 8 ) );
                mean += aux * aux;
            }

            waveform.drawNewBar(mean/chunkPerBar);
        } while (in.available() > 0);
        in.close();
        } catch {...}

I have a method that reads a 16 bit WAVE file, like this:

FileInputStream in = new FileInputStream(file);
byte[] bytes = new byte[8*Number];
do {
    readSize = in.read(bytes, 0, 8*Number);
    ...
} while (...);

I then need to convert each pair of bytes in bytes[] into a short. That is, I better be sure that readSize is even! I know that readSize is not necessarily equal to 8*Number, and that it can be smaller...

Is this parity of readSize somehow guaranteed by the read method, or I must check after the in.read line?

EDIT

Since I also need that readSize to be equal to 8*Number and not just even, I use this workaround, where I am reading once and again until I get all the data I need:

        FileInputStream in = new FileInputStream(f);
        int i, readSize, chunkPerBar = 8*Number;
        byte[] bytes = new byte[chunkPerBar];
        double mean;
        do {
            // If I cannot read *exactly* chunkPerBar bytes, keep adding or exit
            readSize = in.read(bytes, 0, chunkPerBar);
            while ((readSize < chunkPerBar) && (in.available() > 0))
                readSize += in.read(bytes, readSize, chunkPerBar - readSize);
            if (readSize < chunkPerBar) break;  // Discard the last chunk if < chunkPerBar

            mean = 0;
            for (i = 0; i < chunkPerBar / 2; i++) {
                aux = (short)( ( bytes[2*i] & 0xff )|( bytes[2*i+1] << 8 ) );
                mean += aux * aux;
            }

            waveform.drawNewBar(mean/chunkPerBar);
        } while (in.available() > 0);
        in.close();
        } catch {...}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文