File: StructuredBytes.java

package info (click to toggle)
libsapdbc-java 5567-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,840 kB
  • ctags: 6,242
  • sloc: java: 49,887; makefile: 83
file content (729 lines) | stat: -rw-r--r-- 16,755 bytes parent folder | download
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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
/*


    ========== licence begin GPL
    Copyright (C) 2002-2003 SAP AG

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    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
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    ========== licence end


*/

package com.sap.dbtech.util;

import java.io.*;

import com.sap.dbtech.jdbc.translators.ConversionExceptionSapDB;

/**
 *
 *
 */
public class StructuredBytes
    implements StructuredMem, Traceable
{
    public static final String sapdbEncodingC = "ISO8859_1";

    protected byte [] data;
    protected int ptrOffs;
    protected int byteSize;

    protected final static byte blank_C = 32;
    public final static char[] hexDigits_C = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    private static final int fillBufSizeC = 1024;
    private static final int unicodeWidthC = 2;
    static byte [] zeroBytes = new byte [fillBufSizeC];
    static byte [] blankBytes = new byte [fillBufSizeC];
    static byte [] blankUnicodeBytes = new byte [fillBufSizeC * 2];
    static {

        for (int i = 0; i < fillBufSizeC; i+=2) {
            zeroBytes  [i] = zeroBytes [i+1] = 0;
            blankBytes [i] = blankBytes[i+1] = blank_C;
            blankUnicodeBytes [i] = 0;
            blankUnicodeBytes [i + 1] = blank_C;
        }
    }




    /**
     * Access <i>data</i> as StructuredMem
     * @param data byte[]
     */
    public StructuredBytes (
        byte [] data)
    {
        this.data=data;
        this.ptrOffs=0;
        this.byteSize=data.length;
    }
    
    /**
     * Access <i>data</i> as StructuredMem
     * @param data byte[]
     * @param offset int
     */
    public StructuredBytes (
        byte [] data,
        int offset)
    {
        this.data = data;
        this.ptrOffs = offset;
        this.byteSize = data.length - ptrOffs;
    }


    /**
     * Create a new StructuredMem of <i>size<i> bytes
     * @param size int
     */
    public StructuredBytes (
        int size)
    {
        this.data=new byte[size];
        this.byteSize=size;
    }
    
    /**
     *
     * @return byte[]
     */
    public byte[] bytes ()
    {
        return data;
    }
    /**
     *
     * @return byte[]
     * @param offset int
     * @param len int
     */
    public byte[] getBytes (
        int offset,
        int len)
    {
        byte [] result = new byte [len];
        System.arraycopy (data, offset + this.ptrOffs, result, 0, len);
        return result;
    }
    /**
     *
     * @return int[]
     * @param offset int
     * @param len int
     */
    public int[] getIntArray (
        int offset,
        int len)
    {
        int [] result = new int [len];
        for (int i = 0; i < len; i++) {
          result[i] = this.getUInt1(offset+i);
        }
        return result;
    }
    /**
     * Read a signed one byte integer from <i>offset</i>
     * @return byte
     * @param offset int
     */
    public byte getInt1 (
        int offset)
    {
        return data [offset + this.ptrOffs];
    }
    /**
     * Read an unsigned one byte integer from <i>offset</i>
     * @return int
     * @param offset int
     */
    public int getUInt1 (
        int offset)
    {
        int result = data [offset + this.ptrOffs];

        if (result < 0) {
            result += 256;
        }
        return result;
    }
    /**
     * Read a signed two byte integer from <i>offset</i>
     * @return int
     * @param offset int
     */
    public int getInt2 (
        int offset)
    {
        int result;
        int ofs=offset+this.ptrOffs;
        
        int lowerByte = data [ofs + 1];
        if (lowerByte < 0) {
            lowerByte += 256;
        }
        return (data [ofs] * 256) + lowerByte;
    }
    
    /**
     * Read a signed four byte integer from <i>offset</i>
     * @return int
     * @param offset int
     */
    public int getInt4 (
        int offset)
    {
        byte[] d=this.data;
        int ofs=offset+this.ptrOffs;
        int result=0;
        
        for (int i = 0; i < 4; ++i) {
            int oneByte = d[ofs + i];
            if (oneByte < 0) {
                oneByte += 256;
            }
            result = (result * 256) + oneByte;
        }
        
        return result;
    }

    /**
     *
     * @return com.sap.dbtech.util.StructuredBytes
     * @param offset int
     */
    public StructuredMem getPointer (
        int offset)
    {
        return new StructuredBytes (this.data, this.ptrOffs + offset);
    }
    /**
     *
     * @return java.lang.String
     * @param offset int
     * @param len int
     */
    public String getString (
        int offset,
        int len)
    {
        try {
            return new String (data, offset + this.ptrOffs, len, sapdbEncodingC);
        }
        catch (java.io.UnsupportedEncodingException exc) {
            return new String (data, offset + this.ptrOffs, len);
        }
    }
    
    /**
     *
     * @return byte[]
     * @param offset int
     * @param len int
     */
    public byte [] getStrippedBytes (
        int offset,
        int len)
    {
        while ((len > 0) && (data [offset + this.ptrOffs + len - 1] == 0)) {
            --len;
        }
        return getBytes (offset, len);
    }
    /**
    *
    * @param offset int
    * @param len int
    */
   public byte [] getStrippedUnicodeString (
       int offset,
       int len)
   {
       while ((len > 1) && (data [offset + this.ptrOffs + len - 1] == 0x20)&& (data [offset + this.ptrOffs + len - 2] == 0x00)) {
           len-=2;
       }
       return getBytes(offset, len);

       
//       String trimmedString;
//
//       trimmedString = StringUtil.trimLeft (this.getString (offset, len));
//       return trimmedString;
   }
    /**
     *
     * @param offset int
     * @param len int
     */
    public String getStrippedString (
        int offset,
        int len)
    {
        while ((len > 0) && (data [offset + this.ptrOffs + len - 1] == 0x20)) {
            --len;
        }
        return getString(offset, len);

        
//        String trimmedString;
//
//        trimmedString = StringUtil.trimLeft (this.getString (offset, len));
//        return trimmedString;
    }
    /**
     * reads a bigendian unicode character from <i>offset</i>
     *
     * @param offset
     *
     * @return char
     */
    public char
    getBigUnicodeChar (
        int offset)
    {
        int realOffset = this.ptrOffs + offset;

        int high = this.data [realOffset];
        if (high < 0) {
            high += 256;
        }
        int low = this.data [realOffset + 1];
        if (low < 0) {
            low += 256;
        }
        int result = (high << 8) + low;
        return (char) result;
    }
    /**
     * converts <i>lenInBytes</i> bytes
     * into <i>lenInBytes</i> chars.
     *
     * @param offset
     *
     * @param lenInBytes
     *
     * @return char []
     */
    public char []
    getBigUnicode (
        int offset,
        int lenInBytes)
    {
        int realOffset = this.ptrOffs + offset;

        return UnicodeUtil.bigUnicode2Chars (this.data,
            realOffset, lenInBytes);
    }
    /**
     *
     * @param offset int
     */
    public void moveBase (
        int offset)
    {
        this.ptrOffs += offset;
        if (this.size () < 0) {
            throw new ArrayIndexOutOfBoundsException ();
        }
        this.byteSize -= offset;
    }
    /**
     *
     * @param value byte[]
     * @param offset int
     */
    public void putBytes (
        byte [] value,
        int offset)
    {
        System.arraycopy (value, 0, data, offset + this.ptrOffs, value.length);
        return;
    }
    /**
     *
     * @param value byte[]
     * @param offset int
     * @param len int
     */
    private void putBytes (
        byte [] value,
        int offset,
        int len,
        byte [] filler)
    {
        int copyLen = value.length;
        int fillLen = 0;

        if (copyLen > len) {
            copyLen = len;
        }
        else if (copyLen <  len) {
            fillLen = len - copyLen;
        }
        System.arraycopy (value, 0, data, offset + this.ptrOffs, copyLen);
        if (fillLen > 0) {
            this.fill (offset + copyLen, fillLen, filler);
        }
        return;
    }
    /**
     *
     * @param value byte[]
     * @param offset int
     * @param len int
     */
    public void putBytes (
        byte [] value,
        int offset,
        int len)
    {
        this.putBytes (value, offset, len, zeroBytes);
    }
    /**
     *
     * @param value byte[]
     * @param offset int
     * @param len int
     */
    public void putStringBytes (
        byte [] value,
        int offset,
        int len)
    {
        this.putBytes (value, offset, len, blankBytes);
    }
    /**
     *
     * @param value byte[]
     * @param offset int
     * @param len int
     */
    public void putUnicodeBytes (
        byte [] value,
        int offset,
        int len)
    {
        this.putBytes (value, offset, len, blankUnicodeBytes);
    }
    /**
     *
     * @param value char[]
     * @param offsetInBytes int
     * @param lenInBytes int
     */
    public void
    putBigUnicode (
        char [] value,
        int offsetInBytes,
        int lenInBytes)
    {
        int copyLen = value.length;
        int lenInChars = lenInBytes / unicodeWidthC;
        int fillLen = 0;

        /*
         * calculate sizes
         */
        if (copyLen > lenInChars) {
            copyLen = lenInChars;
        }
        else if (copyLen <  lenInBytes) {
            fillLen = lenInBytes - (copyLen * unicodeWidthC);
        }
        /*
         * convert unicode to bytes
         */
        int realOffset = this.ptrOffs + offsetInBytes;
        for (int i = 0; i < copyLen; ++i) {
            char current = value [i];
            this.data [i * 2 + realOffset] = (byte) (current / 256);
            this.data [i * 2 + realOffset + 1] = (byte) (current % 256);
        }
        /*
         * fill with unicode blanks
         */
        if (fillLen > 0) {
            this.fill (offsetInBytes + (copyLen * unicodeWidthC),
                fillLen, blankUnicodeBytes);
        }
    }
    /**
     * Write a signed one byte integer at <i>offset</i>
     * @param value int
     * @param offset int
     */
    public void putInt1 (
        int value,
        int offset)
    {
        data [offset + this.ptrOffs] = (byte) value;
        return;
    }
    /**
     * Write a signed two byte integer at <i>offset</i>
     * @param value The value to write.
     * @param offset The offset.
     */
    public void putInt2 (int value,
                         int offset)
    {
        int ofs = offset + this.ptrOffs;
        byte[] d=data;
        
        if(value==0) {
            d[ofs]=d[ofs+1]=0;
            return;
        } 
        d[ofs] = (byte) ((value >> 8) & 0xff); // high byte
        d[ofs + 1] = (byte)(value  & 0xff);    // low byte
        return;
    }
    
    /**
     * Write a signed four byte integer at <i>offset</i>
     * @param value int
     * @param offset int
     */
    public void putInt4 (
        int value,
        int offset)
    {
        int ofs = offset + this.ptrOffs;
        byte[] d=data;
        
        if(value==0) {
            d[ofs] = d[ofs+1] = d[ofs+2] = d[ofs+3] = 0;
            return;
        }
        
        for (int i = 3; i >= 0; --i) {
            d[ofs + i] = (byte) (value & 0xff);
            value >>= 8;
        }
        return;
    }
    
	public void putInt8 (
		long value,
		int offset)
	{
		int ofs = offset + this.ptrOffs;
		byte[] d=data;
        
		if(value==0) {
			d[ofs] = d[ofs+1] = d[ofs+2] = d[ofs+3] =
			d[ofs+4] = d[ofs+5] = d[ofs+6] = d[ofs+7] 
			= 0;
			return;
		}
        
		for (int i = 7; i >= 0; --i) {
			d[ofs + i] = (byte) (value & 0xff);
			value >>= 8;
		}
		return;
	}

    /**
     *
     * @param value java.lang.String
     * @param offset int
     */
    public int putStringThrowExc (
        String value,
        int offset) throws ConversionExceptionSapDB
    {
        // encoding default is ISO8859_1
        // other encodings are not supported (now).
        
        int ofs=offset+this.ptrOffs;
        char[] ca=value.toCharArray();
        int strlen=ca.length;
        
        for(int i=0; i<strlen; ++i) {
            char c=ca[i];
            if(c <= 255 ) {
                data[ofs++]=(byte) c;
            } else {
//                data[ofs++]=63; // '?'
               throw new ConversionExceptionSapDB(MessageTranslator.translate(MessageKey.ERROR_UNICODETOASCII,value));
            }
        }
        
        return strlen;
    }
    
    /**
    *
    * @param value java.lang.String
    * @param offset int
    */
   public int putString (
       String value,
       int offset)
   {
       // encoding default is ISO8859_1
       // other encodings are not supported (now).
       
       int ofs=offset+this.ptrOffs;
       char[] ca=value.toCharArray();
       int strlen=ca.length;
       
       for(int i=0; i<strlen; ++i) {
           char c=ca[i];
           if(c <= 255 ) {
               data[ofs++]=(byte) c;
           } else {
               data[ofs++]=63; // '?'
           }
       }
       
       return strlen;
   }
    /**
     *
     * @return int
     * @param value java.lang.String
     * @param offset int
     * @param len int
     */
    public int putString (
        String value,
        int offset,
        int len)
    {
        char[] ca=value.toCharArray();
        int strlen=ca.length;
        if(strlen >= len) {
            int ofs=offset+this.ptrOffs;
            for(int i=0; i<len; ++i) {
                char c=ca[i];
                if(c <= 255 ) {
                    data[ofs + i]=(byte) c;
                } else {
                    data[ofs + i ]=63; // '?'
                }
            }
        } else {
            int ofs=offset+this.ptrOffs;
            for(int i=0; i<strlen; ++i) {
                char c=ca[i];
                if(c <= 255 ) {
                    data[ofs + i]=(byte) c;
                } else {
                    data[ofs + i ]=63; // '?'
                }
            }
            fill(offset + strlen, len - strlen, blankBytes);
        }
        return len;
    }

    /**
     *
     * @return int
     */
    public int size ()
    {
        return this.data.length - this.ptrOffs;
    }
    /**
     *
     */
    private void fill (
        int offset,
        int len,
        byte [] filler)
    {
        int chunkLen;

        while (len > 0) {
            chunkLen = java.lang.Math.min (len, fillBufSizeC);
            System.arraycopy (filler, 0, this.data, offset + this.ptrOffs, chunkLen);
            len -= chunkLen;
            offset += chunkLen;
        }
    }
    /**
     *
     * @param outstream java.io.Writer
     */
    public void traceOn (
        PrintStream outstream)
    {
        traceOn (outstream, 0, this.byteSize);
        return;
    }
    /**
     *
     * @param outstream java.io.Writer
     */
    public void traceOn (
        PrintStream outstream,
        int maxBuf)
    {
        traceOn (outstream, 0, maxBuf);
        return;
    }
    /**
     *
     * @param outstream Writer
     * @param from int
     * @param to int
     */
    public void traceOn (
        PrintStream outstream,
        int from,
        int to)
    {
        outstream.print (StringUtil.hexDump (this.data, from + this.ptrOffs, to + this.ptrOffs));
    }

    public static byte[] getInt4Const(int value)
    {
        byte[] int4bytes=new byte[4];
        for (int i = 3; i >= 0; --i) {
            byte oneByte = (byte) (value & 0xff);
            int4bytes[i] = oneByte;
            value >>= 8;
        }
        return int4bytes;
    }

	/**
	 * @param offset
	 * @return
	 */
	public long getInt8(int offset) {
		byte[] d=this.data;
		int ofs=offset+this.ptrOffs;
		long result=0;
        
		for (int i = 0; i < 8; ++i) {
			int oneByte = d[ofs + i];
			if (oneByte < 0) {
				oneByte += 256;
			}
			result = (result * 256) + oneByte;
		}
        
		return result;	
	}

}