KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > util > ClassFileStruct


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core.util;
12
13 /**
14  * Abstract class that defines helpers methods for decoding .class file.
15  */

16 public abstract class ClassFileStruct {
17
18     protected double doubleAt(byte[] reference, int relativeOffset, int structOffset) {
19         return (Double.longBitsToDouble(this.i8At(reference, relativeOffset, structOffset)));
20     }
21
22     protected float floatAt(byte[] reference, int relativeOffset, int structOffset) {
23         return (Float.intBitsToFloat(this.i4At(reference, relativeOffset, structOffset)));
24     }
25     protected int i1At(byte[] reference, int relativeOffset, int structOffset) {
26         return reference[relativeOffset + structOffset];
27     }
28     protected int i2At(byte[] reference, int relativeOffset, int structOffset) {
29         int position = relativeOffset + structOffset;
30         return (reference[position++] << 8) + (reference[position] & 0xFF);
31     }
32     protected int i4At(byte[] reference, int relativeOffset, int structOffset) {
33         int position = relativeOffset + structOffset;
34         return ((reference[position++] & 0xFF) << 24)
35             + ((reference[position++] & 0xFF) << 16)
36             + ((reference[position++] & 0xFF) << 8)
37             + (reference[position] & 0xFF);
38     }
39     protected long i8At(byte[] reference, int relativeOffset, int structOffset) {
40         int position = relativeOffset + structOffset;
41         return (((long) (reference[position++] & 0xFF)) << 56)
42                         + (((long) (reference[position++] & 0xFF)) << 48)
43                         + (((long) (reference[position++] & 0xFF)) << 40)
44                         + (((long) (reference[position++] & 0xFF)) << 32)
45                         + (((long) (reference[position++] & 0xFF)) << 24)
46                         + (((long) (reference[position++] & 0xFF)) << 16)
47                         + (((long) (reference[position++] & 0xFF)) << 8)
48                         + (reference[position++] & 0xFF);
49     }
50
51     protected int u1At(byte[] reference, int relativeOffset, int structOffset) {
52         return (reference[relativeOffset + structOffset] & 0xFF);
53     }
54     protected int u2At(byte[] reference, int relativeOffset, int structOffset) {
55         int position = relativeOffset + structOffset;
56         return ((reference[position++] & 0xFF) << 8) + (reference[position] & 0xFF);
57     }
58     protected long u4At(byte[] reference, int relativeOffset, int structOffset) {
59         int position = relativeOffset + structOffset;
60         return (
61             ((reference[position++] & 0xFFL) << 24)
62                 + ((reference[position++] & 0xFF) << 16)
63                 + ((reference[position++] & 0xFF) << 8)
64                 + (reference[position] & 0xFF));
65     }
66     protected char[] utf8At(byte[] reference, int relativeOffset, int structOffset, int bytesAvailable) {
67         int length = bytesAvailable;
68         char outputBuf[] = new char[bytesAvailable];
69         int outputPos = 0;
70         int readOffset = structOffset + relativeOffset;
71
72         while (length != 0) {
73             int x = reference[readOffset++] & 0xFF;
74             length--;
75             if ((0x80 & x) != 0) {
76                 if ((x & 0x20) != 0) {
77                     length-=2;
78                     x = ((x & 0xF) << 12) + ((reference[readOffset++] & 0x3F) << 6) + (reference[readOffset++] & 0x3F);
79                 } else {
80                     length--;
81                     x = ((x & 0x1F) << 6) + (reference[readOffset++] & 0x3F);
82                 }
83             }
84             outputBuf[outputPos++] = (char) x;
85         }
86
87         if (outputPos != bytesAvailable) {
88             System.arraycopy(outputBuf, 0, (outputBuf = new char[outputPos]), 0, outputPos);
89         }
90         return outputBuf;
91     }
92
93     final boolean equals(char[] first, char[] second) {
94         if (first == second)
95             return true;
96         if (first == null || second == null)
97             return false;
98         if (first.length != second.length)
99             return false;
100     
101         for (int i = first.length; --i >= 0;)
102             if (first[i] != second[i])
103                 return false;
104         return true;
105     }
106 }
107
Popular Tags