Returns the length of this source in chars, even if doing so requires opening and traversing an entire stream. To avoid a potentially expensive operation, see {@link #lengthIfKnown}. <p>The default implementation calls {@link #lengthIfKnown} and returns the value if present. If absent, it will fall
()
| 211 | * @since 19.0 |
| 212 | */ |
| 213 | public long length() throws IOException { |
| 214 | Optional<Long> lengthIfKnown = lengthIfKnown(); |
| 215 | if (lengthIfKnown.isPresent()) { |
| 216 | return lengthIfKnown.get(); |
| 217 | } |
| 218 | |
| 219 | Closer closer = Closer.create(); |
| 220 | try { |
| 221 | Reader reader = closer.register(openStream()); |
| 222 | return countBySkipping(reader); |
| 223 | } catch (Throwable e) { |
| 224 | throw closer.rethrow(e); |
| 225 | } finally { |
| 226 | closer.close(); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | private static long countBySkipping(Reader reader) throws IOException { |
| 231 | long count = 0; |
no test coverage detected