Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1051 chris 1
package com.gebauz.bauzoid.file;
2
 
3
import java.io.DataOutputStream;
4
import java.io.IOException;
5
import java.io.OutputStream;
6
 
7
import com.gebauz.bauzoid.math.Matrix4;
8
import com.gebauz.bauzoid.math.Vector2;
9
import com.gebauz.bauzoid.math.Vector3;
10
import com.gebauz.bauzoid.math.Vector4;
11
 
12
/** File wrier that supports interpreting various data types.
13
 *
14
 *  No endian-ness handling - uses Standard Java endianness.
15
 */
16
public class FileWriter
17
{
18
        private DataOutputStream mOS = null;
19
 
20
        /** Constructor. Takes an InputStream and claims responsibility for closing it. */
21
        public FileWriter(OutputStream os)
22
        {
23
                mOS = new DataOutputStream(os);
24
        }
25
 
26
        /** Close the InputStream. */
27
        public void close() throws IOException
28
        {
29
                if (mOS != null)
30
                {
31
                        mOS.close();
32
                        mOS = null;
33
                }
34
        }
35
 
36
        /** writes an int for the length, then interprets the following length bytes as the String. */
37
        public final void writeString(String s) throws IOException
38
        {
39
                writeInt(s.length());
40
                writeString(s);
41
        }
42
 
43
        /** write a 8-bit C-style bool value. */
44
        public final void writeBool(boolean b) throws IOException
45
        {
46
                writeByte((byte)(b ? 1 : 0));
47
        }
48
 
49
        /** writes 3 float values and returns a Vector2. */
50
        public final void writeVector2(Vector2 v) throws IOException
51
        {
52
                writeFloat(v.x);
53
                writeFloat(v.y);
54
        }
55
 
56
        /** writes 3 float values and returns a Vector3. */
57
        public final void writeVector3(Vector3 v) throws IOException
58
        {
59
                writeFloat(v.x);
60
                writeFloat(v.y);
61
                writeFloat(v.z);
62
        }
63
 
64
        /** writes 4 float values and returns a Vector4. */
65
        public final void writeVector4(Vector4 v) throws IOException
66
        {
67
                writeFloat(v.x);
68
                writeFloat(v.y);
69
                writeFloat(v.z);
70
                writeFloat(v.w);
71
        }
72
 
73
        /** write a 4x4 matrix (16 floats). */
74
        public final void writeMatrix4(Matrix4 m) throws IOException
75
        {
76
                float[] matrix = m.toGLMatrix();
77
                for (int i = 0; i < 16; i++)
78
                {
79
                        writeFloat(matrix[i]);
80
                }
81
        }
82
 
83
        /** write a bitmap. */
84
/*      public final Bitmap writeBitmap() throws IOException
85
        {
86
                return BitmapFactory.decodeStream(mInputStream);
87
        }*/
88
 
89
        /** Get the internal input stream. */
90
        public final OutputStream getOutputStream()
91
        {
92
                return mOS;
93
        }
94
 
95
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
96
 
97
        public final void write(byte[] buffer, int offset, int length) throws IOException
98
        {
99
                mOS.write(buffer, offset, length);             
100
        }
101
 
102
 
103
        /** Equivalent to write(buffer, 0, buffer.length).*/
104
        public final void write(byte[] buffer) throws IOException
105
        {
106
                mOS.write(buffer);
107
        }
108
 
109
        /** writes a Java boolean (32-bit single, 8-bit in arrays). */
110
        public final void writeBoolean(boolean b) throws IOException
111
        {
112
                mOS.writeBoolean(b);
113
        }
114
 
115
        /** writes an 8-bit byte. */
116
        public final void writeByte(byte b) throws IOException
117
        {
118
                mOS.writeByte(b);
119
        }
120
 
121
        /** writes a big-endian 16-bit character value. */
122
        public final void writeChar(char c) throws IOException
123
        {
124
                mOS.writeChar(c);
125
        }
126
 
127
        /** writes a big-endian 64-bit double value. */
128
        public final void writeDouble(double d) throws IOException
129
        {
130
                mOS.writeDouble(d);
131
        }
132
 
133
        /** writes a big-endian 32-bit float value. */
134
        public final void writeFloat(float f) throws IOException
135
        {
136
                mOS.writeFloat(f);
137
        }
138
 
139
        /** writes a big-endian 32-bit integer value. */
140
        public final void writeInt(int n) throws IOException
141
        {
142
                mOS.writeInt(n);
143
        }
144
 
145
        /** Returns a string containing the next line of text available from this stream. */
146
        public final void writeLine(String s) throws IOException
147
        {
148
                writeString(s + "\n");
149
        }
150
 
151
        /** writes a big-endian 64-bit long value. */
152
        public final void writeLong(long l) throws IOException
153
        {
154
                mOS.writeLong(l);
155
        }
156
 
157
        /** writes a big-endian 16-bit short value. */
158
        public final void writeShort(short s) throws IOException
159
        {
160
                mOS.writeShort(s);
161
        }
162
 
163
        /** writes a string encoded with modified UTF-8. */
164
        public final void writeUTF(String s) throws IOException
165
        {
166
                mOS.writeUTF(s);
167
        }
168
 
169
        /** writes an unsigned 8-bit byte value and returns it as an int. */
170
/*      public final void writeUnsignedByte(int ub) throws IOException
171
        {
172
                mOS.writeUnsignedByte(ub);
173
        }*/
174
 
175
        /** writes a big-endian 16-bit unsigned short value and returns it as an int. */
176
/*      public final void writeUnsignedShort(int us) throws IOException
177
        {
178
                mOS.writeUnsignedShort(us);
179
        }*/
180
 
181
}
182
 
183