Why cant a RandomAccessFile be casted to Inputstream?

丶灬走出姿态 提交于 2020-01-21 07:38:51

问题


I get compilation error when I do this cast:

RandomAccessFile raf = new RandomAccessFile(...)
InputStream is = (InputStream)raf;

RandomAccessFile is supposed to subclass InputStream although not directly.

From docs:

RandomAccessFile implements DataInput which inturn DataInputstream & InputStream

Why is this invalid?

Also appreciate your input on what would be the right way to use the RandomAccessFile as InputStream?

I am thinking of wrapper approach.


回答1:


RandomAccessFile extends Object, and does not extend InputStream.

If you want get an InputStream from a RandomAccessFile I think implementing a wrapper class is your simplest bet. Luckily the only abstract method of InputStream is read().

RandomAccessFile implements DataInput which inturn DataInputstream & InputStream

DataInputStream is a subclass of InputStream, which also happens to implement DataInput. The inheritance and interface implementation tree looks like this:

           InputStream      DataInput
               \              /   \
                \            /     \
                 \          /       \
                DataInputStream   RandomAccessFile

You could use a DataInputStream anywhere where you could use an InputStream or a DataInput. You could use a RandomAccessFile anywhere where you could use a DataInput.

But you can't go up and then down in the inheritance hierarchy like this using casts. In particular, casting a class to a subclass (or an interface to an implementation) will raise a ClassCastException unless the object happens to be an instance of the child class.

Even though two classes happen to extend Object that doesn't mean they are interchangeable.




回答2:


This is easily achievable using the Channels utility class...

// STEP 1:  Create random access file read-only
RandomAccessFile raf = new RandomAccessFile("/text.txt", "r");

// STEP 2:  Use Channels to convert to InputStream
InputStream is = Channels.newInputStream(raf.getChannel());



回答3:


RandomAccessFile is supposed to extends InputStream although not directly.

No it isn't. See the Javadoc.

From docs:

RandomAccessFile implements DataInput which in turn DataInputstream & InputStream.

That's not 'from the docs' at all. You made it up. What you've written doesn't even make sense. DataInput is an interface. DataInputStream and InputStream are classes. Interfaces don't implement, or extend, classes.

What the Javadoc actuallys says is that RandomAccessFile extends java.lang.Object and implements Closeable, DataInput, DataOutput.




回答4:


To build on @robert-christian’s answer, the main reason to use RandomAccessFile to begin with is to seek to some position, rather than skipping bytes from a FileInputStream. But then why bother with pre-NIO APIs at all?

try (FileChannel ch = FileChannel.open(Paths.get(…), StandardOpenOption.READ)) {
    InputStream is = Channels.newInputStream(ch.position(…));
    // …
}


来源:https://stackoverflow.com/questions/9287664/why-cant-a-randomaccessfile-be-casted-to-inputstream

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