1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
28
29
30
31
32
33
34
35
36 public class SwappedDataInputStream extends ProxyInputStream
37 implements DataInput
38 {
39
40
41
42
43
44
45 public SwappedDataInputStream( InputStream input )
46 {
47 super( input );
48 }
49
50
51 public boolean readBoolean()
52 throws IOException, EOFException
53 {
54 return ( 0 == readByte() );
55 }
56
57
58 public byte readByte()
59 throws IOException, EOFException
60 {
61 return (byte)in.read();
62 }
63
64
65 public char readChar()
66 throws IOException, EOFException
67 {
68 return (char)readShort();
69 }
70
71
72 public double readDouble()
73 throws IOException, EOFException
74 {
75 return EndianUtils.readSwappedDouble( in );
76 }
77
78
79 public float readFloat()
80 throws IOException, EOFException
81 {
82 return EndianUtils.readSwappedFloat( in );
83 }
84
85
86 public void readFully( byte[] data )
87 throws IOException, EOFException
88 {
89 readFully( data, 0, data.length );
90 }
91
92
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
113 public int readInt()
114 throws IOException, EOFException
115 {
116 return EndianUtils.readSwappedInteger( in );
117 }
118
119
120
121
122
123
124 public String readLine()
125 throws IOException, EOFException
126 {
127 throw new UnsupportedOperationException(
128 "Operation not supported: readLine()" );
129 }
130
131
132 public long readLong()
133 throws IOException, EOFException
134 {
135 return EndianUtils.readSwappedLong( in );
136 }
137
138
139 public short readShort()
140 throws IOException, EOFException
141 {
142 return EndianUtils.readSwappedShort( in );
143 }
144
145
146 public int readUnsignedByte()
147 throws IOException, EOFException
148 {
149 return in.read();
150 }
151
152
153 public int readUnsignedShort()
154 throws IOException, EOFException
155 {
156 return EndianUtils.readSwappedUnsignedShort( in );
157 }
158
159
160
161
162
163
164 public String readUTF()
165 throws IOException, EOFException
166 {
167 throw new UnsupportedOperationException(
168 "Operation not supported: readUTF()" );
169 }
170
171
172 public int skipBytes( int count )
173 throws IOException, EOFException
174 {
175 return (int)in.skip( count );
176 }
177
178 }