alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Commons IO example source code file (CountingOutputStreamTest.java)

This example Commons IO source code file (CountingOutputStreamTest.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Commons IO tags/keywords

arithmeticexception, arithmeticexception, bytearrayoutputstream, countingoutputstream, countingoutputstream, countingoutputstreamtest, expected, expected, io, ioexception, nullinputstream, nullinputstream, nulloutputstream, outputstream, testcase

The Commons IO CountingOutputStreamTest.java source code

/*
 * 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.io.output;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import junit.framework.TestCase;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.NullInputStream;


/**
 * @version $Revision: 618694 $ $Date: 2008-02-05 16:34:21 +0000 (Tue, 05 Feb 2008) $
 */

public class CountingOutputStreamTest extends TestCase {

    public CountingOutputStreamTest(String name) {
        super(name);
    }

    public void testCounting() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        CountingOutputStream cos = new CountingOutputStream(baos);

        for(int i = 0; i < 20; i++) {
            cos.write(i);
        }
        assertByteArrayEquals("CountingOutputStream.write(int)", baos.toByteArray(), 0, 20);
        assertEquals("CountingOutputStream.getCount()", cos.getCount(), 20);

        byte[] array = new byte[10];
        for(int i = 20; i < 30; i++) {
            array[i-20] = (byte)i;
        }
        cos.write(array);
        assertByteArrayEquals("CountingOutputStream.write(byte[])", baos.toByteArray(), 0, 30);
        assertEquals("CountingOutputStream.getCount()", cos.getCount(), 30);

        for(int i = 25; i < 35; i++) {
            array[i-25] = (byte)i;
        }
        cos.write(array, 5, 5);
        assertByteArrayEquals("CountingOutputStream.write(byte[], int, int)", baos.toByteArray(), 0, 35);
        assertEquals("CountingOutputStream.getCount()", cos.getCount(), 35);

        int count = cos.resetCount();
        assertEquals("CountingOutputStream.resetCount()", count, 35);

        for(int i = 0; i < 10; i++) {
            cos.write(i);
        }
        assertByteArrayEquals("CountingOutputStream.write(int)", baos.toByteArray(), 35, 45);
        assertEquals("CountingOutputStream.getCount()", cos.getCount(), 10);

    }

    /**
     * Test for files > 2GB in size - see issue IO-84
     */
    public void testLargeFiles_IO84() throws Exception {
        long size = (long)Integer.MAX_VALUE + (long)1;

        NullInputStream mock     = new NullInputStream(size);
        OutputStream nos         = new NullOutputStream();
        CountingOutputStream cos = new CountingOutputStream(nos);

        // Test integer methods
        IOUtils.copyLarge(mock, cos);
        try {
            cos.getCount();
            fail("Expected getCount() to throw an ArithmeticException");
        } catch (ArithmeticException ae) {
            // expected result
        }
        try {
            cos.resetCount();
            fail("Expected resetCount() to throw an ArithmeticException");
        } catch (ArithmeticException ae) {
            // expected result
        }

        mock.close();

        // Test long methods
        IOUtils.copyLarge(mock, cos);
        assertEquals("getByteCount()",   size, cos.getByteCount());
        assertEquals("resetByteCount()", size, cos.resetByteCount());
    }

    private void assertByteArrayEquals(String msg, byte[] array, int start, int end) {
        for (int i = start; i < end; i++) {
            assertEquals(msg+": array[" + i + "] mismatch", array[i], i-start);
        }
    }

}

Other Commons IO examples (source code examples)

Here is a short list of links related to this Commons IO CountingOutputStreamTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.