Coverage Report - org.apache.commons.io.input.SwappedDataInputStream
 
Classes in this File Line Coverage Branch Coverage Complexity
SwappedDataInputStream
96%
25/26
83%
5/6
1.312
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  * 
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.io.input;
 18  
 
 19  
 import static org.apache.commons.io.IOUtils.EOF;
 20  
 
 21  
 import java.io.DataInput;
 22  
 import java.io.EOFException;
 23  
 import java.io.IOException;
 24  
 import java.io.InputStream;
 25  
 
 26  
 import org.apache.commons.io.EndianUtils;
 27  
 
 28  
 /**
 29  
  * DataInput for systems relying on little endian data formats.
 30  
  * When read, values will be changed from little endian to big 
 31  
  * endian formats for internal usage. 
 32  
  * <p>
 33  
  * <b>Origin of code: </b>Avalon Excalibur (IO)
 34  
  *
 35  
  * @version CVS $Revision: 1302050 $
 36  
  */
 37  
 public class SwappedDataInputStream extends ProxyInputStream
 38  
     implements DataInput
 39  
 {
 40  
 
 41  
     /**
 42  
      * Constructs a SwappedDataInputStream.
 43  
      *
 44  
      * @param input InputStream to read from
 45  
      */
 46  
     public SwappedDataInputStream( final InputStream input )
 47  
     {
 48  15
         super( input );
 49  15
     }
 50  
 
 51  
     /**
 52  
      * Return <code>{@link #readByte()} != 0</code>
 53  
      * @return false if the byte read is zero, otherwise true
 54  
      * @throws IOException if an I/O error occurs
 55  
      * @throws EOFException if an end of file is reached unexpectedly
 56  
      */
 57  
     public boolean readBoolean()
 58  
         throws IOException, EOFException
 59  
     {
 60  3
         return 0 != readByte();
 61  
     }
 62  
 
 63  
     /**
 64  
      * Invokes the delegate's <code>read()</code> method.
 65  
      * @return the byte read or -1 if the end of stream
 66  
      * @throws IOException if an I/O error occurs
 67  
      * @throws EOFException if an end of file is reached unexpectedly
 68  
      */
 69  
     public byte readByte()
 70  
         throws IOException, EOFException
 71  
     {
 72  4
         return (byte)in.read();
 73  
     }
 74  
 
 75  
     /**
 76  
      * Reads a character delegating to {@link #readShort()}.
 77  
      * @return the byte read or -1 if the end of stream
 78  
      * @throws IOException if an I/O error occurs
 79  
      * @throws EOFException if an end of file is reached unexpectedly
 80  
      */
 81  
     public char readChar()
 82  
         throws IOException, EOFException
 83  
     {
 84  1
         return (char)readShort();
 85  
     }
 86  
 
 87  
     /**
 88  
      * Delegates to {@link EndianUtils#readSwappedDouble(InputStream)}. 
 89  
      * @return the read long
 90  
      * @throws IOException if an I/O error occurs
 91  
      * @throws EOFException if an end of file is reached unexpectedly
 92  
      */
 93  
     public double readDouble()
 94  
         throws IOException, EOFException
 95  
     {
 96  1
         return EndianUtils.readSwappedDouble( in );
 97  
     }
 98  
 
 99  
     /**
 100  
      * Delegates to {@link EndianUtils#readSwappedFloat(InputStream)}. 
 101  
      * @return the read long
 102  
      * @throws IOException if an I/O error occurs
 103  
      * @throws EOFException if an end of file is reached unexpectedly
 104  
      */
 105  
     public float readFloat()
 106  
         throws IOException, EOFException
 107  
     {
 108  1
         return EndianUtils.readSwappedFloat( in );
 109  
     }
 110  
 
 111  
     /**
 112  
      * Invokes the delegate's <code>read(byte[] data, int, int)</code> method.
 113  
      * 
 114  
      * @param data the buffer to read the bytes into
 115  
      * @throws EOFException if an end of file is reached unexpectedly
 116  
      * @throws IOException if an I/O error occurs
 117  
      */
 118  
     public void readFully( final byte[] data )
 119  
         throws IOException, EOFException
 120  
     {
 121  1
         readFully( data, 0, data.length );
 122  1
     }
 123  
 
 124  
 
 125  
     /**
 126  
      * Invokes the delegate's <code>read(byte[] data, int, int)</code> method.
 127  
      * 
 128  
      * @param data the buffer to read the bytes into
 129  
      * @param offset The start offset
 130  
      * @param length The number of bytes to read
 131  
      * @throws EOFException if an end of file is reached unexpectedly
 132  
      * @throws IOException if an I/O error occurs
 133  
      */
 134  
     public void readFully( final byte[] data, final int offset, final int length )
 135  
         throws IOException, EOFException
 136  
     {
 137  1
         int remaining = length;
 138  
 
 139  2
         while( remaining > 0 )
 140  
         {
 141  1
             final int location = offset + length - remaining;
 142  1
             final int count = read( data, location, remaining );
 143  
 
 144  1
             if (EOF == count)
 145  
             {
 146  0
                 throw new EOFException();
 147  
             }
 148  
 
 149  1
             remaining -= count;
 150  1
         }
 151  1
     }
 152  
 
 153  
     /**
 154  
      * Delegates to {@link EndianUtils#readSwappedInteger(InputStream)}. 
 155  
      * @return the read long
 156  
      * @throws EOFException if an end of file is reached unexpectedly
 157  
      * @throws IOException if an I/O error occurs
 158  
      */
 159  
     public int readInt()
 160  
         throws IOException, EOFException
 161  
     {
 162  2
         return EndianUtils.readSwappedInteger( in );
 163  
     }
 164  
 
 165  
     /**
 166  
      * Not currently supported - throws {@link UnsupportedOperationException}.
 167  
      * @return the line read
 168  
      * @throws EOFException if an end of file is reached unexpectedly
 169  
      * @throws IOException if an I/O error occurs
 170  
      */
 171  
     public String readLine()
 172  
         throws IOException, EOFException
 173  
     {
 174  1
         throw new UnsupportedOperationException( 
 175  
                 "Operation not supported: readLine()" );
 176  
     }
 177  
 
 178  
     /**
 179  
      * Delegates to {@link EndianUtils#readSwappedLong(InputStream)}. 
 180  
      * @return the read long
 181  
      * @throws EOFException if an end of file is reached unexpectedly
 182  
      * @throws IOException if an I/O error occurs
 183  
      */
 184  
     public long readLong()
 185  
         throws IOException, EOFException
 186  
     {
 187  1
         return EndianUtils.readSwappedLong( in );
 188  
     }
 189  
 
 190  
     /**
 191  
      * Delegates to {@link EndianUtils#readSwappedShort(InputStream)}. 
 192  
      * @return the read long
 193  
      * @throws EOFException if an end of file is reached unexpectedly
 194  
      * @throws IOException if an I/O error occurs
 195  
      */
 196  
     public short readShort()
 197  
         throws IOException, EOFException
 198  
     {
 199  2
         return EndianUtils.readSwappedShort( in );
 200  
     }
 201  
 
 202  
     /**
 203  
      * Invokes the delegate's <code>read()</code> method.
 204  
      * @return the byte read or -1 if the end of stream
 205  
      * @throws EOFException if an end of file is reached unexpectedly
 206  
      * @throws IOException if an I/O error occurs
 207  
      */
 208  
     public int readUnsignedByte()
 209  
         throws IOException, EOFException
 210  
     {
 211  1
         return in.read();
 212  
     }
 213  
 
 214  
     /**
 215  
      * Delegates to {@link EndianUtils#readSwappedUnsignedShort(InputStream)}. 
 216  
      * @return the read long
 217  
      * @throws EOFException if an end of file is reached unexpectedly
 218  
      * @throws IOException if an I/O error occurs
 219  
      */
 220  
     public int readUnsignedShort()
 221  
         throws IOException, EOFException
 222  
     {
 223  1
         return EndianUtils.readSwappedUnsignedShort( in );
 224  
     }
 225  
 
 226  
     /**
 227  
      * Not currently supported - throws {@link UnsupportedOperationException}.
 228  
      * @return UTF String read
 229  
      * @throws EOFException if an end of file is reached unexpectedly
 230  
      * @throws IOException if an I/O error occurs
 231  
      */
 232  
     public String readUTF()
 233  
         throws IOException, EOFException
 234  
     {
 235  1
         throw new UnsupportedOperationException( 
 236  
                 "Operation not supported: readUTF()" );
 237  
     }
 238  
 
 239  
     /**
 240  
      * Invokes the delegate's <code>skip(int)</code> method.
 241  
      * @param count the number of bytes to skip
 242  
      * @return the number of bytes to skipped or -1 if the end of stream
 243  
      * @throws EOFException if an end of file is reached unexpectedly
 244  
      * @throws IOException if an I/O error occurs
 245  
      */
 246  
     public int skipBytes( final int count )
 247  
         throws IOException, EOFException
 248  
     {
 249  1
         return (int)in.skip( count );
 250  
     }
 251  
 
 252  
 }