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
()
| 151 | * @since 19.0 |
| 152 | */ |
| 153 | @Beta |
| 154 | public long length() throws IOException { |
| 155 | Optional<Long> lengthIfKnown = lengthIfKnown(); |
| 156 | if (lengthIfKnown.isPresent()) { |
| 157 | return lengthIfKnown.get(); |
| 158 | } |
| 159 | |
| 160 | Closer closer = Closer.create(); |
| 161 | try { |
| 162 | Reader reader = closer.register(openStream()); |
| 163 | return countBySkipping(reader); |
| 164 | } catch (Throwable e) { |
| 165 | throw closer.rethrow(e); |
| 166 | } finally { |
| 167 | closer.close(); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | private long countBySkipping(Reader reader) throws IOException { |
| 172 | long count = 0; |
no test coverage detected