diff --git a/src/main/java/com/speedment/internal/core/stream/autoclose/AbstractAutoClosingStream.java b/src/main/java/com/speedment/internal/core/stream/autoclose/AbstractAutoClosingStream.java index 7b44381d4b..adb13171eb 100644 --- a/src/main/java/com/speedment/internal/core/stream/autoclose/AbstractAutoClosingStream.java +++ b/src/main/java/com/speedment/internal/core/stream/autoclose/AbstractAutoClosingStream.java @@ -17,6 +17,7 @@ package com.speedment.internal.core.stream.autoclose; import com.speedment.exception.SpeedmentException; +import static com.speedment.internal.util.NullUtil.requireNonNulls; import com.speedment.internal.util.holder.Holder; import java.util.Collection; import java.util.HashSet; @@ -156,25 +157,26 @@ public static UnsupportedOperationException newUnsupportedException(String metho @SafeVarargs // Creating a Stream of an array is safe. @SuppressWarnings({"unchecked", "varargs"}) public static void composedClose(T... closeables) { - final Holder eHolder = new Holder<>(); - Stream.of(closeables).forEachOrdered(s -> { + requireNonNulls(closeables); + Exception exception = null; + + for (final T closable : closeables) { try { - s.close(); + closable.close(); } catch (Exception e) { - final Exception exisitingException = eHolder.get(); - if (exisitingException == null) { - eHolder.set(e); + if (exception == null) { + exception = e; } else { try { - exisitingException.addSuppressed(e); + exception.addSuppressed(e); } catch (Exception ignored) { } } } - }); - final Exception e = eHolder.get(); - if (e != null) { - throw new SpeedmentException(e); + } + + if (exception != null) { + throw new SpeedmentException(exception); } } @@ -188,12 +190,13 @@ public static void composedClose(T... closeables) { * an exception */ public static void composedRunnable(Collection runnables) { - composedClose( - runnables.stream() - .map(CloseImpl::new) - .toArray(s -> new CloseImpl[s]) - ); - + requireNonNulls(runnables); + final AutoCloseable[] closables = new AutoCloseable[runnables.size()]; + int i = 0; + for (final Runnable r : runnables) { + closables[i++] = new CloseImpl(r); + } + composedClose(closables); } private static class CloseImpl implements AutoCloseable { diff --git a/src/main/java/com/speedment/internal/core/stream/builder/AbstractStreamBuilder.java b/src/main/java/com/speedment/internal/core/stream/builder/AbstractStreamBuilder.java index 2d223a30e6..f2d90b46ee 100644 --- a/src/main/java/com/speedment/internal/core/stream/builder/AbstractStreamBuilder.java +++ b/src/main/java/com/speedment/internal/core/stream/builder/AbstractStreamBuilder.java @@ -98,7 +98,9 @@ public void close() { if (!closed) { closed = true; try { - AbstractAutoClosingStream.composedRunnable(closeHandlers); // Run this stream's close handlers + if (!closeHandlers.isEmpty()) { + AbstractAutoClosingStream.composedRunnable(closeHandlers); // Run this stream's close handlers + } } catch (Exception e) { throw new SpeedmentException(e); } finally {