From da2b7e30558eb0669127274e4b9de22123ed3eac Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 15 Jun 2017 14:20:12 +0300 Subject: [PATCH] #146: LengthOfInput closes Input --- .../java/org/cactoos/io/LengthOfInput.java | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/cactoos/io/LengthOfInput.java b/src/main/java/org/cactoos/io/LengthOfInput.java index 2fa55ce1ba..46b5260640 100644 --- a/src/main/java/org/cactoos/io/LengthOfInput.java +++ b/src/main/java/org/cactoos/io/LengthOfInput.java @@ -70,19 +70,20 @@ public LengthOfInput(final Input input, final int max) { @Override public Long asValue() throws IOException { - final InputStream stream = this.source.stream(); - final byte[] buf = new byte[this.size]; - long length = 0L; - while (true) { - final int len = stream.read(buf); - if (len > 0) { - length += (long) len; - } - if (len != buf.length) { - break; + try (final InputStream stream = this.source.stream()) { + final byte[] buf = new byte[this.size]; + long length = 0L; + while (true) { + final int len = stream.read(buf); + if (len > 0) { + length += (long) len; + } + if (len != buf.length) { + break; + } } + return length; } - return length; } }