1 /***
2 * LETest.java
3 *
4 * copyright (c) 1998-2002 Roedy Green, Canadian Mind Products
5 * #327 - 964 Heywood Avenue
6 * Victoria, BC Canada V8V 2Y5
7 * tel: (250) 361-9093
8 * mailto:roedy@mindprod.com
9 * http://mindprod.com
10 *
11 * Version 1.0 1998 January 6
12 * 1.1 1998 January 7 - officially implements DataInput
13 * 1.2 1998 January 9 - add LERandomAccessFile
14 * 1.3 1998 August 27
15 * 1.4 1998 November 10 - add new address and phone.
16 * 1.5 1999 October 8 - use com.mindprod.ledatastream package name.
17 *
18 * Tests Little Endian LEDataInputStream and LEDataOutputStream
19 * and demonstrates the use of its methods.
20 *
21 * Output should look like this:
22 *
23 * 44
24 * -1
25 * a
26 * -249
27 * -123456789012
28 * -649
29 * -749
30 * true
31 * 3.14
32 * 4.14
33 * echidna
34 * kangaroo
35 * dingo
36 *
37 * Then repeated.
38 *
39 */
40 package com.mindprod.ledatastream;
41 import java.io.FileInputStream;
42 import java.io.FileNotFoundException;
43 import java.io.FileOutputStream;
44 import java.io.IOException;
45
46 public class LETest
47 {
48
49 public static void main (String[] args)
50 {
51
52 // Write little-endian binary data into a sequential file
53
54 // O P E N
55 FileOutputStream fos;
56 try
57 {
58 fos = new FileOutputStream("C:/temp/temp.dat", false /* append */);
59 }
60 catch ( IOException e )
61 {
62 System.out.println("Unexpected IOException opening LEDataOutputStream");
63 return;
64 }
65
66 LEDataOutputStream ledos = new LEDataOutputStream(fos);
67
68 // W R I T E
69 try
70 {
71 ledos.writeByte((byte)44);
72 ledos.writeByte((byte)0xff);
73 ledos.writeChar('a');
74 ledos.writeInt(-249);
75 ledos.writeLong(-123456789012L);
76 ledos.writeShort((short)-649);
77 ledos.writeShort((short)-749);
78 ledos.writeBoolean(true);
79 ledos.writeDouble(3.14D);
80 ledos.writeFloat(4.14F);
81 ledos.writeUTF("echidna");
82 ledos.writeBytes("kangaroo" /* string -> LSB 8-bit */);
83 ledos.writeChars("dingo" /* string 16-bit Unicode */);
84 }
85 catch ( IOException e )
86 {
87 System.out.println("Unexpected IOException writing LEDataOutputStream");
88 return;
89 }
90
91 // C L O S E
92 try
93 {
94 ledos.close();
95 }
96 catch ( IOException e )
97 {
98 System.out.println("Unexpected IOException closing LEDataOutputStream");
99 return;
100 }
101
102 // Read little-endian binary data from a sequential file
103 // import java.io.*;
104
105 // O P E N
106 FileInputStream fis;
107 try
108 {
109 fis = new FileInputStream("C:/temp/temp.dat");
110 }
111 catch ( FileNotFoundException e )
112 {
113 System.out.println("Unexpected IOException opening LEDataInputStream");
114 return;
115 }
116 LEDataInputStream ledis = new LEDataInputStream(fis);
117
118 // R E A D
119 try
120 {
121 byte b = ledis.readByte();
122 System.out.println(b);
123 byte ub = (byte) ledis.readUnsignedByte();
124 System.out.println(ub);
125 char c = ledis.readChar();
126 System.out.println(c);
127 int j = ledis.readInt();
128 System.out.println(j);
129 long l = ledis.readLong();
130 System.out.println(l);
131 short s = ledis.readShort();
132 System.out.println(s);
133 short us = (short) ledis.readUnsignedShort();
134 System.out.println(us);
135 boolean q = ledis.readBoolean();
136 System.out.println(q);
137 double d = ledis.readDouble();
138 System.out.println(d);
139 float f = ledis.readFloat();
140 System.out.println(f);
141 String u = ledis.readUTF();
142 System.out.println(u);
143 byte[] ba = new byte[8];
144 ledis.readFully(ba, 0 /* offset in ba */, ba.length /* bytes to read */);
145 System.out.println(new String(ba));
146 /* there is no readChars method */
147 c = ledis.readChar();
148 System.out.print(c);
149 c = ledis.readChar();
150 System.out.print(c);
151 c = ledis.readChar();
152 System.out.print(c);
153 c = ledis.readChar();
154 System.out.print(c);
155 c = ledis.readChar();
156 System.out.println(c);
157
158 }
159 catch ( IOException e )
160 {
161 System.out.println("Unexpected IOException reading LEDataInputStream");
162 return;
163 }
164
165 // C L O S E
166 try
167 {
168 ledis.close();
169 }
170 catch ( IOException e )
171 {
172 System.out.println("Unexpected IOException closing LEDataInputStream");
173 return;
174 }
175 // Write little endian data to a random access files
176
177 // O P E N
178 LERandomAccessFile leraf;
179 try
180 {
181 leraf = new LERandomAccessFile("C:/temp/rand.dat", "rw" /* read/write */);
182 }
183 catch ( IOException e )
184 {
185 System.out.println("Unexpected IOException creating LERandomAccessFile");
186
187 return;
188 }
189
190 try
191 {
192 // W R I T E
193 leraf.seek(0 /* byte offset in file*/);
194 leraf.writeByte((byte)44);
195 leraf.writeByte((byte)0xff);
196 leraf.writeChar('a');
197 leraf.writeInt(-249);
198 leraf.writeLong(-123456789012L);
199 leraf.writeShort((short)-649);
200 leraf.writeShort((short)-749);
201 leraf.writeBoolean(true);
202 leraf.writeDouble(3.14D);
203 leraf.writeFloat(4.14F);
204 leraf.writeUTF("echidna");
205 leraf.writeBytes("kangaroo" /* string -> LSB 8-bit */);
206 leraf.writeChars("dingo" /* string 16-bit Unicode */);
207 leraf.seek(0 /* byte offset in file*/);
208
209 }
210 catch ( IOException e )
211 {
212 System.out.println("Unexpected IOException writing LERandomAccessFile");
213 return;
214 }
215
216 try
217 {
218 // R E A D
219 byte b = leraf.readByte();
220 System.out.println(b);
221 byte ub = (byte) leraf.readUnsignedByte();
222 System.out.println(ub);
223 char c = leraf.readChar();
224 System.out.println(c);
225 int j = leraf.readInt();
226 System.out.println(j);
227 long l = leraf.readLong();
228 System.out.println(l);
229 short s = leraf.readShort();
230 System.out.println(s);
231 short us = (short) leraf.readUnsignedShort();
232 System.out.println(us);
233 boolean q = leraf.readBoolean();
234 System.out.println(q);
235 double d = leraf.readDouble();
236 System.out.println(d);
237 float f = leraf.readFloat();
238 System.out.println(f);
239 String u = leraf.readUTF();
240 System.out.println(u);
241 byte[] ba = new byte[8];
242 leraf.readFully(ba, 0 /* offset in ba */, ba.length /* bytes to read */);
243 System.out.println(new String(ba));
244 /* there is no readChars method */
245 c = leraf.readChar();
246 System.out.print(c);
247 c = leraf.readChar();
248 System.out.print(c);
249 c = leraf.readChar();
250 System.out.print(c);
251 c = leraf.readChar();
252 System.out.print(c);
253 c = leraf.readChar();
254 System.out.println(c);
255
256 }
257 catch ( IOException e )
258 {
259 System.out.println("Unexpected IOException reading LERandomAccessFile");
260 return;
261 }
262
263 // C L O S E
264 try
265 {
266 leraf.close();
267 }
268 catch ( IOException e )
269 {
270 System.out.println("Unexpected IOException closing LERandomAccessFile");
271 return;
272 }
273
274 } // end main
275 } // end class LETest
This page was automatically generated by Maven