ObjectInputStream can read a lot more than Objects. The class implements DataInput, so it can read all kinds of data. Then there's special methods like readUnshared. So a Stream would be a pretty limited subset of ObjectInputStream's functonality.
But if you just want to read Objects one by one, you can write your own method:
public Stream<Object> toStream(final ObjectInputStream stream) {
return Stream.generate(() -> readObject(stream)).onClose(
() -> close(stream));
}
private static Object readObject(ObjectInputStream stream) {
try {
return stream.readObject();
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
private static void close(Closeable c) {
try {
c.close();
} catch (IOException e) {
logger.log(Level.WARNING, "Couldn't close " + c, e);
}
}
You could even have a typed stream:
public <T> Stream<T> toStream(final ObjectInputStream stream,
final Class<T> cls) {
return Stream.generate(() -> cls.cast(readObject(stream))).onClose(
() -> close(stream));
}