Java: InputStream conventions for data availability, closed state, etc?

The InputStream abstract class is for reading bytes from any source, not just a file. Its descendants provide methods for specific sources. For example FileInputStream can be used to read bytes from a file, while SocketInputStream is used to read bytes from a socket.

The InputStream abstract class is for reading bytes from any source, not just a file. Its descendants provide methods for specific sources. For example, FileInputStream can be used to read bytes from a file, while SocketInputStream is used to read bytes from a socket.

This is why InputStream doesn't offer any methods for determining the file size, because it would make no sense for sources other than files. I'm not sure what exactly you mean by reading file over a network. If it means reading a file from a remote filesystem (e.g. Over NFS), then you can use the length() method in the File class to get the size of the file: File file = new File("/path/to/file"); System.out.

Println(file.length()); You can determine whether the FileInputStream is open or not using the valid() method on the FileDescriptor instance obtained by calling the getFD() method on the 'FileInputStream' object: boolean isOpen(FileInputStream stream) { return stream.getFD().valid(); } Of course, bear in mind that the Law of Leaky Abstractions applies.

Assume I connected to a file that is hosted by an HTTP server. The file is known to be static only (no dynamic data) and will always be a fixed size. Is there a class I can use the read from such a file that will give length() and valid() methods, and will also work for local files?

– William the Coderer Nov 13 '11 at 4:16.

Java.io. Inputstream does one thing, and one thing only. It provides an interface for reading bytes of data from... anything.It could be a file, an HTPP website, a database, whatever decided to provide the InputStream.

As such, it is bare exactly because that simplicity makes it universal. For example, an InputStream could be used for streaming live television.In that case there is no length of content, since you could stream forever. Thus if InputStream provided a length of content feature, it would become useless for endless streams.

End of the day, you're going to have to use the technology one step up from the input stream to determine things like existence. For example, use File. Exists for file existance, look for a 404 error for HTTP non-existence, or a database exception for database non-existence.

Only look to the Inputstream when you want to read bytes from the file, because that's all it's intended to do.No more, no less.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions