KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > io > input > SwappedDataInputStream


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.io.input;
17
18 import java.io.DataInput JavaDoc;
19 import java.io.EOFException JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22
23 import org.apache.commons.io.EndianUtils;
24
25 /**
26  * DataInput for systems relying on little endian data formats.
27  * When read, values will be changed from little endian to big
28  * endian formats for internal usage.
29  *
30  * <p><b>Origin of code: </b>Avalon Excalibur (IO)</p>
31  *
32  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
33  * @version CVS $Revision: 1.8 $ $Date: 2004/02/23 04:56:59 $
34  */

35 public class SwappedDataInputStream extends ProxyInputStream
36     implements DataInput JavaDoc
37 {
38
39     /**
40      * Constructs a SwappedDataInputStream.
41      *
42      * @param input InputStream to read from
43      */

44     public SwappedDataInputStream( InputStream JavaDoc input )
45     {
46         super( input );
47     }
48
49     /** @see java.io.DataInput#readBoolean() */
50     public boolean readBoolean()
51         throws IOException JavaDoc, EOFException JavaDoc
52     {
53         return ( 0 == readByte() );
54     }
55
56     /** @see java.io.DataInput#readByte() */
57     public byte readByte()
58         throws IOException JavaDoc, EOFException JavaDoc
59     {
60         return (byte)in.read();
61     }
62
63     /** @see java.io.DataInput#readChar() */
64     public char readChar()
65         throws IOException JavaDoc, EOFException JavaDoc
66     {
67         return (char)readShort();
68     }
69
70     /** @see java.io.DataInput#readDouble() */
71     public double readDouble()
72         throws IOException JavaDoc, EOFException JavaDoc
73     {
74         return EndianUtils.readSwappedDouble( in );
75     }
76
77     /** @see java.io.DataInput#readFloat() */
78     public float readFloat()
79         throws IOException JavaDoc, EOFException JavaDoc
80     {
81         return EndianUtils.readSwappedFloat( in );
82     }
83
84     /** @see java.io.DataInput#readFully(byte[]) */
85     public void readFully( byte[] data )
86         throws IOException JavaDoc, EOFException JavaDoc
87     {
88         readFully( data, 0, data.length );
89     }
90
91     /** @see java.io.DataInput#readFully(byte[], int, int) */
92     public void readFully( byte[] data, int offset, int length )
93         throws IOException JavaDoc, EOFException JavaDoc
94     {
95         int remaining = length;
96
97         while( remaining > 0 )
98         {
99             int location = offset + ( length - remaining );
100             int count = read( data, location, remaining );
101
102             if( -1 == count )
103             {
104                 throw new EOFException JavaDoc();
105             }
106
107             remaining -= count;
108         }
109     }
110
111     /** @see java.io.DataInput#readInt() */
112     public int readInt()
113         throws IOException JavaDoc, EOFException JavaDoc
114     {
115         return EndianUtils.readSwappedInteger( in );
116     }
117
118     /**
119      * Not currently supported.
120      *
121      * @see java.io.DataInput#readLine()
122      */

123     public String JavaDoc readLine()
124         throws IOException JavaDoc, EOFException JavaDoc
125     {
126         throw new UnsupportedOperationException JavaDoc(
127                 "Operation not supported: readLine()" );
128     }
129
130     /** @see java.io.DataInput#readLong() */
131     public long readLong()
132         throws IOException JavaDoc, EOFException JavaDoc
133     {
134         return EndianUtils.readSwappedLong( in );
135     }
136
137     /** @see java.io.DataInput#readShort() */
138     public short readShort()
139         throws IOException JavaDoc, EOFException JavaDoc
140     {
141         return EndianUtils.readSwappedShort( in );
142     }
143
144     /** @see java.io.DataInput#readUnsignedByte() */
145     public int readUnsignedByte()
146         throws IOException JavaDoc, EOFException JavaDoc
147     {
148         return in.read();
149     }
150
151     /** @see java.io.DataInput#readUnsignedShort() */
152     public int readUnsignedShort()
153         throws IOException JavaDoc, EOFException JavaDoc
154     {
155         return EndianUtils.readSwappedUnsignedShort( in );
156     }
157
158     /**
159      * Not currently supported.
160      *
161      * @see java.io.DataInput#readUTF()
162      */

163     public String JavaDoc readUTF()
164         throws IOException JavaDoc, EOFException JavaDoc
165     {
166         throw new UnsupportedOperationException JavaDoc(
167                 "Operation not supported: readUTF()" );
168     }
169
170     /** @see java.io.DataInput#skipBytes(int) */
171     public int skipBytes( int count )
172         throws IOException JavaDoc, EOFException JavaDoc
173     {
174         return (int)in.skip( count );
175     }
176
177 }
178
Popular Tags