KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > io > SwappedDataInputStream


1 /*
2
3  ============================================================================
4                    The Apache Software License, Version 1.1
5  ============================================================================
6  
7  Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  
9  Redistribution and use in source and binary forms, with or without modifica-
10  tion, are permitted provided that the following conditions are met:
11  
12  1. Redistributions of source code must retain the above copyright notice,
13     this list of conditions and the following disclaimer.
14  
15  2. Redistributions in binary form must reproduce the above copyright notice,
16     this list of conditions and the following disclaimer in the documentation
17     and/or other materials provided with the distribution.
18  
19  3. The end-user documentation included with the redistribution, if any, must
20     include the following acknowledgment: "This product includes software
21     developed by the Apache Software Foundation (http://www.apache.org/)."
22     Alternately, this acknowledgment may appear in the software itself, if
23     and wherever such third-party acknowledgments normally appear.
24  
25  4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation"
26     must not be used to endorse or promote products derived from this software
27     without prior written permission. For written permission, please contact
28     apache@apache.org.
29  
30  5. Products derived from this software may not be called "Apache", nor may
31     "Apache" appear in their name, without prior written permission of the
32     Apache Software Foundation.
33  
34  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  
45  This software consists of voluntary contributions made by many individuals
46  on behalf of the Apache Software Foundation. For more information on the
47  Apache Software Foundation, please see <http://www.apache.org/>.
48  
49 */

50 package org.apache.avalon.excalibur.io;
51
52 import java.io.DataInput JavaDoc;
53 import java.io.EOFException JavaDoc;
54 import java.io.IOException JavaDoc;
55 import java.io.InputStream JavaDoc;
56
57 /**
58  * DataInput for systems relying on little endian data formats.
59  *
60  * @author <a HREF="mailto:peter at apache.org">Peter Donald</a>
61  * @version CVS $Revision: 1.4 $ $Date: 2003/03/22 12:46:24 $
62  * @since 4.0
63  */

64 public class SwappedDataInputStream
65     implements DataInput JavaDoc
66 {
67     //The underlying input stream
68
private InputStream JavaDoc m_input;
69
70     public SwappedDataInputStream( final InputStream JavaDoc input )
71     {
72         m_input = input;
73     }
74
75     public boolean readBoolean()
76         throws IOException JavaDoc, EOFException JavaDoc
77     {
78         return ( 0 == readByte() );
79     }
80
81     public byte readByte()
82         throws IOException JavaDoc, EOFException JavaDoc
83     {
84         return (byte)m_input.read();
85     }
86
87     public char readChar()
88         throws IOException JavaDoc, EOFException JavaDoc
89     {
90         return (char)readShort();
91     }
92
93     public double readDouble()
94         throws IOException JavaDoc, EOFException JavaDoc
95     {
96         return EndianUtil.readSwappedDouble( m_input );
97     }
98
99     public float readFloat()
100         throws IOException JavaDoc, EOFException JavaDoc
101     {
102         return EndianUtil.readSwappedFloat( m_input );
103     }
104
105     public void readFully( final byte[] data )
106         throws IOException JavaDoc, EOFException JavaDoc
107     {
108         readFully( data, 0, data.length );
109     }
110
111     public void readFully( final byte[] data, final int offset, final int length )
112         throws IOException JavaDoc, EOFException JavaDoc
113     {
114         int remaining = length;
115
116         while( remaining > 0 )
117         {
118             final int location = offset + ( length - remaining );
119             final int count = read( data, location, remaining );
120
121             if( -1 == count )
122             {
123                 throw new EOFException JavaDoc();
124             }
125
126             remaining -= count;
127         }
128     }
129
130     public int readInt()
131         throws IOException JavaDoc, EOFException JavaDoc
132     {
133         return EndianUtil.readSwappedInteger( m_input );
134     }
135
136     public String JavaDoc readLine()
137         throws IOException JavaDoc, EOFException JavaDoc
138     {
139         throw new IOException JavaDoc( "Operation not supported" );
140     }
141
142     public long readLong()
143         throws IOException JavaDoc, EOFException JavaDoc
144     {
145         return EndianUtil.readSwappedLong( m_input );
146     }
147
148     public short readShort()
149         throws IOException JavaDoc, EOFException JavaDoc
150     {
151         return EndianUtil.readSwappedShort( m_input );
152     }
153
154     public int readUnsignedByte()
155         throws IOException JavaDoc, EOFException JavaDoc
156     {
157         return m_input.read();
158     }
159
160     public int readUnsignedShort()
161         throws IOException JavaDoc, EOFException JavaDoc
162     {
163         return EndianUtil.readSwappedUnsignedShort( m_input );
164     }
165
166     public String JavaDoc readUTF()
167         throws IOException JavaDoc, EOFException JavaDoc
168     {
169         throw new IOException JavaDoc( "Operation not supported" );
170     }
171
172     public int skipBytes( final int count )
173         throws IOException JavaDoc, EOFException JavaDoc
174     {
175         return (int)m_input.skip( count );
176     }
177
178     public int available()
179         throws IOException JavaDoc, EOFException JavaDoc
180     {
181         return m_input.available();
182     }
183
184     public void close()
185         throws IOException JavaDoc, EOFException JavaDoc
186     {
187         m_input.close();
188     }
189
190     public int read()
191         throws IOException JavaDoc, EOFException JavaDoc
192     {
193         return m_input.read();
194     }
195
196     public int read( final byte[] data )
197         throws IOException JavaDoc, EOFException JavaDoc
198     {
199         return read( data, 0, data.length );
200     }
201
202     public int read( final byte[] data, final int offset, final int length )
203         throws IOException JavaDoc, EOFException JavaDoc
204     {
205         return m_input.read( data, offset, length );
206     }
207
208     public long skip( final long count )
209         throws IOException JavaDoc, EOFException JavaDoc
210     {
211         return m_input.skip( count );
212     }
213
214     public void mark( final int readLimit )
215     {
216         m_input.mark( readLimit );
217     }
218
219     public boolean markSupported()
220     {
221         return m_input.markSupported();
222     }
223
224     public void reset()
225         throws IOException JavaDoc
226     {
227         m_input.reset();
228     }
229 }
230
Popular Tags