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 #lengthIfKnown. The default implementation calls #lengthIfKnown and returns the value if present. If absent, it will fall
()
| 158 | */ |
| 159 | |
| 160 | @Beta |
| 161 | public long length() throws IOException { |
| 162 | Optional<Long> lengthIfKnown = lengthIfKnown(); |
| 163 | if (lengthIfKnown.isPresent()) { |
| 164 | return lengthIfKnown.get(); |
| 165 | } |
| 166 | Closer closer = Closer.create(); |
| 167 | try { |
| 168 | Reader reader = closer.register(openStream()); |
| 169 | return countBySkipping(reader); |
| 170 | } catch (Throwable e) { |
| 171 | throw closer.rethrow(e); |
| 172 | } finally { |
| 173 | closer.close(); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | private long countBySkipping(Reader reader) throws IOException { |
| 178 | long count = 0; |
no test coverage detected