Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / shp / LETest.java @ 213

History | View | Annotate | Download (6.39 KB)

1
/*
2
  * LETest.java
3
  *
4
  * Copyright (c) 1998
5
  * Roedy Green
6
  * Canadian Mind Products
7
  * #208 - 525 Ninth Street
8
  * New Westminster, BC Canada V3M 5T9
9
  * tel: (604) 777-1804
10
  * mailto:roedy@mindprod.com
11
  * http://mindprod.com
12
  *
13
  * Copyright (c) 1998 Roedy Green of Canadian Mind Products
14
  * Version 1.0 1998 January 6
15
  *         1.1 1998 January 7 - officially implements DataInput
16
  *         1.2 1998 January 9 - add LERandomAccessFile
17
  *         1.3 1998 August 27
18
  *         1.4 1998 November 10 - add new address and phone.
19
  *         1.5 1999 October 8 - use cmp.LEDataStream package name.
20
  *
21
  * Tests Little Endian LEDataInputStream and LEDataOutputStream
22
  * and demonstrates the use of its methods.
23
  *
24
  * Output should look like this:
25
  *
26
  * 44
27
  * -1
28
  * a
29
  * -249
30
  * -123456789012
31
  * -649
32
  * -749
33
  * true
34
  * 3.14
35
  * 4.14
36
  * echidna
37
  * kangaroo
38
  * dingo
39
  *
40
  * Then repeated.
41
  *
42
  */
43
package com.iver.cit.gvsig.fmap.drivers.shp;
44
import java.io.FileInputStream;
45
import java.io.FileNotFoundException;
46
import java.io.FileOutputStream;
47
import java.io.IOException;
48

    
49
public class LETest
50
  {
51

    
52
          public static void main (String[] args)
53
  {
54

    
55

    
56
        // Write little-endian binary data into a sequential file
57

    
58
        // O P E N
59
        FileOutputStream fos;
60
        try
61
          {
62
          fos = new FileOutputStream("C:/temp/temp.dat", false /* append */);
63
          } catch ( IOException e )
64
          {
65
          System.out.println("Unexpected IOException opening LEDataOutputStream");
66
          return;
67
          }
68

    
69
        LEDataOutputStream ledos = new LEDataOutputStream(fos);
70

    
71
        // W R I T E
72
        try
73
          {
74
          ledos.writeByte((byte)44);
75
          ledos.writeByte((byte)0xff);
76
          ledos.writeChar('a');
77
          ledos.writeInt(-249);
78
          ledos.writeLong(-123456789012L);
79
          ledos.writeShort((short)-649);
80
          ledos.writeShort((short)-749);
81
          ledos.writeBoolean(true);
82
          ledos.writeDouble(3.14D);
83
          ledos.writeFloat(4.14F);
84
          ledos.writeUTF("echidna");
85
          ledos.writeBytes("kangaroo" /* string -> LSB 8-bit */);
86
          ledos.writeChars("dingo" /* string 16-bit Unicode */);
87
          } catch ( IOException e )
88
          {
89
          System.out.println("Unexpected IOException writing LEDataOutputStream");
90
          return;
91
          }
92

    
93
        // C L O S E
94
        try
95
          {
96
          ledos.close();
97
          } catch ( IOException e )
98
          {
99
          System.out.println("Unexpected IOException closing LEDataOutputStream");
100
          return;
101
          }
102

    
103

    
104
        // Read little-endian binary data from a sequential file
105
        // import java.io.*;
106

    
107
        // O P E N
108
        FileInputStream fis;
109
        try
110
          {
111
          fis = new FileInputStream("C:/temp/temp.dat");
112
          } catch ( FileNotFoundException e )
113
          {
114
          System.out.println("Unexpected IOException opening LEDataInputStream");
115
          return;
116
          }
117
        LEDataInputStream ledis = new LEDataInputStream(fis);
118

    
119
        // R E A D
120
        try
121
          {
122
          byte b = ledis.readByte();
123
          System.out.println(b);
124
          byte ub = (byte) ledis.readUnsignedByte();
125
          System.out.println(ub);
126
          char c = ledis.readChar();
127
          System.out.println(c);
128
          int j = ledis.readInt();
129
          System.out.println(j);
130
          long l = ledis.readLong();
131
          System.out.println(l);
132
          short s = ledis.readShort();
133
          System.out.println(s);
134
          short us = (short) ledis.readUnsignedShort();
135
          System.out.println(us);
136
          boolean q = ledis.readBoolean();
137
          System.out.println(q);
138
          double d = ledis.readDouble();
139
          System.out.println(d);
140
          float f = ledis.readFloat();
141
          System.out.println(f);
142
          String u = ledis.readUTF();
143
          System.out.println(u);
144
          byte[] ba = new byte[8];
145
          ledis.readFully(ba, 0 /* offset in ba */, ba.length /* bytes to read */);
146
          System.out.println(new String(ba));
147
          /* there is no readChars method */
148
          c = ledis.readChar();
149
          System.out.print(c);
150
          c = ledis.readChar();
151
          System.out.print(c);
152
          c = ledis.readChar();
153
          System.out.print(c);
154
          c = ledis.readChar();
155
          System.out.print(c);
156
          c = ledis.readChar();
157
          System.out.println(c);
158

    
159

    
160
          } catch ( IOException e )
161
          {
162
          System.out.println("Unexpected IOException reading LEDataInputStream");
163
          return;
164
          }
165

    
166
        // C L O S E
167
        try
168
          {
169
          ledis.close();
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
          } catch ( IOException e )
183
          {
184
          System.out.println("Unexpected IOException creating LERandomAccessFile");
185

    
186
          return;
187
          }
188

    
189
        try
190
          {
191
          // W R I T E
192
          leraf.seek(0 /* byte offset in file*/);
193
          leraf.writeByte((byte)44);
194
          leraf.writeByte((byte)0xff);
195
          leraf.writeChar('a');
196
          leraf.writeInt(-249);
197
          leraf.writeLong(-123456789012L);
198
          leraf.writeShort((short)-649);
199
          leraf.writeShort((short)-749);
200
          leraf.writeBoolean(true);
201
          leraf.writeDouble(3.14D);
202
          leraf.writeFloat(4.14F);
203
          leraf.writeUTF("echidna");
204
          leraf.writeBytes("kangaroo" /* string -> LSB 8-bit */);
205
          leraf.writeChars("dingo" /* string 16-bit Unicode */);
206
          leraf.seek(0 /* byte offset in file*/);
207

    
208
          } catch ( IOException e )
209
          {
210
          System.out.println("Unexpected IOException writing LERandomAccessFile");
211
          return;
212
          }
213

    
214
        try
215
          {
216
          // R E A D
217
          byte b = leraf.readByte();
218
          System.out.println(b);
219
          byte ub = (byte) leraf.readUnsignedByte();
220
          System.out.println(ub);
221
          char c = leraf.readChar();
222
          System.out.println(c);
223
          int j = leraf.readInt();
224
          System.out.println(j);
225
          long l = leraf.readLong();
226
          System.out.println(l);
227
          short s = leraf.readShort();
228
          System.out.println(s);
229
          short us = (short) leraf.readUnsignedShort();
230
          System.out.println(us);
231
          boolean q = leraf.readBoolean();
232
          System.out.println(q);
233
          double d = leraf.readDouble();
234
          System.out.println(d);
235
          float f = leraf.readFloat();
236
          System.out.println(f);
237
          String u = leraf.readUTF();
238
          System.out.println(u);
239
          byte[] ba = new byte[8];
240
          leraf.readFully(ba, 0 /* offset in ba */, ba.length /* bytes to read */);
241
          System.out.println(new String(ba));
242
          /* there is no readChars method */
243
          c = leraf.readChar();
244
          System.out.print(c);
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.println(c);
253

    
254
          } catch ( IOException e )
255
          {
256
          System.out.println("Unexpected IOException reading LERandomAccessFile");
257
          return;
258
          }
259

    
260
        // C L O S E
261
        try
262
          {
263
          leraf.close();
264
          } catch ( IOException e )
265
          {
266
          System.out.println("Unexpected IOException closing LERandomAccessFile");
267
          return;
268
          }
269

    
270
  } // end main    
271
  } // end class LETest