View Javadoc

1   /*
2    * ObjectEndianInputStream
3    * 
4    * Created on 25 april 2001, 22:36 Last edited on October 2001
5    */
6   package nl.javel.gisbeans.io;
7   
8   import java.io.DataInput;
9   import java.io.DataInputStream;
10  import java.io.IOException;
11  import java.io.InputStream;
12  
13  /***
14   * This class enables the object inputstream to be switched from little to big
15   * endian. The class works exactly like an ObjectInputStream
16   * 
17   * @author <a href="mailto:paul.jacobs@javel.nl">Paul Jacobs </a><a
18   *         href="mailto:peter.jacobs@javel.nl">Peter Jacobs </a>
19   * 
20   * @version 1.0
21   * @since JDK 1.0
22   *  
23   */
24  public class ObjectEndianInputStream implements EndianInterface, DataInput
25  {
26  
27  	/*** the datainput stream */
28  	private DataInputStream dataInputStream;
29  
30  	/*** the inputStream */
31  	private InputStream inputStream;
32  
33  	/*** an 8byte buffer */
34  	private byte[] buffer = new byte[8];
35  
36  	/*** the code */
37  	private int encode = EndianInterface.BIG_ENDIAN;
38  
39  	/***
40  	 * constructs a new ObjectEndianInputStream
41  	 * 
42  	 * @param inputStream the inputStream to use
43  	 */
44  	public ObjectEndianInputStream(final InputStream inputStream)
45  	{
46  		this.inputStream = inputStream;
47  		this.dataInputStream = new DataInputStream(inputStream);
48  	}
49  
50  	/***
51  	 * @see java.io.DataInput#readShort()
52  	 */
53  	public final short readShort() throws IOException
54  	{
55  		if (this.encode == EndianInterface.BIG_ENDIAN)
56  		{
57  			return this.dataInputStream.readShort();
58  		}
59  		this.dataInputStream.readFully(this.buffer, 0, 2);
60  		return (short) ((this.buffer[1] & 0xff) << 8 | (this.buffer[0] & 0xff));
61  	}
62  
63  	/***
64  	 * @see java.io.DataInput#readUnsignedShort()
65  	 */
66  	public final int readUnsignedShort() throws IOException
67  	{
68  		if (this.encode == EndianInterface.BIG_ENDIAN)
69  		{
70  			return this.dataInputStream.readUnsignedShort();
71  		}
72  		this.dataInputStream.readFully(this.buffer, 0, 2);
73  		return ((this.buffer[1] & 0xff) << 8 | (this.buffer[0] & 0xff));
74  	}
75  
76  	/***
77  	 * @see java.io.DataInput#readChar()
78  	 */
79  	public final char readChar() throws IOException
80  	{
81  		if (this.encode == EndianInterface.BIG_ENDIAN)
82  		{
83  			return this.dataInputStream.readChar();
84  		}
85  		this.dataInputStream.readFully(this.buffer, 0, 2);
86  		return (char) ((this.buffer[1] & 0xff) << 8 | (this.buffer[0] & 0xff));
87  	}
88  
89  	/***
90  	 * @see java.io.DataInput#readInt()
91  	 */
92  	public final int readInt() throws IOException
93  	{
94  		if (this.encode == EndianInterface.BIG_ENDIAN)
95  		{
96  			return this.dataInputStream.readInt();
97  		}
98  		this.dataInputStream.readFully(this.buffer, 0, 4);
99  		return (this.buffer[3]) << 24 | (this.buffer[2] & 0xff) << 16
100 				| (this.buffer[1] & 0xff) << 8 | (this.buffer[0] & 0xff);
101 	}
102 
103 	/***
104 	 * @see java.io.DataInput#readLong()
105 	 */
106 	public final long readLong() throws IOException
107 	{
108 		if (this.encode == EndianInterface.BIG_ENDIAN)
109 		{
110 			return this.dataInputStream.readLong();
111 		}
112 		this.dataInputStream.readFully(this.buffer, 0, 8);
113 		return (long) (this.buffer[7]) << 56
114 				| (long) (this.buffer[6] & 0xff) << 48
115 				| (long) (this.buffer[5] & 0xff) << 40
116 				| (long) (this.buffer[4] & 0xff) << 32
117 				| (long) (this.buffer[3] & 0xff) << 24
118 				| (long) (this.buffer[2] & 0xff) << 16
119 				| (long) (this.buffer[1] & 0xff) << 8 | (this.buffer[0] & 0xff);
120 	}
121 
122 	/***
123 	 * reads a float from the stream
124 	 * 
125 	 * @see java.io.DataInput#readFloat()
126 	 */
127 	public final float readFloat() throws IOException
128 	{
129 		if (this.encode == EndianInterface.BIG_ENDIAN)
130 		{
131 			return this.dataInputStream.readFloat();
132 		}
133 		return Float.intBitsToFloat(readInt());
134 	}
135 
136 	/***
137 	 * @see java.io.DataInput#readDouble()
138 	 */
139 	public final double readDouble() throws IOException
140 	{
141 		if (this.encode == EndianInterface.BIG_ENDIAN)
142 		{
143 			return this.dataInputStream.readDouble();
144 		}
145 		return Double.longBitsToDouble(readLong());
146 	}
147 
148 	/***
149 	 * reads b from the stream
150 	 * 
151 	 * @param b
152 	 * @return in the value
153 	 * @throws IOException on failure
154 	 */
155 	public final int read(final byte[] b) throws IOException
156 	{
157 		return this.inputStream.read(b);
158 	}
159 
160 	/***
161 	 * reads b from the stream
162 	 * 
163 	 * @param b
164 	 * @param off
165 	 * @param len
166 	 * @return in the value
167 	 * @throws IOException on failure
168 	 */
169 	public final int read(final byte[] b, final int off, final int len)
170 			throws IOException
171 	{
172 		return this.inputStream.read(b, off, len);
173 	}
174 
175 	/***
176 	 * @see java.io.DataInput#readFully(byte[])
177 	 */
178 	public final void readFully(final byte[] b) throws IOException
179 	{
180 		this.dataInputStream.readFully(b, 0, b.length);
181 	}
182 
183 	/***
184 	 * @see java.io.DataInput#readFully(byte[], int, int)
185 	 */
186 	public final void readFully(final byte[] b, final int off, final int len)
187 			throws IOException
188 	{
189 		this.dataInputStream.readFully(b, off, len);
190 	}
191 
192 	/***
193 	 * @see java.io.DataInput#skipBytes(int)
194 	 */
195 	public final int skipBytes(final int n) throws IOException
196 	{
197 		return this.dataInputStream.skipBytes(n);
198 	}
199 
200 	/***
201 	 * @see java.io.DataInput#readBoolean()
202 	 */
203 	public final boolean readBoolean() throws IOException
204 	{
205 		return this.dataInputStream.readBoolean();
206 	}
207 
208 	/***
209 	 * @see java.io.DataInput#readByte()
210 	 */
211 	public final byte readByte() throws IOException
212 	{
213 		return this.dataInputStream.readByte();
214 	}
215 
216 	/***
217 	 * @see java.io.DataInput#readUnsignedByte()
218 	 */
219 	public final int readUnsignedByte() throws IOException
220 	{
221 		return this.dataInputStream.readUnsignedByte();
222 	}
223 
224 	/***
225 	 * @see java.io.DataInput#readUTF()
226 	 */
227 	public final String readUTF() throws IOException
228 	{
229 		return this.dataInputStream.readUTF();
230 	}
231 
232 	/***
233 	 * @see java.io.DataInput#readLine()
234 	 */
235 	public final String readLine()
236 	{
237 		return null; //This method is deprecated because it does not work
238 		// OK...
239 	}
240 
241 	/***
242 	 * reads UTF from the stream
243 	 * 
244 	 * @param dataInput
245 	 * @return String the value
246 	 * @throws IOException
247 	 */
248 	public static final String readUTF(final DataInput dataInput)
249 			throws IOException
250 	{
251 		return DataInputStream.readUTF(dataInput);
252 	}
253 
254 	/***
255 	 * @throws IOException
256 	 */
257 	public final void close() throws IOException
258 	{
259 		this.dataInputStream.close();
260 	}
261 
262 	/***
263 	 * @see nl.javel.gisbeans.io.EndianInterface#setEncode(int)
264 	 */
265 	public void setEncode(final int encode)
266 	{
267 		this.encode = encode;
268 	}
269 
270 	/***
271 	 * @see nl.javel.gisbeans.io.EndianInterface#getEncode()
272 	 */
273 	public int getEncode()
274 	{
275 		return this.encode;
276 	}
277 }