Statistics
| Revision:

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

History | View | Annotate | Download (7.41 KB)

1
/*
2
 * LEDataOutputStream.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 28
18
 *         1.4 1998 November 10 - add new address and phone.
19
 *         1.5 1999 October 8 - use cmp.LEDataStream package name.
20
 *         1.5.x 2000 Febuary 15 - Modifed by James Macgill to include LE/BE switch
21
 * Very similar to DataOutputStream except it writes little-endian instead of
22
 * big-endian binary data.
23
 * We can't extend DataOutputStream directly since it has only final methods.
24
 * This forces us implement LEDataOutputStream with a DataOutputStream object,
25
 * and use wrapper methods.
26
 */
27
package com.iver.cit.gvsig.fmap.drivers.shp;
28

    
29
import java.io.DataOutput;
30
import java.io.DataOutputStream;
31
import java.io.IOException;
32
import java.io.OutputStream;
33

    
34

    
35
/**
36
 * DOCUMENT ME!
37
 *
38
 * @author vcn
39
 */
40
public class LEDataOutputStream implements DataOutput {
41
    private static final String EmbeddedCopyright = "Copyright 1998 Roedy Green, Canadian Mind Products, http://mindprod.com";
42

    
43
    // i n s t a n c e   v a r i a b l e s
44
    protected DataOutputStream d; // to get at high level write methods of DataOutputStream
45
    byte[] w; // work array for composing output
46
    protected boolean littleEndianMode = true;
47

    
48
    /**
49
     * constructor
50
     *
51
     * @param out DOCUMENT ME!
52
     */
53
    public LEDataOutputStream(OutputStream out) {
54
        this.d = new DataOutputStream(out);
55
        w = new byte[8]; // work array for composing output
56
    }
57

    
58
    // L I T T L E   E N D I A N   W R I T E R S
59
    // Little endian methods for multi-byte numeric types.
60
    // Big-endian do fine for single-byte types and strings.
61

    
62
    /**
63
     * like DataOutputStream.writeShort. also acts as a writeUnsignedShort
64
     *
65
     * @param v DOCUMENT ME!
66
     *
67
     * @throws IOException DOCUMENT ME!
68
     */
69
    public final void writeShort(int v) throws IOException {
70
        if (!littleEndianMode) {
71
            d.writeShort(v);
72

    
73
            return;
74
        }
75

    
76
        w[0] = (byte) v;
77
        w[1] = (byte) (v >> 8);
78
        d.write(w, 0, 2);
79
    }
80

    
81
    /**
82
     * like DataOutputStream.writeChar. Note the parm is an int even though
83
     * this as a writeChar
84
     *
85
     * @param v DOCUMENT ME!
86
     *
87
     * @throws IOException DOCUMENT ME!
88
     */
89
    public final void writeChar(int v) throws IOException {
90
        if (!littleEndianMode) {
91
            d.writeChar(v);
92

    
93
            return;
94
        }
95

    
96
        // same code as writeShort
97
        w[0] = (byte) v;
98
        w[1] = (byte) (v >> 8);
99
        d.write(w, 0, 2);
100
    }
101

    
102
    /**
103
     * like DataOutputStream.writeInt.
104
     *
105
     * @param v DOCUMENT ME!
106
     *
107
     * @throws IOException DOCUMENT ME!
108
     */
109
    public final void writeInt(int v) throws IOException {
110
        if (!littleEndianMode) {
111
            d.writeInt(v);
112

    
113
            return;
114
        }
115

    
116
        w[0] = (byte) v;
117
        w[1] = (byte) (v >> 8);
118
        w[2] = (byte) (v >> 16);
119
        w[3] = (byte) (v >> 24);
120
        d.write(w, 0, 4);
121
    }
122

    
123
    /**
124
     * like DataOutputStream.writeLong.
125
     *
126
     * @param v DOCUMENT ME!
127
     *
128
     * @throws IOException DOCUMENT ME!
129
     */
130
    public final void writeLong(long v) throws IOException {
131
        if (!littleEndianMode) {
132
            d.writeLong(v);
133

    
134
            return;
135
        }
136

    
137
        w[0] = (byte) v;
138
        w[1] = (byte) (v >> 8);
139
        w[2] = (byte) (v >> 16);
140
        w[3] = (byte) (v >> 24);
141
        w[4] = (byte) (v >> 32);
142
        w[5] = (byte) (v >> 40);
143
        w[6] = (byte) (v >> 48);
144
        w[7] = (byte) (v >> 56);
145
        d.write(w, 0, 8);
146
    }
147

    
148
    /**
149
     * like DataOutputStream.writeFloat.
150
     *
151
     * @param v DOCUMENT ME!
152
     *
153
     * @throws IOException DOCUMENT ME!
154
     */
155
    public final void writeFloat(float v) throws IOException {
156
        if (!littleEndianMode) {
157
            d.writeFloat(v);
158

    
159
            return;
160
        }
161

    
162
        writeInt(Float.floatToIntBits(v));
163
    }
164

    
165
    /**
166
     * like DataOutputStream.writeDouble.
167
     *
168
     * @param v DOCUMENT ME!
169
     *
170
     * @throws IOException DOCUMENT ME!
171
     */
172
    public final void writeDouble(double v) throws IOException {
173
        if (!littleEndianMode) {
174
            d.writeDouble(v);
175

    
176
            return;
177
        }
178

    
179
        writeLong(Double.doubleToLongBits(v));
180
    }
181

    
182
    /**
183
     * like DataOutputStream.writeChars, flip each char.
184
     *
185
     * @param s DOCUMENT ME!
186
     *
187
     * @throws IOException DOCUMENT ME!
188
     */
189
    public final void writeChars(String s) throws IOException {
190
        if (!littleEndianMode) {
191
            d.writeChars(s);
192

    
193
            return;
194
        }
195

    
196
        int len = s.length();
197

    
198
        for (int i = 0; i < len; i++) {
199
            writeChar(s.charAt(i));
200
        }
201
    }
202
     // end writeChars   
203

    
204
    /**
205
     * DOCUMENT ME!
206
     *
207
     * @param b DOCUMENT ME!
208
     *
209
     * @throws IOException DOCUMENT ME!
210
     */
211
    public final synchronized void write(int b) throws IOException {
212
        d.write(b);
213
    }
214

    
215
    /**
216
     * DOCUMENT ME!
217
     *
218
     * @param b DOCUMENT ME!
219
     * @param off DOCUMENT ME!
220
     * @param len DOCUMENT ME!
221
     *
222
     * @throws IOException DOCUMENT ME!
223
     */
224
    public final synchronized void write(byte[] b, int off, int len)
225
        throws IOException {
226
        d.write(b, off, len);
227
    }
228

    
229
    /**
230
     * DOCUMENT ME!
231
     *
232
     * @throws IOException DOCUMENT ME!
233
     */
234
    public void flush() throws IOException {
235
        d.flush();
236
    }
237

    
238
    /* Only writes one byte */
239
    public final void writeBoolean(boolean v) throws IOException {
240
        d.writeBoolean(v);
241
    }
242

    
243
    /**
244
     * DOCUMENT ME!
245
     *
246
     * @param v DOCUMENT ME!
247
     *
248
     * @throws IOException DOCUMENT ME!
249
     */
250
    public final void writeByte(int v) throws IOException {
251
        d.writeByte(v);
252
    }
253

    
254
    /**
255
     * DOCUMENT ME!
256
     *
257
     * @param s DOCUMENT ME!
258
     *
259
     * @throws IOException DOCUMENT ME!
260
     */
261
    public final void writeBytes(String s) throws IOException {
262
        d.writeBytes(s);
263
    }
264

    
265
    /**
266
     * DOCUMENT ME!
267
     *
268
     * @param str DOCUMENT ME!
269
     *
270
     * @throws IOException DOCUMENT ME!
271
     */
272
    public final void writeUTF(String str) throws IOException {
273
        d.writeUTF(str);
274
    }
275

    
276
    /**
277
     * DOCUMENT ME!
278
     *
279
     * @return DOCUMENT ME!
280
     */
281
    public final int size() {
282
        return d.size();
283
    }
284

    
285
    /**
286
     * DOCUMENT ME!
287
     *
288
     * @param b DOCUMENT ME!
289
     *
290
     * @throws IOException DOCUMENT ME!
291
     */
292
    public final void write(byte[] b) throws IOException {
293
        d.write(b, 0, b.length);
294
    }
295

    
296
    /**
297
     * DOCUMENT ME!
298
     *
299
     * @throws IOException DOCUMENT ME!
300
     */
301
    public final void close() throws IOException {
302
        d.close();
303
    }
304

    
305
    /**
306
     * DOCUMENT ME!
307
     *
308
     * @param flag DOCUMENT ME!
309
     */
310
    public final void setLittleEndianMode(boolean flag) {
311
        littleEndianMode = flag;
312
    }
313

    
314
    /**
315
     * DOCUMENT ME!
316
     *
317
     * @return DOCUMENT ME!
318
     */
319
    public final boolean getLittleEndianMode() {
320
        return littleEndianMode;
321
    }
322

    
323
    /**
324
     * DOCUMENT ME!
325
     *
326
     * @return DOCUMENT ME!
327
     */
328
    public final boolean isLittleEndianMode() {
329
        return littleEndianMode;
330
    }
331
}
332
 // end LEDataOutputStream