KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > jvm > JPF_java_lang_StringBuffer


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf.jvm;
20
21 import gov.nasa.jpf.jvm.MJIEnv;
22
23 /**
24  * MJI NativePeer class for java.lang.StringBuffer library abstraction
25  */

26 public class JPF_java_lang_StringBuffer {
27   
28   static boolean hasSharedField = false; // Java 1.4 has, 1.5 doesn't
29

30   public static void $clinit (MJIEnv env, int clsObjRef) {
31     // apparently, Java 1.5 has changed the implementation of class
32
// StringBuffer so that it doesn't use the 'shared' state anymore
33
// (which was a performance hack to avoid copying the char array
34
// data when creating String objects from subsequently unmodified
35
// StringBuffers
36
// adding this little extra logic here also serves the purpose of
37
// avoiding a native ObjectStreamClass method which is called during
38
// the static StringBuffer init
39
ClassInfo ci = env.getClassInfo();
40     if (ci.getInstanceField("shared") != null) {
41       hasSharedField = true;
42     }
43   }
44   
45   static int appendString (MJIEnv env, int objref, String JavaDoc s) {
46     int slen = s.length();
47     int aref = env.getReferenceField(objref, "value");
48     int alen = env.getArrayLength(aref);
49     int count = env.getIntField(objref, "count");
50     int i, j;
51     int n = count + slen;
52     
53     if (n < alen) {
54       for (i=count, j=0; i<n; i++, j++) {
55         env.setCharArrayElement(aref, i, s.charAt(j));
56       }
57     } else {
58       int m = 3 * alen / 2;
59       if (m < n) {
60         m = n;
61       }
62       int arefNew = env.newCharArray(m);
63       for (i=0; i<count; i++) {
64         env.setCharArrayElement(arefNew, i, env.getCharArrayElement(aref, i));
65       }
66       for (j=0; i<n; i++, j++) {
67         env.setCharArrayElement(arefNew, i, s.charAt(j));
68       }
69       env.setReferenceField(objref, "value", arefNew);
70     }
71     
72     if (hasSharedField) {
73       env.setBooleanField(objref, "shared", false);
74     }
75     env.setIntField(objref, "count", n);
76     
77     return objref;
78   }
79
80   public static int append__Ljava_lang_StringBuffer_2 (MJIEnv env, int objref, int sbref) {
81     int vref = env.getReferenceField(sbref, "value");
82     int sbCount = env.getIntField(sbref, "count");
83
84     // how braindead, how lazy
85
char[] b = env.getCharArrayObject(vref);
86     String JavaDoc s = new String JavaDoc(b, 0, sbCount);
87     
88     return appendString(env, objref, s);
89   }
90   
91   public static int append__Ljava_lang_String_2 (MJIEnv env, int objref, int sref) {
92     String JavaDoc s = env.getStringObject(sref);
93     
94     return appendString(env, objref, s);
95   }
96   
97   public static int append__I (MJIEnv env, int objref, int i) {
98     String JavaDoc s = Integer.toString(i);
99     
100     return appendString(env, objref, s);
101   }
102
103   public static int append__F (MJIEnv env, int objref, float f) {
104     String JavaDoc s = Float.toString(f);
105     
106     return appendString(env, objref, s);
107   }
108
109   public static int append__D (MJIEnv env, int objref, double d) {
110     String JavaDoc s = Double.toString(d);
111     
112     return appendString(env, objref, s);
113   }
114   
115   public static int append__J (MJIEnv env, int objref, long l) {
116     String JavaDoc s = Long.toString(l);
117     
118     return appendString(env, objref, s);
119   }
120
121   public static int append__Z (MJIEnv env, int objref, boolean b) {
122     String JavaDoc s = b ? "true" : "false";
123     
124     return appendString(env, objref, s);
125   }
126   
127   public static int append__B (MJIEnv env, int objref, byte b) {
128     return append__C(env, objref, (char)b);
129   }
130  
131   public static int append__C (MJIEnv env, int objref, char c) {
132     int aref = env.getReferenceField(objref, "value");
133     int alen = env.getArrayLength(aref);
134     int count = env.getIntField(objref, "count");
135     int i, j;
136     int n = count +1;
137     
138     if (n < alen) {
139       env.setCharArrayElement(aref, count, c);
140     } else {
141       int m = 3 * alen / 2;
142       int arefNew = env.newCharArray(m);
143       for (i=0; i<count; i++) {
144         env.setCharArrayElement(arefNew, i, env.getCharArrayElement(aref, i));
145       }
146       env.setCharArrayElement(arefNew, count, c);
147       env.setReferenceField(objref, "value", arefNew);
148     }
149     
150     if (hasSharedField) {
151       env.setBooleanField(objref, "shared", false);
152     }
153     env.setIntField(objref, "count", n);
154     
155     return objref;
156     
157   }
158 }
159
160
Popular Tags