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