1 /*
2 * LEDataOutputStream.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 *
12 * Version 1.0 1998 January 6
13 * 1.1 1998 January 7 - officially implements DataInput
14 * 1.2 1998 January 9 - add LERandomAccessFile
15 * 1.3 1998 August 28
16 * 1.4 1998 November 10 - add new address and phone.
17 * 1.5 1999 October 8 - use com.mindprod.ledatastream package name.
18 * Very similar to DataOutputStream except it writes little-endian instead of
19 * big-endian binary data.
20 * We can't extend DataOutputStream directly since it has only final methods.
21 * This forces us implement LEDataOutputStream with a DataOutputStream object,
22 * and use wrapper methods.
23 */
24
25 package com.mindprod.ledatastream;
26 import java.io.DataOutput;
27 import java.io.DataOutputStream;
28 import java.io.IOException;
29 import java.io.OutputStream;
30
31 public
32 class LEDataOutputStream implements DataOutput
33 {
34
35 private static final String EmbeddedCopyright =
36 "copyright (c) 1998-2002 Roedy Green, Canadian Mind Products, http://mindprod.com";
37 /***
38 * constructor
39 */
40 public LEDataOutputStream(OutputStream out)
41 {
42 this.d = new DataOutputStream(out);
43 w = new byte[8]; // work array for composing output
44 }
45
46 // L I T T L E E N D I A N W R I T E R S
47 // Little endian methods for multi-byte numeric types.
48 // Big-endian do fine for single-byte types and strings.
49
50 /***
51 * like DataOutputStream.writeShort.
52 * also acts as a writeUnsignedShort
53 */
54 public final void writeShort(int v) throws IOException
55 {
56 w[0] = (byte) v;
57 w[1] = (byte)(v >> 8);
58 d.write(w, 0, 2);
59 }
60
61 /***
62 * like DataOutputStream.writeChar.
63 * Note the parm is an int even though this as a writeChar
64 */
65 public final void writeChar(int v) throws IOException
66 {
67 // same code as writeShort
68 w[0] = (byte) v;
69 w[1] = (byte)(v >> 8);
70 d.write(w, 0, 2);
71 }
72
73 /***
74 * like DataOutputStream.writeInt.
75 */
76 public final void writeInt(int v) throws IOException
77 {
78 w[0] = (byte) v;
79 w[1] = (byte)(v >> 8);
80 w[2] = (byte)(v >> 16);
81 w[3] = (byte)(v >> 24);
82 d.write(w, 0, 4);
83 }
84
85 /***
86 * like DataOutputStream.writeLong.
87 */
88 public final void writeLong(long v) throws IOException
89 {
90 w[0] = (byte) v;
91 w[1] = (byte)(v >> 8);
92 w[2] = (byte)(v >> 16);
93 w[3] = (byte)(v >> 24);
94 w[4] = (byte)(v >> 32);
95 w[5] = (byte)(v >> 40);
96 w[6] = (byte)(v >> 48);
97 w[7] = (byte)(v >> 56);
98 d.write(w, 0, 8);
99 }
100
101 /***
102 * like DataOutputStream.writeFloat.
103 */
104 public final void writeFloat(float v) throws IOException
105 {
106 writeInt(Float.floatToIntBits(v));
107 }
108
109 /***
110 * like DataOutputStream.writeDouble.
111 */
112 public final void writeDouble(double v) throws IOException
113 {
114 writeLong(Double.doubleToLongBits(v));
115 }
116
117 /***
118 * like DataOutputStream.writeChars, flip each char.
119 */
120 public final void writeChars(String s) throws IOException
121 {
122 int len = s.length();
123 for ( int i = 0 ; i < len ; i++ )
124 {
125 writeChar(s.charAt(i));
126 }
127 } // end writeChars
128
129 // p u r e l y w r a p p e r m e t h o d s
130 // We cannot inherit since DataOutputStream is final.
131
132 /* This method writes only one byte, even though it says int */
133 public final synchronized void write(int b) throws IOException
134 {
135 d.write(b);
136 }
137
138 public final synchronized void write(byte b[], int off, int len)
139 throws IOException
140 {
141 d.write(b, off, len);
142 }
143
144 public void flush() throws IOException
145 {
146 d.flush();
147 }
148
149 /* Only writes one byte */
150 public final void writeBoolean(boolean v) throws IOException
151 {
152 d.writeBoolean(v);
153 }
154
155 public final void writeByte(int v) throws IOException
156 {
157 d.writeByte(v);
158 }
159
160 public final void writeBytes(String s) throws IOException
161 {
162 d.writeBytes(s);
163 }
164
165 public final void writeUTF(String str) throws IOException
166 {
167 d.writeUTF(str);
168 }
169
170 public final int size()
171 {
172 return d.size();
173 }
174
175 public final void write(byte b[]) throws IOException
176 {
177 d.write(b, 0, b.length);
178 }
179
180 public final void close() throws IOException
181 {
182 d.close();
183 }
184
185 // i n s t a n c e v a r i a b l e s
186
187 protected DataOutputStream d; // to get at high level write methods of DataOutputStream
188 byte w[]; // work array for composing output
189
190 } // end LEDataOutputStream
This page was automatically generated by Maven