1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
// java.io.DataOutputStream
// An implementation of the Java Language Specification section 22.21
// Written by Charles Briscoe-Smith; refer to the file LEGAL for details.
package java.io;
public class DataOutputStream extends FilterOutputStream implements DataOutput {
protected int written;
public DataOutputStream(OutputStream o) { super(o); }
public void write(int b) throws IOException
{
out.write(b & 0xff);
written++;
}
public void write(byte[] b, int off, int len)
throws IOException, NullPointerException,
IndexOutOfBoundsException
{
out.write(b, off, len);
written+=len;
}
public void flush() throws IOException
{
out.flush();
}
public final void writeBoolean(boolean v) throws IOException
{
write(v?1:0);
}
public final void writeByte(int v) throws IOException
{
write(v);
}
public final void writeShort(int v) throws IOException
{
write(v>>8);
write(v);
}
public final void writeChar(int v) throws IOException
{
writeShort(v);
}
public final void writeInt(int v) throws IOException
{
write(v>>24);
write(v>>16);
write(v>>8);
write(v);
}
public final void writeLong(long v) throws IOException
{
write((int)v>>56);
write((int)v>>48);
write((int)v>>40);
write((int)v>>32);
write((int)v>>24);
write((int)v>>16);
write((int)v>>8);
write((int)v);
}
public final void writeFloat(float v) throws IOException
{
// FIXME
throw new InternalError("Can't output floats yet.");
// writeInt(Float.floatToIntBits(v));
}
public final void writeDouble(double v) throws IOException
{
// FIXME
throw new InternalError("Can't output doubles yet.");
// writeLong(Double.doubleToLongBits(v));
}
public final void writeBytes(String s)
throws IOException, NullPointerException,
IndexOutOfBoundsException
{
for (int i=0; i<s.length(); i++) {
write(s.charAt(i));
}
}
public final void writeChars(String s)
throws IOException, NullPointerException,
IndexOutOfBoundsException
{
for (int i=0; i<s.length(); i++) {
writeChar(s.charAt(i));
}
}
public final void writeUTF(String s)
throws IOException, NullPointerException,
IndexOutOfBoundsException
{
int toWrite=0;
char[] str=new char[s.length()];
s.getChars(0, str.length, str, 0);
for (int i=0; i<str.length; i++) {
if (str[i]>0 && str[i]<0x80) toWrite++;
else if (str[i]<0x800) toWrite+=2;
else toWrite+=3;
}
if (toWrite>65535) throw new UTFDataFormatException();
writeShort(toWrite);
for (int i=0; i<str.length; i++) {
if (str[i]>0 && str[i]<0x80)
write(str[i]);
else if (str[i]<0x800) {
write(0xc0 | (0x1f & (str[i]>>6)));
write(0x80 | (0x3f & str[i]));
} else {
write(0xc0 | (0x0f & (str[i]>>12)));
write(0x80 | (0x3f & (str[i]>>6)));
write(0x80 | (0x3f & str[i]));
}
}
}
public final int size()
{
return written;
}
}
|