Blame |
Last modification |
View Log
| RSS feed
package com.gebauz.bauzoid.file;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import com.gebauz.bauzoid.math.Matrix4;
import com.gebauz.bauzoid.math.Vector2;
import com.gebauz.bauzoid.math.Vector3;
import com.gebauz.bauzoid.math.Vector4;
/** File wrier that supports interpreting various data types.
*
* No endian-ness handling - uses Standard Java endianness.
*/
public class FileWriter
{
private DataOutputStream mOS =
null;
/** Constructor. Takes an InputStream and claims responsibility for closing it. */
public FileWriter(OutputStream os
)
{
mOS =
new DataOutputStream(os
);
}
/** Close the InputStream. */
public void close
() throws IOException
{
if (mOS
!=
null)
{
mOS.
close();
mOS =
null;
}
}
/** writes an int for the length, then interprets the following length bytes as the String. */
public final void writeString
(String s
) throws IOException
{
writeInt
(s.
length());
writeString
(s
);
}
/** write a 8-bit C-style bool value. */
public final void writeBool
(boolean b
) throws IOException
{
writeByte
((byte)(b
? 1 :
0));
}
/** writes 3 float values and returns a Vector2. */
public final void writeVector2
(Vector2 v
) throws IOException
{
writeFloat
(v.
x);
writeFloat
(v.
y);
}
/** writes 3 float values and returns a Vector3. */
public final void writeVector3
(Vector3 v
) throws IOException
{
writeFloat
(v.
x);
writeFloat
(v.
y);
writeFloat
(v.
z);
}
/** writes 4 float values and returns a Vector4. */
public final void writeVector4
(Vector4 v
) throws IOException
{
writeFloat
(v.
x);
writeFloat
(v.
y);
writeFloat
(v.
z);
writeFloat
(v.
w);
}
/** write a 4x4 matrix (16 floats). */
public final void writeMatrix4
(Matrix4 m
) throws IOException
{
float[] matrix = m.
toGLMatrix();
for (int i =
0; i
< 16; i++
)
{
writeFloat
(matrix
[i
]);
}
}
/** write a bitmap. */
/* public final Bitmap writeBitmap() throws IOException
{
return BitmapFactory.decodeStream(mInputStream);
}*/
/** Get the internal input stream. */
public final OutputStream getOutputStream
()
{
return mOS
;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public final void write
(byte[] buffer,
int offset,
int length
) throws IOException
{
mOS.
write(buffer, offset, length
);
}
/** Equivalent to write(buffer, 0, buffer.length).*/
public final void write
(byte[] buffer
) throws IOException
{
mOS.
write(buffer
);
}
/** writes a Java boolean (32-bit single, 8-bit in arrays). */
public final void writeBoolean
(boolean b
) throws IOException
{
mOS.
writeBoolean(b
);
}
/** writes an 8-bit byte. */
public final void writeByte
(byte b
) throws IOException
{
mOS.
writeByte(b
);
}
/** writes a big-endian 16-bit character value. */
public final void writeChar
(char c
) throws IOException
{
mOS.
writeChar(c
);
}
/** writes a big-endian 64-bit double value. */
public final void writeDouble
(double d
) throws IOException
{
mOS.
writeDouble(d
);
}
/** writes a big-endian 32-bit float value. */
public final void writeFloat
(float f
) throws IOException
{
mOS.
writeFloat(f
);
}
/** writes a big-endian 32-bit integer value. */
public final void writeInt
(int n
) throws IOException
{
mOS.
writeInt(n
);
}
/** Returns a string containing the next line of text available from this stream. */
public final void writeLine
(String s
) throws IOException
{
writeString
(s +
"\n");
}
/** writes a big-endian 64-bit long value. */
public final void writeLong
(long l
) throws IOException
{
mOS.
writeLong(l
);
}
/** writes a big-endian 16-bit short value. */
public final void writeShort
(short s
) throws IOException
{
mOS.
writeShort(s
);
}
/** writes a string encoded with modified UTF-8. */
public final void writeUTF
(String s
) throws IOException
{
mOS.
writeUTF(s
);
}
/** writes an unsigned 8-bit byte value and returns it as an int. */
/* public final void writeUnsignedByte(int ub) throws IOException
{
mOS.writeUnsignedByte(ub);
}*/
/** writes a big-endian 16-bit unsigned short value and returns it as an int. */
/* public final void writeUnsignedShort(int us) throws IOException
{
mOS.writeUnsignedShort(us);
}*/
}