-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
XdrOutputStream.java
417 lines (384 loc) · 13.2 KB
/
XdrOutputStream.java
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
* Firebird Open Source J2ee connector - jdbc driver
*
* Distributable under LGPL license.
* You may obtain a copy of the License at http://www.gnu.org/copyleft/lgpl.html
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* LGPL License for more details.
*
* This file was created by members of the firebird development team.
* All individual contributions remain the Copyright (C) of those
* individuals. Contributors to this file are either listed here or
* can be obtained from a CVS history command.
*
* All rights reserved.
*/
/*
* The Original Code is the Firebird Java GDS implementation.
*
* The Initial Developer of the Original Code is Alejandro Alberola.
* Portions created by Alejandro Alberola are Copyright (C) 2001
* Boix i Oltra, S.L. All Rights Reserved.
*/
package org.firebirdsql.gds.impl.wire;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import org.firebirdsql.gds.*;
import org.firebirdsql.logging.Logger;
import org.firebirdsql.logging.LoggerFactory;
/**
* An <code>XdrOutputStream</code> writes data in XDR format to an
* underlying <code>java.io.OutputStream</code>.
*
* @author <a href="mailto:alberola@users.sourceforge.net">Alejandro Alberola</a>
* @author <a href="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
* @version 1.0
*/
public class XdrOutputStream {
private static final int BUF_SIZE = 32767;
private static Logger log = LoggerFactory.getLogger(XdrOutputStream.class,false);
private static byte[] textPad = new byte[BUF_SIZE];
private static byte[] zero = new XSQLVAR().encodeInt(0); // todo
private static byte[] minusOne = new XSQLVAR().encodeInt(-1);
private byte[] buf = new byte[BUF_SIZE];
private int count;
private OutputStream out = null;
protected XdrOutputStream() {
// empty, for subclasses.
}
/**
* Create a new instance of <code>XdrOutputStream</code>.
*
* @param out The underlying <code>OutputStream</code> to write to
*/
public XdrOutputStream(OutputStream out) {
this.out = out;
count=0;
// fill the padding with blanks
Arrays.fill(textPad,(byte) 32);
}
/**
* Write a <code>byte</code> buffer to the underlying output stream in
* XDR format.
*
* @param buffer The <code>byte</code> buffer to be written
* @throws IOException if an error occurs while writing to the
* underlying output stream
*/
public void writeBuffer(byte[] buffer) throws IOException {
if (buffer == null)
writeInt(0);
else {
int len = buffer.length;
writeInt(len);
write(buffer, len, (4 - len) & 3);
}
}
/**
* Write a blob buffer to the underlying output stream in XDR format.
*
* @param buffer A <code>byte</code> array containing the blob
* @throws IOException if an error occurs while writing to the
* underlying output stream
*/
public void writeBlobBuffer(byte[] buffer) throws IOException {
int len = buffer.length ; // 2 for short for buffer length
if (log != null && log.isDebugEnabled()) log.debug("writeBlobBuffer len: " + len);
if (len > Short.MAX_VALUE) {
throw new IOException(""); //Need a value???
}
writeInt(len + 2);
writeInt(len + 2); //bizarre but true! three copies of the length
checkBufferSize(2);
buf[count++] = (byte) ((len >> 0) & 0xff);
buf[count++] = (byte) ((len >> 8) & 0xff);
write(buffer, len, ((4 - len+2)&3));
}
/**
* Write a <code>String</code> to the underlying output stream in
* XDR format.
*
* @param s The <code>String</code> to be written
* @throws IOException if an error occurs while writing to the
* underlying output stream
*/
public void writeString(String s) throws IOException {
byte[] buffer = s.getBytes();
writeBuffer(buffer);
/*
int len = buffer.length;
writeInt(len);
if (len > 0) {
write(buffer, len, (4 - len) & 3);
}
*/
}
/**
* Write content of the specified string using the specified Java encoding.
*/
public void writeString(String s, String encoding) throws IOException {
if (encoding != null)
writeBuffer(s.getBytes(encoding));
else
writeBuffer(s.getBytes());
}
/**
* Write a set of distinct byte values (ie parameter buffer).
*
* @param type The type of the parameter value being written,
* e.g. {@link ISCConstants#isc_tpb_version3}
* @param s The set of byte values to be written
* @throws IOException if an error occurs while writing to the
* underlying output stream
*/
public void writeSet(int type, byte[] s) throws IOException {
if (s == null) {
writeInt(1);
checkBufferSize(1);
buf[count++] = (byte) type; //e.g. gds.isc_tpb_version3
}
else {
int len = s.length;
writeInt(len + 1);
checkBufferSize(1);
buf[count++] = (byte) type;
write(s, len, (4 - (len+1)) & 3);
}
}
/**
* Write an <code>Xdrable</code> to this output stream.
*
* @param type Type of the <code>Xdrable</code> to be written,
* e.g. {@link ISCConstants#isc_tpb_version3}
* @param item The object to be written
* @throws IOException if an error occurs while writing to the
* underlying output stream
*/
public void writeTyped(int type, Xdrable item) throws IOException {
int size;
if (item == null) {
writeInt(1);
checkBufferSize(1);
buf[count++] = (byte) type; //e.g. gds.isc_tpb_version3
size = 1;
}
else {
size = item.getLength() + 1;
writeInt(size);
checkBufferSize(1);
buf[count++] = (byte) type; //e.g. gds.isc_tpb_version3
item.write(this);
}
count += (4 - size) & 3;
}
//
// WriteSQLData methods
//
/**
* Write a set of SQL data from a <code>XSQLDA</code> data structure.
*
* @param xsqlda The datastructure containing the SQL data to be written
* @throws IOException if an error occurs while writing to the
* underlying output stream
*/
public void writeSQLData(XSQLDA xsqlda) throws IOException {
for (int i = 0; i < xsqlda.sqld; i++) {
XSQLVAR xsqlvar = xsqlda.sqlvar[i];
if (log != null && log.isDebugEnabled()) {
if (out == null) {
log.debug("db.out null in writeSQLDatum");
}
if (xsqlvar.sqldata == null) {
log.debug("sqldata null in writeSQLDatum: " + xsqlvar);
}
if (xsqlvar.sqldata == null) {
log.debug("sqldata still null in writeSQLDatum: " + xsqlvar);
}
}
int len = xsqlda.ioLength[i];
byte[] buffer = xsqlvar.sqldata;
int tempType = xsqlvar.sqltype & ~1;
if (tempType == ISCConstants.SQL_NULL) {
write(xsqlvar.sqldata != null ? zero : minusOne, 4, 0);
} else
if (len==0) {
if (buffer != null) {
len = buffer.length;
writeInt(len);
write(buffer, len, (4 - len) & 3);
// sqlind
write(zero, 4, 0);
}
else{
writeInt(0);
// sqlind
write(minusOne, 4, 0);
}
}
else if (len < 0){
if (buffer != null) {
write(buffer, -len, 0);
// sqlind
write(zero,4, 0);
}
else{
write(textPad, -len, 0);
// sqlind
write(minusOne, 4, 0);
}
}
else {
// decrement length because it was incremented before
len--;
if (buffer != null) {
int buflen = buffer.length;
if (buflen >=len){
write(buffer, len, (4 - len) & 3);
}
else{
write(buffer, buflen, 0);
write(textPad, len- buflen, (4 - len) & 3);
}
// sqlind
write(zero, 4, 0);
}
else{
write(textPad, len, (4 - len) & 3);
// sqlind
write(minusOne, 4, 0);
}
}
}
}
//
// DataOutputStream methods
//
/**
* Write a <code>long</code> value to the underlying stream in XDR format.
*
* @param v The <code>long</code> value to be written
* @throws IOException if an error occurs while writing to the
* underlying output stream
*/
public void writeLong(long v) throws IOException {
checkBufferSize(8);
buf[count++] = (byte) (v >>> 56 & 0xFF);
buf[count++] = (byte) (v >>> 48 & 0xFF);
buf[count++] = (byte) (v >>> 40 & 0xFF);
buf[count++] = (byte) (v >>> 32 & 0xFF);
buf[count++] = (byte) (v >>> 24 & 0xFF);
buf[count++] = (byte) (v >>> 16 & 0xFF);
buf[count++] = (byte) (v >>> 8 & 0xFF);
buf[count++] = (byte) (v >>> 0 & 0xFF);
}
/**
* Write an <code>int</code> value to the underlying stream in XDR format.
*
* @param v The <code>int</code> value to be written
* @throws IOException if an error occurs while writing to the
* underlying output stream
*/
public void writeInt(int v) throws IOException {
checkBufferSize(4);
buf[count++] = (byte) (v >>> 24);
buf[count++] = (byte) (v >>> 16);
buf[count++] = (byte) (v >>> 8);
buf[count++] = (byte) (v >>> 0);
}
//
// Buffering
// If the piece to write is greater than 256 bytes, write it directly
//
/**
* Write a <code>byte</code> buffer to the underlying output stream
* in XDR format
*
* @param b The <code>byte</code> buffer to be written
* @param len The number of bytes to write
* @param pad The number of (blank) padding bytes to write
* @throws IOException if an error occurs while writing to the
* underlying output stream
*/
public void write(byte[] b, int len, int pad) throws IOException {
if (len > 256 || count + len >= BUF_SIZE){
if (count > 0)
out.write(buf, 0, count);
out.write(b, 0, len);
out.write(textPad, 0, pad);
count = 0;
}
else {
checkBufferSize(len + pad);
System.arraycopy(b, 0, buf, count, len);
count += len;
System.arraycopy(textPad, 0, buf, count, pad);
count += pad;
}
}
/**
* Write a single <code>byte</code> to the underlying output stream in
* XDR format.
*
* @param b The value to be written, will be truncated to a byte
* @throws IOException if an error occurs while writing to the
* underlying output stream
*/
public void write(int b) throws IOException {
checkBufferSize(1);
buf[count++] = (byte)b;
}
/**
* Write an array of <code>byte</code>s to the underlying output stream
* in XDR format.
*
* @param b The <code>byte</code> array to be written
* @throws IOException if an error occurs while writing to the
* underlying output stream
*/
public void write(byte b[]) throws IOException{
write(b,b.length, 0);
}
/**
* Flush all buffered data to the underlying output stream.
*
* @throws IOException if an error occurs while writing to the
* underlying output stream
*/
public void flush() throws IOException {
if (count > 0){
out.write(buf,0,count);
}
count=0;
out.flush();
}
/**
* Close this stream and the underlying output stream.
*
* @throws IOException if an error occurs while closing the
* underlying stream
*/
public void close() throws IOException {
try {
out.close();
} finally {
buf = null;
out = null;
}
}
/**
* Ensure that there is room in the buffer for a given number
* of direct writes to it.
* @param countToWrite The size of write that is being checked.
* @throws IOException If a write to the underlying OutputStream fails
*/
private void checkBufferSize(int countToWrite) throws IOException {
if (BUF_SIZE - count <= countToWrite){
out.write(buf, 0, count);
count = 0;
}
}
}