/*
 * Copyright 2018, TeamDev. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.testing.logging;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;

/**
 * An {@link java.io.OutputStream} which stores its input.
 *
 * <p>The stream stores all the given bytes in a {@link java.nio.ByteBuffer}. The size of the buffer is
 * exactly 1 MiB. If the steam is not {@linkplain #clear() cleared} before the buffer fills up,
 * an {@link java.nio.BufferOverflowException} occurs.
 *
 * @author Dmytro Dashenkov
 */
final class MemoizingStream extends OutputStream {

    private static final int TEN_MEBI_BYTES = 10 * 1024 * 1024;

    private final ByteBuffer memory = ByteBuffer.allocate(TEN_MEBI_BYTES);

    @Override
    public void write(int b) {
        /*
           According to the `OutputStream` contract, a negative value may represent the end of
           the stream. The actual data is the lowest 8 bits of the int.
         */
        if (b >= 0) {
            @SuppressWarnings("NumericCastThatLosesPrecision")
                // Adheres to the OutputStream contract.
            byte byteValue = (byte) b;
            memory.put(byteValue);
        }
    }

    /**
     * Clears the memoized input.
     */
    void clear() {
        memory.clear();
    }

    /**
     * Copies the memoized input into the given stream and {@linkplain #clear() clears} memory.
     *
     * @param stream the target stream
     * @throws java.io.IOException if the target stream throws an {@link java.io.IOException} on a write operation
     */
    void flushTo(OutputStream stream) throws IOException {
        int entryCount = memory.position();
        for (int i = 0; i < entryCount; i++) {
            byte byteValue = memory.get(i);
            stream.write(byteValue);
        }
    }
}

@@ -1114,295 +1114,105 @@
 ava.
-nio.ByteBuffer;%0A%0A/**%0A * An %7B@link java.io.OutputStream%7D which stores its input.%0A *%0A * %3Cp%3EThe stream stores all the given bytes in a %7B@link java.nio.ByteBuffer%7D. The size of the buffer is%0A * exactly 1 MiB. If the steam is not %7B@linkplain #clear() cleared%7D before the buffer fills up,
+util.List;%0A%0Aimport static com.google.common.collect.Lists.newArrayListWithExpectedSize;%0A%0A/**
 %0A * 
-a
+A
 n %7B@
@@ -1220,80 +1220,45 @@
 ink 
-java.nio.BufferOverflowException%7D occurs.%0A *%0A * @author Dmytro Dashenkov
+OutputStream%7D which stores its input.
 %0A */
@@ -1339,19 +1339,19 @@
 nal int 
-TEN
+ONE
 _MEBI_BY
@@ -1352,17 +1352,16 @@
 EBI_BYTE
-S
  = 10 * 
@@ -1396,51 +1396,60 @@
 nal 
-ByteBuffer memory = ByteBuffer.allocate(TEN
+List%3CByte%3E memory = newArrayListWithExpectedSize(ONE
 _MEB
@@ -1454,17 +1454,16 @@
 EBI_BYTE
-S
 );%0A%0A    
@@ -1893,19 +1893,19 @@
  memory.
-put
+add
 (byteVal
@@ -2191,24 +2191,16 @@
 @throws 
-java.io.
 IOExcept
@@ -2241,24 +2241,16 @@
  %7B@link 
-java.io.
 IOExcept
@@ -2354,42 +2354,47 @@
     
-int entryCount = memory.position
+byte%5B%5D buffer = new byte%5Bmemory.size
 ()
+%5D
 ;%0A  
@@ -2423,18 +2423,21 @@
 i %3C 
-entryCount
+memory.size()
 ; i+
@@ -2454,29 +2454,24 @@
        b
-yte byteValue
+uffer%5Bi%5D
  = memor
@@ -2486,22 +2486,28 @@
 ;%0A      
+  %7D%0A
       
+  
 stream.w
@@ -2512,24 +2512,21 @@
 .write(b
-yteValue
+uffer
 );%0A     
@@ -2524,22 +2524,29 @@
 r);%0A        
-%7D
+clear();
 %0A    %7D%0A%7D%0A

Use a list instead of a byte buffer.