Returns the size of this source in bytes, even if doing so requires opening and traversing an entire stream. To avoid a potentially expensive operation, see #sizeIfKnown. The default implementation calls #sizeIfKnown and returns the value if present. If absent, it will fall back
()
| 190 | * @throws IOException if an I/O error occurs in the process of reading the size of this source |
| 191 | */ |
| 192 | public long size() throws IOException { |
| 193 | Optional<Long> sizeIfKnown = sizeIfKnown(); |
| 194 | if (sizeIfKnown.isPresent()) { |
| 195 | return sizeIfKnown.get(); |
| 196 | } |
| 197 | |
| 198 | Closer closer = Closer.create(); |
| 199 | try { |
| 200 | InputStream in = closer.register(openStream()); |
| 201 | return countBySkipping(in); |
| 202 | } catch (IOException e) { |
| 203 | // skip may not be supported... at any rate, try reading |
| 204 | } finally { |
| 205 | closer.close(); |
| 206 | } |
| 207 | |
| 208 | closer = Closer.create(); |
| 209 | try { |
| 210 | InputStream in = closer.register(openStream()); |
| 211 | return ByteStreams.exhaust(in); |
| 212 | } catch (Throwable e) { |
| 213 | throw closer.rethrow(e); |
| 214 | } finally { |
| 215 | closer.close(); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Counts the bytes in the given input stream using skip if possible. Returns SKIP_FAILED if the |
nothing calls this directly
no test coverage detected