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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
#include "stdafx.h"
#include "BasicTypeContainers.h"
#include "DataOutputStream.h"
//Creates a new data output stream to write data to the specified underlying output stream. The counter written is set to zero.
//Parameters:
//out - the underlying output stream, to be saved for later use.
DataOutputStream::DataOutputStream( OutputStream *out ) : stream( out ), written( 0 )
{
}
// 4J Stu - We cannot always delete the stream when we are destroyed, but we want to clear it up as there
// are occasions when we don't have a handle to the child stream elsewhere and want to delete it
void DataOutputStream::deleteChildStream()
{
delete stream;
}
//Writes the specified byte (the low eight bits of the argument b) to the underlying output stream.
//If no exception is thrown, the counter written is incremented by 1.
//Implements the write method of OutputStream.
//Parameters:
//b - the byte to be written.
void DataOutputStream::write(unsigned int b)
{
stream->write( b );
// TODO 4J Stu - Exception handling?
written++;
}
void DataOutputStream::flush()
{
stream->flush();
}
//Writes b.length bytes from the specified byte array to this output stream.
//The general contract for write(b) is that it should have exactly the same effect as the call write(b, 0, b.length).
//Parameters:
//b - the data.
void DataOutputStream::write(byteArray b)
{
write(b, 0, b.length);
}
//Writes len bytes from the specified byte array starting at offset off to the underlying output stream.
//If no exception is thrown, the counter written is incremented by len.
//Parameters:
//b - the data.
//off - the start offset in the data.
//len - the number of bytes to write.
void DataOutputStream::write(byteArray b, unsigned int offset, unsigned int length)
{
stream->write(b, offset, length);
// TODO 4J Stu - Some form of error checking?
written += length;
}
//Closes this output stream and releases any system resources associated with the stream.
//The close method of FilterOutputStream calls its flush method, and then calls the close method of its underlying output stream.
void DataOutputStream::close()
{
stream->close();
}
//Writes out a byte to the underlying output stream as a 1-byte value. If no exception is thrown, the counter written is incremented by 1.
//Parameters:
//v - a byte value to be written.
void DataOutputStream::writeByte(byte a)
{
stream->write( a );
}
//Converts the double argument to a long using the doubleToLongBits method in class Double,
//and then writes that long value to the underlying output stream as an 8-byte quantity,
//high byte first. If no exception is thrown, the counter written is incremented by 8.
//Parameters:
//v - a double value to be written.
void DataOutputStream::writeDouble(double a)
{
int64_t bits = Double::doubleToLongBits( a );
writeLong( bits );
// TODO 4J Stu - Error handling?
written += 8;
}
//Converts the float argument to an int using the floatToIntBits method in class Float,
//and then writes that int value to the underlying output stream as a 4-byte quantity, high byte first.
//If no exception is thrown, the counter written is incremented by 4.
//Parameters:
//v - a float value to be written.
void DataOutputStream::writeFloat(float a)
{
int bits = Float::floatToIntBits( a );
writeInt( bits );
// TODO 4J Stu - Error handling?
written += 4;
}
//Writes an int to the underlying output stream as four bytes, high byte first. If no exception is thrown, the counter written is incremented by 4.
//Parameters:
//v - an int to be written.
void DataOutputStream::writeInt(int a)
{
stream->write( (a >> 24) & 0xff );
stream->write( (a >> 16) & 0xff );
stream->write( (a >> 8) & 0xff );
stream->write( a & 0xff );
// TODO 4J Stu - Error handling?
written += 4;
}
//Writes a long to the underlying output stream as eight bytes, high byte first.
//In no exception is thrown, the counter written is incremented by 8.
//Parameters:
//v - a long to be written.
void DataOutputStream::writeLong(int64_t a)
{
stream->write( (a >> 56) & 0xff );
stream->write( (a >> 48) & 0xff );
stream->write( (a >> 40) & 0xff );
stream->write( (a >> 32) & 0xff );
stream->write( (a >> 24) & 0xff );
stream->write( (a >> 16) & 0xff );
stream->write( (a >> 8) & 0xff );
stream->write( a & 0xff );
// TODO 4J Stu - Error handling?
written += 4;
}
//Writes a short to the underlying output stream as two bytes, high byte first.
//If no exception is thrown, the counter written is incremented by 2.
//Parameters:
//v - a short to be written.
void DataOutputStream::writeShort(short a)
{
stream->write( (a >> 8) & 0xff );
stream->write( a & 0xff );
// TODO 4J Stu - Error handling?
written += 2;
}
void DataOutputStream::writeUnsignedShort(unsigned short a)
{
stream->write( (a >> 8) & 0xff );
stream->write( a & 0xff );
// TODO 4J Stu - Error handling?
written += 2;
}
//Writes a char to the underlying output stream as a 2-byte value, high byte first.
//If no exception is thrown, the counter written is incremented by 2.
//Parameters:
//v - a char value to be written.
void DataOutputStream::writeChar( wchar_t v )
{
stream->write( (v >> 8) & 0xff );
stream->write( v & 0xff );
// TODO 4J Stu - Error handling?
written += 2;
}
//Writes a string to the underlying output stream as a sequence of characters.
//Each character is written to the data output stream as if by the writeChar method.
//If no exception is thrown, the counter written is incremented by twice the length of s.
//Parameters:
//s - a String value to be written.
void DataOutputStream::writeChars(const wstring& str)
{
for( unsigned int i = 0; i < str.length(); i++)
{
writeChar( str.at( i ) );
// TODO 4J Stu - Error handling?
}
// Incrementing handled by the writeChar function
}
//Writes a boolean to the underlying output stream as a 1-byte value.
//The value true is written out as the value (byte)1; the value false is written out as the value (byte)0.
//If no exception is thrown, the counter written is incremented by 1.
//Parameters:
//v - a boolean value to be written.
void DataOutputStream::writeBoolean(bool b)
{
stream->write( b ? (byte)1 : (byte)0 );
// TODO 4J Stu - Error handling?
written += 1;
}
//Writes a string to the underlying output stream using modified UTF-8 encoding in a machine-independent manner.
//First, two bytes are written to the output stream as if by the writeShort method giving the number of bytes to follow.
//This value is the number of bytes actually written out, not the length of the string. Following the length,
//each character of the string is output, in sequence, using the modified UTF-8 encoding for the character.
//If no exception is thrown, the counter written is incremented by the total number of bytes written to the output stream.
//This will be at least two plus the length of str, and at most two plus thrice the length of str.
//Parameters:
//str - a string to be written.
void DataOutputStream::writeUTF(const wstring& str)
{
int strlen = (int)str.length();
int utflen = 0;
int c, count = 0;
/* use charAt instead of copying String to char array */
for (int i = 0; i < strlen; i++)
{
c = str.at(i);
if ((c >= 0x0001) && (c <= 0x007F))
{
utflen++;
}
else if (c > 0x07FF)
{
utflen += 3;
}
else
{
utflen += 2;
}
}
//if (utflen > 65535)
// throw new UTFDataFormatException(
// "encoded string too long: " + utflen + " bytes");
byteArray bytearr(utflen+2);
bytearr[count++] = (byte) ((utflen >> 8) & 0xFF);
bytearr[count++] = (byte) ((utflen >> 0) & 0xFF);
int i=0;
for (i=0; i<strlen; i++)
{
c = str.at(i);
if (!((c >= 0x0001) && (c <= 0x007F))) break;
bytearr[count++] = (byte) c;
}
for (;i < strlen; i++)
{
c = str.at(i);
if ((c >= 0x0001) && (c <= 0x007F))
{
bytearr[count++] = (byte) c;
}
else if (c > 0x07FF)
{
bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F));
bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F));
bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
}
else
{
bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F));
bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F));
}
}
write(bytearr, 0, utflen+2);
delete[] bytearr.data;
}
// 4J Added
void DataOutputStream::writePlayerUID(PlayerUID player)
{
#if defined(__PS3__) || defined(__ORBIS__) || defined (__PSVITA__)
for(int idPos=0;idPos<sizeof(PlayerUID); idPos++)
writeByte(((char*)&player)[idPos]);
#elif defined(_DURANGO)
writeUTF(player.toString());
#else
writeLong(player);
#endif // PS3
}
|