-
Notifications
You must be signed in to change notification settings - Fork 0
/
CTypedStructWriteFormat.java
698 lines (654 loc) · 21.9 KB
/
CTypedStructWriteFormat.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
package sztejkat.abstractfmt;
import sztejkat.abstractfmt.logging.SLogging;
import java.io.IOException;
/**
The <i>typed</i> stream implementing {@link ITypedStructWriteFormat}.
<p>
This stream is implemented over the {@link AReservedNameWriteFormat}.
<h1>Use of reserved names</h1>
This stream do reserve following names:
<pre id="default_reserved_names_table">
bool
byte
char
short
int
long
float
double
str
bool[]
byte[]
char[]
short[]
int[]
long[]
float[]
double[]
</pre>
with {@link AReservedNameWriteFormat#reserveName}
and requests to optimize them by calling {@link IStructWriteFormat#optimizeBeginName}
on the underlying stream. The call is made in order of appearance on above list.
<p>
<i>Note: This set may be customized by an apropriate constructor argument
what may be wise if XML format is used as a back-end due to some of them not
being valid XML tag names.</i>
<h2>Describing primitive elementary operations</h2>
If an allowed primitive elementary operation is initialized
this class tests if:
<ul>
<li>there was already primitive elementary operation since last user
signal;</li>
<li>said operation is same type;</li>
</ul>
If it is, nothing is done.
<p>
If it is not:
<ul>
<li>if there was an elementary primitive operation of another type
the <code>end</code> signal is written to a stream;</li>
<li>then the <code>begin</code> signal is written
with {@link AReservedNameWriteFormat#beginReserved} and a name matching the type.
</ul>
<h2>Describing primitive block operations</h2>
Alike rules do apply, but since no block operation type can be changed the set
of choices is limited.
<h1>Effect and examples</h1>
In effect the sequnece of elementary primitive operations of the same type
is enclosed by <code>begin(type_name)</code> and <code>end</code> signals.
<p>
For an example if user writes:
<pre>
begin("mystruct")
writeInt(33)
writeInt(34)
writeFloat(0.1f)
end()
</pre>
the underlying stream will receive:
<pre>
begin("mystruct")
begin("int")
writeInt(33)
writeInt(34)
end();
begin("float");
writeFloat(0.1f)
end();
end()
</pre>
And alike, for block sequence it will be like below:
<br>
User writes:
<pre>
begin("mystruct")
writeIntBlock(new int[100])
writeIntBlock(new int[100])
end()
</pre>
the underlying stream will receive:
<pre>
begin("mystruct")
begin("int[]")
writeIntBlock(new int[100])
writeIntBlock(new int[100])
end()
end()
</pre>
Notice a slightly unefficient double <code>end()</code> for block operations. This cost is intentional
due to greater simplicity of encoding and a necessity to ensure that blocks are properly terminated
even in case of un-typed stream.
<p>
Theoretically the side effect <i>could be</i> such that a typed stream <i>could</i> allow:
<pre>
writeIntBlock(...)
writeLongBlock(...)
</pre>
due to injected <code>end</code> signals but I will directly <code>prohibit it</code> for sake of
contract clarity.
<h1>Design notes</h1>
This class do slightly duplicate the state machine of {@link AStructFormatBase}.
This is intentional.
*/
public class CTypedStructWriteFormat extends AReservedNameWriteFormat implements ITypedStructWriteFormat
{
private static final long TLEVEL = SLogging.getDebugLevelForClass(CTypedStructWriteFormat.class);
private static final boolean TRACE = (TLEVEL!=0);
private static final java.io.PrintStream TOUT = TRACE ? SLogging.createDebugOutputForClass("CTypedStructWriteFormat.",CTypedStructWriteFormat.class) : null;
/** An index used for reserved "bool" name in type names table
@see #type_names
*/
public static final int BOOLEAN_idx = 0;
/** An index used for reserved "byte" name in type names table
@see #type_names
*/
public static final int BYTE_idx = 1;
/** An index used for reserved "char" name in type names table
@see #type_names
*/
public static final int CHAR_idx = 2;
/** An index used for reserved "short" name in type names table
@see #type_names
*/
public static final int SHORT_idx = 3;
/** An index used for reserved "int" name in type names table
@see #type_names
*/
public static final int INT_idx = 4;
/** An index used for reserved "long" name in type names table
@see #type_names
*/
public static final int LONG_idx = 5;
/** An index used for reserved "float" name in type names table
@see #type_names
*/
public static final int FLOAT_idx = 6;
/** An index used for reserved "double" name in type names table
@see #type_names
*/
public static final int DOUBLE_idx = 7;
//Important: all blocks above STRING_blk_idx. See validation code.
/** An index used for reserved "str" name in type names table
@see #type_names
*/
public static final int STRING_blk_idx = 8;
/** An index used for reserved "bool[]" name in type names table
@see #type_names
*/
public static final int BOOLEAN_blk_idx = 9;
/** An index used for reserved "byte[]" name in type names table
@see #type_names
*/
public static final int BYTE_blk_idx = 10;
/** An index used for reserved "char[]" name in type names table
@see #type_names
*/
public static final int CHAR_blk_idx = 11;
/** An index used for reserved "short[]" name in type names table
@see #type_names
*/
public static final int SHORT_blk_idx = 12;
/** An index used for reserved "int[]" name in type names table
@see #type_names
*/
public static final int INT_blk_idx = 13;
/** An index used for reserved "long[]" name in type names table
@see #type_names
*/
public static final int LONG_blk_idx = 14;
/** An index used for reserved "float[]" name in type names table
@see #type_names
*/
public static final int FLOAT_blk_idx = 15;
/** An index used for reserved "double[]" name in type names table
@see #type_names
*/
public static final int DOUBLE_blk_idx = 16;
/** Default value to be used for {@link #type_names} */
public static final String [] DEFAULT_TYPE_NAMES =
{
//Note: meaning must match the XXX_idx constants.
"bool",
"byte",
"char",
"short",
"int",
"long",
"float",
"double",
"str",
"bool[]",
"byte[]",
"char[]",
"short[]",
"int[]",
"long[]",
"float[]",
"double[]"
};
/** An immutable array indexed by <code>XXX_idx</code> constants
used to fetch type names. */
private final String [] type_names;
/** is carrying <code>XXXX_idx</code> operation code in progress
or -1 if there is no operation in progress. */
private int current_type;
/** Tracks format open state */
private boolean opened;
/** Tracks format closes state */
private boolean closed;
/* *************************************************************************
Construction
**************************************************************************/
/** Creates
@param engine an underlying low level engine, see {@link AStructWriteFormatAdapter#AStructWriteFormatAdapter}
@param escape an escape character. This character must not be a first character of any type name
@param type_names an array of names of types, indexed by <code>XXXX_idx</code> constants.
Immutable, taken not copied, cannot be null, can't contain null.
*/
public CTypedStructWriteFormat(
IStructWriteFormat engine,
char escape,
String [] type_names
)
{
super(engine, escape);
assert(assertTypeNames(type_names));
this.type_names = type_names;
//reserve them, order doesn't matter.
for(int i = type_names.length; --i>=0;)
reserveName(type_names[i]);
//no type info
current_type = -1;
};
/** Validates constructor parameter in assertion mode
@param type_names constructor parameter
@return always true, but may assert before returning.
*/
private static boolean assertTypeNames(String [] type_names)
{
assert(type_names!=null);
assert(type_names.length==1+DOUBLE_blk_idx):"type_names.length="+type_names.length;
for(int i = type_names.length; --i>=0;)
{
assert(type_names[i]!=null):"null name at type_names["+i+"]";
};
return true;
};
/* ***********************************************************************
Internal services
************************************************************************/
/* ----------------------------------------------------------------
State validation
----------------------------------------------------------------*/
/** Throws if closed
@throws EClosed .*/
private final void validateNotClosed()throws EClosed
{
if (closed) throw new EClosed("Already closed");
};
/** Throws if not opened
@throws ENotOpen .*/
private final void validateOpen()throws ENotOpen
{
if (!opened) throw new ENotOpen("Not open yet");
};
/** Calls {@link #validateOpen} and {@link #validateNotClosed}
@throws EClosed if already closed
@throws ENotOpen if not open yet.
*/
private final void validateUsable()throws EClosed,ENotOpen
{
validateOpen();
validateNotClosed();
};
/* *************************************************************************
IStructWriteFormat
**************************************************************************/
/* ----------------------------------------------------------------------
Signals
----------------------------------------------------------------------*/
/** Terminates pending type information, if any
@throws IOException if failed.
*/
private void closePendingTypeInfo()throws IOException
{
if (current_type!=-1)
{
if (TRACE) TOUT.println("closePendingTypeInfo(), closing pending type info.");
current_type=-1;
super.end();
}else
{
if (TRACE) TOUT.println("closePendingTypeInfo(), no pending type info.");
};
};
/** Overriden to manage type info */
@Override public void begin(String name)throws IOException
{
if (TRACE) TOUT.println("begin(\""+name+"\") ENTER");
validateUsable();
//clean-up type info if present
closePendingTypeInfo();
//do it.
super.begin(name);
if (TRACE) TOUT.println("begin() LEAVE");
};
/** Overriden to manage type info */
@Override public void end()throws IOException
{
if (TRACE) TOUT.println("end() ENTER");
validateUsable();
//clean-up type info if present
closePendingTypeInfo();
super.end();
if (TRACE) TOUT.println("end() LEAVE");
};
/* ----------------------------------------------------------------------
Elementary ops
----------------------------------------------------------------------*/
/** Makes sure that type info describes what is specified by passed constant
@param XXX_idx type constant
@throws IOException if failed.
*/
private void validateTypeInfo(int XXX_idx)throws IOException
{
if (TRACE) TOUT.println("validateTypeInfo("+XXX_idx+") ENTER");
assert(XXX_idx>=BOOLEAN_idx);
assert(XXX_idx<=DOUBLE_blk_idx);
validateUsable();
int ct = current_type ; //fast access.
if (ct!=XXX_idx)
{
//different
if (TRACE) TOUT.println("validateTypeInfo, switching type");
closePendingTypeInfo();
super.beginReserved(type_names[XXX_idx]);
current_type = XXX_idx;
};
if (TRACE) TOUT.println("validateTypeInfo("+XXX_idx+") LEAVE");
};
/** Makes sure that type info describes what is specified by passed constant
AND that said elementary operation is currently allowed.
@param XXX_idx type constant, elementary primitive operation.
@throws IOException if failed.
*/
private void validateElementaryTypeInfo(int XXX_idx)throws IOException
{
assert(XXX_idx>=BOOLEAN_idx);
assert(XXX_idx<STRING_blk_idx);
int ct = current_type;
if (ct>=STRING_blk_idx) //this simple check is enough, all blocks are above it.
throw new IllegalStateException("blok operation "+type_names[ct]+" in progress");
validateTypeInfo(XXX_idx);
};
/** Makes sure that type info describes what is specified by passed constant
AND that said block operation is currently allowed.
@param XXX_idx type constant, primitive block operation.
@throws IOException if failed.
*/
private void validateBlockTypeInfo(int XXX_idx)throws IOException
{
assert(XXX_idx>=STRING_blk_idx);
assert(XXX_idx<=DOUBLE_blk_idx);
int ct = current_type;
if ((ct>=STRING_blk_idx)&&(ct!=XXX_idx)) //this simple check is enough to reject non-matching blocks.
throw new IllegalStateException("confliciting blok operation "+type_names[ct]+" in progress");
validateTypeInfo(XXX_idx);
};
/* ..............................................................................
Primitive ops.
..............................................................................*/
/** Overriden to inject type information */
@Override public void writeBoolean(boolean v)throws IOException
{
validateElementaryTypeInfo(BOOLEAN_idx);
super.writeBoolean(v);
};
/** Overriden to inject type information */
@Override public void writeByte(byte v)throws IOException
{
validateElementaryTypeInfo(BYTE_idx);
super.writeByte(v);
};
/** Overriden to inject type information */
@Override public void writeChar(char v)throws IOException
{
validateElementaryTypeInfo(CHAR_idx);
super.writeChar(v);
};
/** Overriden to inject type information */
@Override public void writeShort(short v)throws IOException
{
validateElementaryTypeInfo(SHORT_idx);
super.writeShort(v);
};
/** Overriden to inject type information */
@Override public void writeInt(int v)throws IOException
{
validateElementaryTypeInfo(INT_idx);
super.writeInt(v);
};
/** Overriden to inject type information */
@Override public void writeLong(long v)throws IOException
{
validateElementaryTypeInfo(LONG_idx);
super.writeLong(v);
};
/** Overriden to inject type information */
@Override public void writeFloat(float v)throws IOException
{
validateElementaryTypeInfo(FLOAT_idx);
super.writeFloat(v);
};
/** Overriden to inject type information */
@Override public void writeDouble(double v)throws IOException
{
validateElementaryTypeInfo(DOUBLE_idx);
super.writeDouble(v);
};
/* ..............................................................................
block ops.
..............................................................................*/
/** Overriden to inject type information */
@Override public void writeBooleanBlock(boolean [] buffer, int offset, int length)throws IOException
{
validateBlockTypeInfo(BOOLEAN_blk_idx);
super.writeBooleanBlock(buffer,offset,length);
};
/** Overriden to inject type information */
@Override public void writeBooleanBlock(boolean [] buffer)throws IOException
{
validateBlockTypeInfo(BOOLEAN_blk_idx);
super.writeBooleanBlock(buffer);
};
/** Overriden to inject type information */
@Override public void writeBooleanBlock(boolean v)throws IOException
{
validateBlockTypeInfo(BOOLEAN_blk_idx);
super.writeBooleanBlock(v);
};
/** Overriden to inject type information */
@Override public void writeByteBlock(byte [] buffer, int offset, int length)throws IOException
{
validateBlockTypeInfo(BYTE_blk_idx);
super.writeByteBlock(buffer,offset,length);
};
/** Overriden to inject type information */
@Override public void writeByteBlock(byte [] buffer)throws IOException
{
validateBlockTypeInfo(BYTE_blk_idx);
super.writeByteBlock(buffer);
};
/** Overriden to inject type information */
@Override public void writeByteBlock(byte v)throws IOException
{
validateBlockTypeInfo(BYTE_blk_idx);
super.writeByteBlock(v);
};
/** Overriden to inject type information */
@Override public void writeCharBlock(char [] buffer, int offset, int length)throws IOException
{
validateBlockTypeInfo(CHAR_blk_idx);
super.writeCharBlock(buffer,offset,length);
};
/** Overriden to inject type information */
@Override public void writeCharBlock(char [] buffer)throws IOException
{
validateBlockTypeInfo(CHAR_blk_idx);
super.writeCharBlock(buffer);
};
/** Overriden to inject type information */
@Override public void writeCharBlock(char v)throws IOException
{
validateBlockTypeInfo(CHAR_blk_idx);
super.writeCharBlock(v);
};
/** Overriden to inject type information */
@Override public void writeShortBlock(short [] buffer, int offset, int length)throws IOException
{
validateBlockTypeInfo(SHORT_blk_idx);
super.writeShortBlock(buffer,offset,length);
};
/** Overriden to inject type information */
@Override public void writeShortBlock(short [] buffer)throws IOException
{
validateBlockTypeInfo(SHORT_blk_idx);
super.writeShortBlock(buffer);
};
/** Overriden to inject type information */
@Override public void writeShortBlock(short v)throws IOException
{
validateBlockTypeInfo(SHORT_blk_idx);
super.writeShortBlock(v);
};
/** Overriden to inject type information */
@Override public void writeIntBlock(int [] buffer, int offset, int length)throws IOException
{
validateBlockTypeInfo(INT_blk_idx);
super.writeIntBlock(buffer,offset,length);
};
/** Overriden to inject type information */
@Override public void writeIntBlock(int [] buffer)throws IOException
{
validateBlockTypeInfo(INT_blk_idx);
super.writeIntBlock(buffer);
};
/** Overriden to inject type information */
@Override public void writeIntBlock(int v)throws IOException
{
validateBlockTypeInfo(INT_blk_idx);
super.writeIntBlock(v);
};
/** Overriden to inject type information */
@Override public void writeLongBlock(long [] buffer, int offset, int length)throws IOException
{
validateBlockTypeInfo(LONG_blk_idx);
super.writeLongBlock(buffer,offset,length);
};
/** Overriden to inject type information */
@Override public void writeLongBlock(long [] buffer)throws IOException
{
validateBlockTypeInfo(LONG_blk_idx);
super.writeLongBlock(buffer);
};
/** Overriden to inject type information */
@Override public void writeLongBlock(long v)throws IOException
{
validateBlockTypeInfo(LONG_blk_idx);
super.writeLongBlock(v);
};
/** Overriden to inject type information */
@Override public void writeFloatBlock(float [] buffer, int offset, int length)throws IOException
{
validateBlockTypeInfo(FLOAT_blk_idx);
super.writeFloatBlock(buffer,offset,length);
};
/** Overriden to inject type information */
@Override public void writeFloatBlock(float [] buffer)throws IOException
{
validateBlockTypeInfo(FLOAT_blk_idx);
super.writeFloatBlock(buffer);
};
/** Overriden to inject type information */
@Override public void writeFloatBlock(float v)throws IOException
{
validateBlockTypeInfo(FLOAT_blk_idx);
super.writeFloatBlock(v);
};
/** Overriden to inject type information */
@Override public void writeDoubleBlock(double [] buffer, int offset, int length)throws IOException
{
validateBlockTypeInfo(DOUBLE_blk_idx);
super.writeDoubleBlock(buffer,offset,length);
};
/** Overriden to inject type information */
@Override public void writeDoubleBlock(double [] buffer)throws IOException
{
validateBlockTypeInfo(DOUBLE_blk_idx);
super.writeDoubleBlock(buffer);
};
/** Overriden to inject type information */
@Override public void writeDoubleBlock(double v)throws IOException
{
validateBlockTypeInfo(DOUBLE_blk_idx);
super.writeDoubleBlock(v);
};
/** Overriden to inject type information */
@Override public void writeString(CharSequence characters, int offset, int length)throws IOException
{
validateBlockTypeInfo(STRING_blk_idx);
super.writeString(characters,offset,length);
};
/** Overriden to inject type information */
@Override public void writeString(CharSequence characters)throws IOException
{
validateBlockTypeInfo(STRING_blk_idx);
super.writeString(characters);
};
@Override public void writeString(char c)throws IOException
{
validateBlockTypeInfo(STRING_blk_idx);
super.writeString(c);
}
/* ----------------------------------------------------------------------
State management.
----------------------------------------------------------------------*/
/** Overriden to make sure, that reserved names are optimized. */
@Override public void open()throws IOException,EClosed
{
//Note: we need to duplicate some state tracking machinery.
if (TRACE) TOUT.println("open() ENTER");
validateNotClosed();
if (opened)
{
if (TRACE) TOUT.println("open(), already open LEAVE");
return;
};
if (TRACE) TOUT.println("open()->openImpl()");
openImpl();
opened = true; //not set if openImpl failed.
//low level close will be invoked regardless.
if (TRACE) TOUT.println("open() LEAVE");
};
private void openImpl()throws IOException
{
if (TRACE) TOUT.println("openImpl() ENTER");
//Note: reserved names optimization can be turned on only AFTER stream is
//opened.
super.open();
//now hint downstream about optimization.
//This time order DO MATTER.
for(int i=0,n=type_names.length; i<n; i++)
{
if (TRACE) TOUT.println("optimizing \""+type_names[i]+"\"");
super.optimizeBeginName(type_names[i]);
};
//make sure no type info is present.
current_type = -1;
if (TRACE) TOUT.println("openImpl() LEAVE");
};
/** Overriden to ensure, that if type information was written it is terminated. */
@Override public void close()throws IOException
{
if (TRACE) TOUT.println("close() ENTER");
if (closed)
{
if (TRACE) TOUT.println("close(), alreay closed LEAVE");
return;
};
try{
if (TRACE) TOUT.println("close()->closeImpl()");
closeImpl();
if (TRACE) TOUT.println("close()->super.close()");
}finally{ closed = true; }
if (TRACE) TOUT.println("close() LEAVE");
};
private void closeImpl()throws IOException
{
if (TRACE) TOUT.println("closeImpl() ENTER");
try{
closePendingTypeInfo();
}finally{ super.close(); };
if (TRACE) TOUT.println("closeImpl() LEAVE");
};
};