home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Commons IO example source code file (SwappedDataInputStreamTest.java)

This example Commons IO source code file (SwappedDataInputStreamTest.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

bytearrayinputstream, bytearrayinputstream, io, ioexception, ioexception, override, override, swappeddatainputstream, swappeddatainputstream, swappeddatainputstreamtest, testcase

The Commons IO SwappedDataInputStreamTest.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.input;


import java.io.ByteArrayInputStream;
import java.io.IOException;

import junit.framework.TestCase;


/**
 * Test for the SwappedDataInputStream. This also 
 * effectively tests the underlying EndianUtils Stream methods.
 *
 * @version $Revision: 995160 $ $Date: 2010-09-08 18:15:52 +0100 (Wed, 08 Sep 2010) $
 */

public class SwappedDataInputStreamTest extends TestCase {

    private SwappedDataInputStream sdis;
    private byte[] bytes;

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

    @Override
    public void setUp() {
        bytes = new byte[] {
            0x01,
            0x02,
            0x03,
            0x04,
            0x05,
            0x06,
            0x07,
            0x08
        };
        ByteArrayInputStream bais = new ByteArrayInputStream( bytes );
        this.sdis = new SwappedDataInputStream( bais );
    }

    @Override
    public void tearDown() {
        this.sdis = null;
    }

    public void testReadBoolean() throws IOException {
        bytes = new byte[] {
            0x00,
            0x01,
            0x02,
        };
        ByteArrayInputStream bais = new ByteArrayInputStream( bytes );
        SwappedDataInputStream sdis = new SwappedDataInputStream( bais );
        assertEquals( false, sdis.readBoolean() );
        assertEquals( true, sdis.readBoolean() );
        assertEquals( true, sdis.readBoolean() );
    }

    public void testReadByte() throws IOException {
        assertEquals( 0x01, this.sdis.readByte() );
    }

    public void testReadChar() throws IOException {
        assertEquals( (char) 0x0201, this.sdis.readChar() );
    }

    public void testReadDouble() throws IOException {
        assertEquals( Double.longBitsToDouble(0x0807060504030201L), this.sdis.readDouble(), 0 );
    }

    public void testReadFloat() throws IOException {
        assertEquals( Float.intBitsToFloat(0x04030201), this.sdis.readFloat(), 0 );
    }

    public void testReadFully() throws IOException {
        byte[] bytesIn = new byte[8];
        this.sdis.readFully(bytesIn);
        for( int i=0; i<8; i++) {
            assertEquals( bytes[i], bytesIn[i] );
        }
    }

    public void testReadInt() throws IOException {
        assertEquals( 0x04030201, this.sdis.readInt() );
    }

    public void testReadLine() throws IOException {
        try {
            this.sdis.readLine();
            fail("readLine should be unsupported. ");
        } catch(UnsupportedOperationException uoe) {
        }
    }

    public void testReadLong() throws IOException {
        assertEquals( 0x0807060504030201L, this.sdis.readLong() );
    }

    public void testReadShort() throws IOException {
        assertEquals( (short) 0x0201, this.sdis.readShort() );
    }

    public void testReadUnsignedByte() throws IOException {
        assertEquals( 0x01, this.sdis.readUnsignedByte() );
    }

    public void testReadUnsignedShort() throws IOException {
        assertEquals( (short) 0x0201, this.sdis.readUnsignedShort() );
    }

    public void testReadUTF() throws IOException {
        try {
            this.sdis.readUTF();
            fail("readUTF should be unsupported. ");
        } catch(UnsupportedOperationException uoe) {
        }
    }

    public void testSkipBytes() throws IOException {
        this.sdis.skipBytes(4);
        assertEquals( 0x08070605, this.sdis.readInt() );
    }

}

Other Commons IO examples (source code examples)

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



my book on functional programming

 

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.