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  
  */
 36  
 public class SwappedDataInputStream extends ProxyInputStream
 37  
     implements DataInput
 38  
 {
 39  
 
 40  
     /**
 41  
      * Constructs a SwappedDataInputStream.
 42  
      *
 43  
      * @param input InputStream to read from
 44  
      */
 45  
     public SwappedDataInputStream( final InputStream input )
 46  
     {
 47  15
         super( input );
 48  15
     }
 49  
 
 50  
     /**
 51  
      * Return <code>{@link #readByte()} != 0</code>
 52  
      * @return false if the byte read is zero, otherwise true
 53  
      * @throws IOException if an I/O error occurs
 54  
      * @throws EOFException if an end of file is reached unexpectedly
 55  
      */
 56  
     @Override
 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  
     @Override
 70  
     public byte readByte()
 71  
         throws IOException, EOFException
 72  
     {
 73  4
         return (byte)in.read();
 74  
     }
 75  
 
 76  
     /**
 77  
      * Reads a character delegating to {@link #readShort()}.
 78  
      * @return the byte read or -1 if the end of stream
 79  
      * @throws IOException if an I/O error occurs
 80  
      * @throws EOFException if an end of file is reached unexpectedly
 81  
      */
 82  
     @Override
 83  
     public char readChar()
 84  
         throws IOException, EOFException
 85  
     {
 86  1
         return (char)readShort();
 87  
     }
 88  
 
 89  
     /**
 90  
      * Delegates to {@link EndianUtils#readSwappedDouble(InputStream)}. 
 91  
      * @return the read long
 92  
      * @throws IOException if an I/O error occurs
 93  
      * @throws EOFException if an end of file is reached unexpectedly
 94  
      */
 95  
     @Override
 96  
     public double readDouble()
 97  
         throws IOException, EOFException
 98  
     {
 99  1
         return EndianUtils.readSwappedDouble( in );
 100  
     }
 101  
 
 102  
     /**
 103  
      * Delegates to {@link EndianUtils#readSwappedFloat(InputStream)}. 
 104  
      * @return the read long
 105  
      * @throws IOException if an I/O error occurs
 106  
      * @throws EOFException if an end of file is reached unexpectedly
 107  
      */
 108  
     @Override
 109  
     public float readFloat()
 110  
         throws IOException, EOFException
 111  
     {
 112  1
         return EndianUtils.readSwappedFloat( in );
 113  
     }
 114  
 
 115  
     /**
 116  
      * Invokes the delegate's <code>read(byte[] data, int, int)</code> method.
 117  
      * 
 118  
      * @param data the buffer to read the bytes into
 119  
      * @throws EOFException if an end of file is reached unexpectedly
 120  
      * @throws IOException if an I/O error occurs
 121  
      */
 122  
     @Override
 123  
     public void readFully( final byte[] data )
 124  
         throws IOException, EOFException
 125  
     {
 126  1
         readFully( data, 0, data.length );
 127  1
     }
 128  
 
 129  
 
 130  
     /**
 131  
      * Invokes the delegate's <code>read(byte[] data, int, int)</code> method.
 132  
      * 
 133  
      * @param data the buffer to read the bytes into
 134  
      * @param offset The start offset
 135  
      * @param length The number of bytes to read
 136  
      * @throws EOFException if an end of file is reached unexpectedly
 137  
      * @throws IOException if an I/O error occurs
 138  
      */
 139  
     @Override
 140  
     public void readFully( final byte[] data, final int offset, final int length )
 141  
         throws IOException, EOFException
 142  
     {
 143  1
         int remaining = length;
 144  
 
 145  2
         while( remaining > 0 )
 146  
         {
 147  1
             final int location = offset + length - remaining;
 148  1
             final int count = read( data, location, remaining );
 149  
 
 150  1
             if (EOF == count)
 151  
             {
 152  0
                 throw new EOFException();
 153  
             }
 154  
 
 155  1
             remaining -= count;
 156  1
         }
 157  1
     }
 158  
 
 159  
     /**
 160  
      * Delegates to {@link EndianUtils#readSwappedInteger(InputStream)}. 
 161  
      * @return the read long
 162  
      * @throws EOFException if an end of file is reached unexpectedly
 163  
      * @throws IOException if an I/O error occurs
 164  
      */
 165  
     @Override
 166  
     public int readInt()
 167  
         throws IOException, EOFException
 168  
     {
 169  2
         return EndianUtils.readSwappedInteger( in );
 170  
     }
 171  
 
 172  
     /**
 173  
      * Not currently supported - throws {@link UnsupportedOperationException}.
 174  
      * @return the line read
 175  
      * @throws EOFException if an end of file is reached unexpectedly
 176  
      * @throws IOException if an I/O error occurs
 177  
      */
 178  
     @Override
 179  
     public String readLine()
 180  
         throws IOException, EOFException
 181  
     {
 182  1
         throw new UnsupportedOperationException( 
 183  
                 "Operation not supported: readLine()" );
 184  
     }
 185  
 
 186  
     /**
 187  
      * Delegates to {@link EndianUtils#readSwappedLong(InputStream)}. 
 188  
      * @return the read long
 189  
      * @throws EOFException if an end of file is reached unexpectedly
 190  
      * @throws IOException if an I/O error occurs
 191  
      */
 192  
     @Override
 193  
     public long readLong()
 194  
         throws IOException, EOFException
 195  
     {
 196  1
         return EndianUtils.readSwappedLong( in );
 197  
     }
 198  
 
 199  
     /**
 200  
      * Delegates to {@link EndianUtils#readSwappedShort(InputStream)}. 
 201  
      * @return the read long
 202  
      * @throws EOFException if an end of file is reached unexpectedly
 203  
      * @throws IOException if an I/O error occurs
 204  
      */
 205  
     @Override
 206  
     public short readShort()
 207  
         throws IOException, EOFException
 208  
     {
 209  2
         return EndianUtils.readSwappedShort( in );
 210  
     }
 211  
 
 212  
     /**
 213  
      * Invokes the delegate's <code>read()</code> method.
 214  
      * @return the byte read or -1 if the end of stream
 215  
      * @throws EOFException if an end of file is reached unexpectedly
 216  
      * @throws IOException if an I/O error occurs
 217  
      */
 218  
     @Override
 219  
     public int readUnsignedByte()
 220  
         throws IOException, EOFException
 221  
     {
 222  1
         return in.read();
 223  
     }
 224  
 
 225  
     /**
 226  
      * Delegates to {@link EndianUtils#readSwappedUnsignedShort(InputStream)}. 
 227  
      * @return the read long
 228  
      * @throws EOFException if an end of file is reached unexpectedly
 229  
      * @throws IOException if an I/O error occurs
 230  
      */
 231  
     @Override
 232  
     public int readUnsignedShort()
 233  
         throws IOException, EOFException
 234  
     {
 235  1
         return EndianUtils.readSwappedUnsignedShort( in );
 236  
     }
 237  
 
 238  
     /**
 239  
      * Not currently supported - throws {@link UnsupportedOperationException}.
 240  
      * @return UTF String read
 241  
      * @throws EOFException if an end of file is reached unexpectedly
 242  
      * @throws IOException if an I/O error occurs
 243  
      */
 244  
     @Override
 245  
     public String readUTF()
 246  
         throws IOException, EOFException
 247  
     {
 248  1
         throw new UnsupportedOperationException( 
 249  
                 "Operation not supported: readUTF()" );
 250  
     }
 251  
 
 252  
     /**
 253  
      * Invokes the delegate's <code>skip(int)</code> method.
 254  
      * @param count the number of bytes to skip
 255  
      * @return the number of bytes to skipped or -1 if the end of stream
 256  
      * @throws EOFException if an end of file is reached unexpectedly
 257  
      * @throws IOException if an I/O error occurs
 258  
      */
 259  
     @Override
 260  
     public int skipBytes( final int count )
 261  
         throws IOException, EOFException
 262  
     {
 263  1
         return (int)in.skip( count );
 264  
     }
 265  
 
 266  
 }