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.DataOutput;
9   import java.io.DataOutputStream;
10  import java.io.IOException;
11  import java.io.OutputStream;
12  
13  /***
14   * This class enables the object outputstream to be switched from little to big
15   * endian. The class works exactly like an ObjectOutputStream
16   * 
17   * @author <a href="mailto:peter.jacobs@javel.nl">Peter Jacobs </a><a
18   *         href="mailto:paul.jacobs@javel.nl">Paul Jacobs </a>
19   * @version 1.0
20   * @since JDK 1.0
21   *  
22   */
23  public class ObjectEndianOutputStream implements EndianInterface, DataOutput
24  {
25  
26  	/*** the dataOutputStream */
27  	private DataOutputStream dataOutputStream = null;
28  
29  	/*** the buffer */
30  	private byte[] buffer = new byte[8];
31  
32  	/*** the encode */
33  	private int encode = EndianInterface.BIG_ENDIAN;
34  
35  	/***
36  	 * constructs a new ObjectEndianOutputStream.
37  	 * 
38  	 * @param outputStream the target.
39  	 */
40  	public ObjectEndianOutputStream(final OutputStream outputStream)
41  	{
42  		this.dataOutputStream = new DataOutputStream(outputStream);
43  	}
44  
45  	/***
46  	 * @see java.io.DataOutput#write(byte[], int, int)
47  	 */
48  	public void write(final byte[] buffer, final int off, final int len)
49  			throws IOException
50  	{
51  		this.dataOutputStream.write(this.buffer, off, len);
52  	}
53  
54  	/***
55  	 * @see java.io.DataOutput#writeFloat(float)
56  	 */
57  	public void writeFloat(final float value) throws IOException
58  	{
59  		if (this.encode == EndianInterface.BIG_ENDIAN)
60  		{
61  			this.dataOutputStream.writeFloat(value);
62  		} else
63  		{
64  			this.writeInt(Float.floatToIntBits(value));
65  		}
66  	}
67  
68  	/***
69  	 * @see java.io.DataOutput#write(int)
70  	 */
71  	public void write(final int value) throws IOException
72  	{
73  		this.dataOutputStream.write(value);
74  	}
75  
76  	/***
77  	 * @see java.io.DataOutput#writeShort(int)
78  	 */
79  	public void writeShort(final int value) throws IOException
80  	{
81  		if (this.encode == EndianInterface.BIG_ENDIAN)
82  		{
83  			this.dataOutputStream.writeShort(value);
84  		} else
85  		{
86  			this.buffer[0] = (byte) value;
87  			this.buffer[1] = (byte) (value >> 8);
88  			this.dataOutputStream.write(this.buffer, 0, 2);
89  		}
90  	}
91  
92  	/***
93  	 * @see java.io.DataOutput#writeBytes(java.lang.String)
94  	 */
95  	public void writeBytes(final String string) throws IOException
96  	{
97  		this.dataOutputStream.writeBytes(string);
98  	}
99  
100 	/***
101 	 * @see java.io.DataOutput#writeChar(int)
102 	 */
103 	public void writeChar(final int value) throws IOException
104 	{
105 		if (this.encode == EndianInterface.BIG_ENDIAN)
106 		{
107 			this.dataOutputStream.writeChar(value);
108 		} else
109 		{
110 			this.buffer[0] = (byte) value;
111 			this.buffer[1] = (byte) (value >> 8);
112 			this.dataOutputStream.write(this.buffer, 0, 2);
113 		}
114 	}
115 
116 	/***
117 	 * @see java.io.DataOutput#writeByte(int)
118 	 */
119 	public void writeByte(final int value) throws IOException
120 	{
121 		this.dataOutputStream.writeByte(value);
122 	}
123 
124 	/***
125 	 * @see java.io.DataOutput#writeBoolean(boolean)
126 	 */
127 	public void writeBoolean(final boolean value) throws IOException
128 	{
129 		this.dataOutputStream.writeBoolean(value);
130 	}
131 
132 	/***
133 	 * @see java.io.DataOutput#writeLong(long)
134 	 */
135 	public void writeLong(final long value) throws IOException
136 	{
137 		if (this.encode == EndianInterface.BIG_ENDIAN)
138 		{
139 			this.dataOutputStream.writeLong(value);
140 		} else
141 		{
142 			this.buffer[0] = (byte) value;
143 			this.buffer[1] = (byte) (value >> 8);
144 			this.buffer[2] = (byte) (value >> 16);
145 			this.buffer[3] = (byte) (value >> 24);
146 			this.buffer[4] = (byte) (value >> 32);
147 			this.buffer[5] = (byte) (value >> 40);
148 			this.buffer[6] = (byte) (value >> 48);
149 			this.buffer[7] = (byte) (value >> 56);
150 			this.dataOutputStream.write(this.buffer, 0, 8);
151 		}
152 	}
153 
154 	/***
155 	 * @see java.io.DataOutput#writeUTF(java.lang.String)
156 	 */
157 	public void writeUTF(final String string) throws IOException
158 	{
159 		this.dataOutputStream.writeUTF(string);
160 	}
161 
162 	/***
163 	 * @see java.io.DataOutput#writeInt(int)
164 	 */
165 	public void writeInt(final int value) throws IOException
166 	{
167 		if (this.encode == EndianInterface.BIG_ENDIAN)
168 		{
169 			this.dataOutputStream.writeInt(value);
170 		} else
171 		{
172 			this.buffer[0] = (byte) value;
173 			this.buffer[1] = (byte) (value >> 8);
174 			this.buffer[2] = (byte) (value >> 16);
175 			this.buffer[3] = (byte) (value >> 24);
176 			this.dataOutputStream.write(this.buffer, 0, 4);
177 		}
178 	}
179 
180 	/***
181 	 * @see java.io.DataOutput#writeChars(java.lang.String)
182 	 */
183 	public void writeChars(final String string) throws IOException
184 	{
185 		if (this.encode == EndianInterface.BIG_ENDIAN)
186 		{
187 			this.dataOutputStream.writeChars(string);
188 		} else
189 		{
190 			int length = string.length();
191 			for (int i = 0; i < length; i++)
192 			{
193 				this.writeChar(string.charAt(i));
194 			}
195 		}
196 	}
197 
198 	/***
199 	 * @see java.io.DataOutput#write(byte[])
200 	 */
201 	public void write(final byte[] buffer) throws IOException
202 	{
203 		this.dataOutputStream.write(this.buffer, 0, this.buffer.length);
204 	}
205 
206 	/***
207 	 * @see java.io.DataOutput#writeDouble(double)
208 	 */
209 	public void writeDouble(final double value) throws IOException
210 	{
211 		if (this.encode == EndianInterface.BIG_ENDIAN)
212 		{
213 			this.dataOutputStream.writeDouble(value);
214 		} else
215 		{
216 			this.writeLong(Double.doubleToLongBits(value));
217 		}
218 	}
219 
220 	/***
221 	 * @return the size of the stream
222 	 */
223 	public final int size()
224 	{
225 		return this.dataOutputStream.size();
226 	}
227 
228 	/***
229 	 * flushes the stream
230 	 * 
231 	 * @throws IOException
232 	 */
233 	public void flush() throws IOException
234 	{
235 		this.dataOutputStream.flush();
236 	}
237 
238 	/***
239 	 * closes the stream
240 	 * 
241 	 * @throws IOException
242 	 */
243 	public void close() throws IOException
244 	{
245 		this.dataOutputStream.close();
246 	}
247 
248 	/***
249 	 * @see nl.javel.gisbeans.io.EndianInterface#setEncode(int)
250 	 */
251 	public void setEncode(final int encode)
252 	{
253 		this.encode = encode;
254 	}
255 
256 	/***
257 	 * @see nl.javel.gisbeans.io.EndianInterface#getEncode()
258 	 */
259 	public int getEncode()
260 	{
261 		return this.encode;
262 	}
263 }