/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.commons.compress.utils;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import org.junit.Test;

public class CountingStreamTest {

    @Test
    public void output() throws Exception {
        // I don't like "test all at once" tests either, but the class
        // is so trivial
        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
        final CountingOutputStream o = new CountingOutputStream(bos);
        o.write(1);
        assertEquals(1, o.getBytesWritten());
        o.write(new byte[] { 2, 3 });
        assertEquals(3, o.getBytesWritten());
        o.write(new byte[] { 2, 3, 4, 5, }, 2, 1);
        assertEquals(4, o.getBytesWritten());
        o.count(-1);
        assertEquals(4, o.getBytesWritten());
        o.count(-2);
        assertEquals(2, o.getBytesWritten());
        o.close();
        assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bos.toByteArray());
    }

    @Test
    public void input() throws Exception {
        // I don't like "test all at once" tests either, but the class
        // is so trivial
        final ByteArrayInputStream bis =
            new ByteArrayInputStream(new byte[] { 1, 2, 3, 4 });
        final CountingInputStream i = new CountingInputStream(bis);
        assertEquals(1, i.read());
        assertEquals(1, i.getBytesRead());
        byte[] b = new byte[2];
        i.read(b);
        assertEquals(3, i.getBytesRead());
        assertArrayEquals(new byte[] { 2, 3 }, b);
        b = new byte[3];
        i.read(b, 1, 1);
        assertArrayEquals(new byte[] { 0, 4, 0 }, b);
        assertEquals(4, i.getBytesRead());
        i.count(-1);
        assertEquals(4, i.getBytesRead());
        i.count(-2);
        assertEquals(2, i.getBytesRead());
        i.close();
    }

}

@@ -1295,32 +1295,37 @@
 ream();%0A        
+try (
 final CountingOu
@@ -1368,18 +1368,24 @@
 eam(bos)
-;%0A
+) %7B%0A    
         
@@ -1392,24 +1392,28 @@
 o.write(1);%0A
+    
         asse
@@ -1446,32 +1446,36 @@
 ten());%0A        
+    
 o.write(new byte
@@ -1484,24 +1484,28 @@
  %7B 2, 3 %7D);%0A
+    
         asse
@@ -1538,32 +1538,36 @@
 ten());%0A        
+ 
+   
 o.write(new byte
@@ -1593,32 +1593,36 @@
  2, 1);%0A        
+    
 assertEquals(4, 
@@ -1643,32 +1643,36 @@
 ten());%0A        
+ 
+   
 o.count(-1);%0A   
@@ -1660,32 +1660,36 @@
    o.count(-1);%0A
+    
         assertEq
@@ -1718,32 +1718,36 @@
 ten());%0A        
+    
 o.count(-2);%0A   
@@ -1743,32 +1743,36 @@
 nt(-2);%0A        
+  
+  
 assertEquals(2, 
@@ -1805,18 +1805,9 @@
     
-o.close();
+%7D
 %0A   
@@ -2142,24 +2142,29 @@
 %7D);%0A        
+try (
 final Counti
@@ -2209,18 +2209,24 @@
 eam(bis)
-;%0A
+) %7B%0A    
         
@@ -2244,32 +2244,36 @@
 s(1, i.read());%0A
+    
         assertEq
@@ -2307,16 +2307,20 @@
         
+    
 byte%5B%5D b
@@ -2335,16 +2335,20 @@
 yte%5B2%5D;%0A
+    
         
@@ -2358,32 +2358,36 @@
 ead(b);%0A        
+    
 assertEquals(3, 
@@ -2397,32 +2397,36 @@
 etBytesRead());%0A
+    
         assertAr
@@ -2468,16 +2468,20 @@
         
+ 
+   
 b = new 
@@ -2489,16 +2489,20 @@
 yte%5B3%5D;%0A
+    
         
@@ -2518,32 +2518,36 @@
  1, 1);%0A        
+ 
+   
 assertArrayEqual
@@ -2576,32 +2576,36 @@
  %7D, b);%0A        
+    
 assertEquals(4, 
@@ -2623,32 +2623,36 @@
 ead());%0A        
+  
+  
 i.count(-1);%0A   
@@ -2640,32 +2640,36 @@
    i.count(-1);%0A
+    
         assertEq
@@ -2695,32 +2695,36 @@
 ead());%0A        
+  
+  
 i.count(-2);%0A   
@@ -2712,32 +2712,36 @@
    i.count(-2);%0A
+    
         assertEq
@@ -2779,18 +2779,9 @@
     
-i.close();
+%7D
 %0A   

Use try with resources.