1 16 17 package org.apache.commons.io.input; 18 19 import java.io.ByteArrayInputStream ; 20 21 import junit.framework.TestCase; 22 23 28 public class CountingInputStreamTest extends TestCase { 29 30 public CountingInputStreamTest(String name) { 31 super(name); 32 } 33 34 public void testCounting() throws Exception { 35 String text = "A piece of text"; 36 byte[] bytes = text.getBytes(); 37 ByteArrayInputStream bais = new ByteArrayInputStream (bytes); 38 CountingInputStream cis = new CountingInputStream(bais); 39 40 byte[] result = new byte[21]; 44 45 byte[] ba = new byte[5]; 46 int found = cis.read(ba); 47 System.arraycopy(ba, 0, result, 0, 5); 48 assertEquals( found, cis.getCount() ); 49 50 int value = cis.read(); 51 found++; 52 result[5] = (byte)value; 53 assertEquals( found, cis.getCount() ); 54 55 found += cis.read(result, 6, 5); 56 assertEquals( found, cis.getCount() ); 57 58 found += cis.read(result, 11, 10); assertEquals( found, cis.getCount() ); 60 61 String textResult = new String (result).trim(); 63 assertEquals(textResult, text); 64 } 65 } 66 67 | Popular Tags |