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 * @author <a href="mailto:peter@apache.org">Peter Donald</a>
34 * @version CVS $Revision: 437567 $ $Date: 2006-08-28 08:39:07 +0200 (Mo, 28 Aug 2006) $
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( InputStream input )
46 {
47 super( input );
48 }
49
50 /** @see java.io.DataInput#readBoolean() */
51 public boolean readBoolean()
52 throws IOException, EOFException
53 {
54 return ( 0 == readByte() );
55 }
56
57 /** @see java.io.DataInput#readByte() */
58 public byte readByte()
59 throws IOException, EOFException
60 {
61 return (byte)in.read();
62 }
63
64 /** @see java.io.DataInput#readChar() */
65 public char readChar()
66 throws IOException, EOFException
67 {
68 return (char)readShort();
69 }
70
71 /** @see java.io.DataInput#readDouble() */
72 public double readDouble()
73 throws IOException, EOFException
74 {
75 return EndianUtils.readSwappedDouble( in );
76 }
77
78 /** @see java.io.DataInput#readFloat() */
79 public float readFloat()
80 throws IOException, EOFException
81 {
82 return EndianUtils.readSwappedFloat( in );
83 }
84
85 /** @see java.io.DataInput#readFully(byte[]) */
86 public void readFully( byte[] data )
87 throws IOException, EOFException
88 {
89 readFully( data, 0, data.length );
90 }
91
92 /** @see java.io.DataInput#readFully(byte[], int, int) */
93 public void readFully( byte[] data, int offset, int length )
94 throws IOException, EOFException
95 {
96 int remaining = length;
97
98 while( remaining > 0 )
99 {
100 int location = offset + ( length - remaining );
101 int count = read( data, location, remaining );
102
103 if( -1 == count )
104 {
105 throw new EOFException();
106 }
107
108 remaining -= count;
109 }
110 }
111
112 /** @see java.io.DataInput#readInt() */
113 public int readInt()
114 throws IOException, EOFException
115 {
116 return EndianUtils.readSwappedInteger( in );
117 }
118
119 /**
120 * Not currently supported.
121 *
122 * @see java.io.DataInput#readLine()
123 */
124 public String readLine()
125 throws IOException, EOFException
126 {
127 throw new UnsupportedOperationException(
128 "Operation not supported: readLine()" );
129 }
130
131 /** @see java.io.DataInput#readLong() */
132 public long readLong()
133 throws IOException, EOFException
134 {
135 return EndianUtils.readSwappedLong( in );
136 }
137
138 /** @see java.io.DataInput#readShort() */
139 public short readShort()
140 throws IOException, EOFException
141 {
142 return EndianUtils.readSwappedShort( in );
143 }
144
145 /** @see java.io.DataInput#readUnsignedByte() */
146 public int readUnsignedByte()
147 throws IOException, EOFException
148 {
149 return in.read();
150 }
151
152 /** @see java.io.DataInput#readUnsignedShort() */
153 public int readUnsignedShort()
154 throws IOException, EOFException
155 {
156 return EndianUtils.readSwappedUnsignedShort( in );
157 }
158
159 /**
160 * Not currently supported.
161 *
162 * @see java.io.DataInput#readUTF()
163 */
164 public String readUTF()
165 throws IOException, EOFException
166 {
167 throw new UnsupportedOperationException(
168 "Operation not supported: readUTF()" );
169 }
170
171 /** @see java.io.DataInput#skipBytes(int) */
172 public int skipBytes( int count )
173 throws IOException, EOFException
174 {
175 return (int)in.skip( count );
176 }
177
178 }