java - 奇怪的 FileInputStream/Data FileInputStream 行为 : seek()ing to odd positions

标签 java java-me java-io

优点:

所以,我有这个二进制数据文件(大小 - 恰好 640631 字节),我正试图让 Java 读取它。

我有两个可互换的类实现为读取该数据的层。其中之一使用 RandomAccessFile,效果非常好。

缺点:

另一个(这个问题主要是关于那个)尝试使用 FileInputStreamDataInputStream 以便可以(至少理论上)读取完全相同的数据MIDP 2.0 (CLDC 1.1) Java 配置(没有 RandomAccessFile)。

在那个类中,我像这样打开数据文件:

FileInputStream res = new FileInputStream(new File(filename));
h = new DataInputStream(res);

...并像这样实现seek()/skip()(position 是一个long记下文件中的当前位置):

public void seek(long pos) throws java.io.IOException {

    if (! this.isOpen()) {
        throw new java.io.IOException("No file is open");
    }

    if (pos < position) {
        // Seek to the start, then skip some bytes
        this.reset();
        this.skip(pos);
    } else if (pos > position) {
        // skip the remaining bytes until the position
        this.skip(pos - position);
    }
}

public void skip(long bytes) throws java.io.IOException {

    if (! this.isOpen()) {
        throw new java.io.IOException("No file is open");
    }

    long skipped = 0, step = 0;

    do {
        step = h.skipBytes((int)(bytes - skipped));
        if (step < 0) {
            throw new java.io.IOException("skip() failed");
        }
        skipped += step;
    } while (skipped < bytes);

    position += bytes;
}

丑陋的:

第二个类(FileInputStream/DataInputStream)的问题是有时它决定将文件位置重置到文件中某个奇怪的地方:) 这个当我在 J2SE(计算机)和 J2ME(手机)上运行时都会发生。下面是该阅读器类的实际用法示例和发生的错误:

// Open the data file
Reader r = new Reader(filename);

// r.position = 0, actual position in a file = 0

// Skip to where the data block that is needed starts
// (determined by some other code)
r.seek(189248);

// r.position = 189248, actual position in a file = 189248

// Do some reading...
r.readID(); r.readName(); r.readSurname();

// r.position = 189332, actual position in a file = 189332

// Skip some bytes (an unneeded record)
r.skip(288);

// r.position = 189620, actual position in a file = 189620

// Do some more reading...
r.readID(); r.readName(); r.readSurname();

// r.position = 189673, actual position in a file = 189673

// Skip some bytes (an unneeded record)
r.skip(37);

// AAAAND HERE WE GO:
// r.position = 189710, actual position in a file = 477

我能够确定,当被要求跳过另外 37 个字节时,Java 将文件指针从最开始或文件定位到字节 477。

“新鲜”(刚打开文件后)寻找位置 189710(及之后)工作正常。但是,每次我需要 seek() 时重新打开一个文件实在是太慢了,尤其是在手机上。

发生了什么事?

最佳答案

我看不出有什么问题。您对最后一次跳过之前的 r.position 值是否肯定?除非 JDK 流中存在潜在错误,或者如果您有多个线程使用 Reader,那么我唯一可以猜测的可能性是当您读取字段时某些东西错误地修改了位置值。

关于java - 奇怪的 FileInputStream/Data FileInputStream 行为 : seek()ing to odd positions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3964451/

相关文章:

java - 从 java 代码创建 PDF 文件 [不使用 iText]

java - 如何确定MINA中的服务器端口

java - 关于 future 的好文档/书

java - JPanel GridBagLayout 未对齐垂直对象

ide - 有没有在 Blackberry 上运行的免费移动 IDE?

java - j2me中的HTML文本提取

java - Java如何保证ListenableFuture中future.isDone()时回调被调用

java - 如何在玩框架java中测试来自假应用程序的路由时模拟 Controller 依赖关系

java-me - 在嵌入式系统上运行的 Java VM 的建议

java - 来自 socket.getInputStream() 的 ObjectInputStream