View Javadoc

1   // ArrayCopy.java, created Mon Feb  5 23:23:21 2001 by joewhaley
2   // Copyright (C) 2001-3 John Whaley <jwhaley@alum.mit.edu>
3   // Licensed under the terms of the GNU LGPL; see COPYING for details.
4   package joeq.Runtime;
5   
6   /*
7    * @author  John Whaley <jwhaley@alum.mit.edu>
8    * @version $Id: ArrayCopy.java 1457 2004-03-09 22:38:33Z jwhaley $
9    */
10  public abstract class ArrayCopy {
11  
12      public static void arraycopy(Object src, int src_position,
13                                   Object dst, int dst_position,
14                                   int length) {
15          if (dst instanceof char[]) {
16              if (src instanceof char[]) {
17                  ArrayCopy.arraycopy((char[])src, src_position, (char[])dst, dst_position, length);
18                  return;
19              }
20          } else if (dst instanceof Object[]) {
21              if (src instanceof Object[]) {
22                  ArrayCopy.arraycopy((Object[])src, src_position, (Object[])dst, dst_position, length);
23                  return;
24              }
25          } else if (dst instanceof byte[]) {
26              if (src instanceof byte[]) {
27                  ArrayCopy.arraycopy((byte[])src, src_position, (byte[])dst, dst_position, length);
28                  return;
29              }
30          } else if (dst instanceof short[]) {
31              if (src instanceof short[]) {
32                  ArrayCopy.arraycopy((short[])src, src_position, (short[])dst, dst_position, length);
33                  return;
34              }
35          } else if (dst instanceof int[]) {
36              if (src instanceof int[]) {
37                  ArrayCopy.arraycopy((int[])src, src_position, (int[])dst, dst_position, length);
38                  return;
39              }
40          } else if (dst instanceof float[]) {
41              if (src instanceof float[]) {
42                  ArrayCopy.arraycopy((float[])src, src_position, (float[])dst, dst_position, length);
43                  return;
44              }
45          } else if (dst instanceof long[]) {
46              if (src instanceof long[]) {
47                  ArrayCopy.arraycopy((long[])src, src_position, (long[])dst, dst_position, length);
48                  return;
49              }
50          } else if (dst instanceof double[]) {
51              if (src instanceof double[]) {
52                  ArrayCopy.arraycopy((double[])src, src_position, (double[])dst, dst_position, length);
53                  return;
54              }
55          }
56          throw new ArrayStoreException("destination array type "+dst.getClass()+" does not match source array type "+src.getClass());
57      }
58      
59      public static void arraycopy(Object[] src, int src_position,
60                                   Object[] dst, int dst_position,
61                                   int length) {
62          if ((src_position < 0) || (dst_position < 0) || (length < 0) ||
63              (src_position+length > src.length) || (dst_position+length > dst.length))
64              throw new ArrayIndexOutOfBoundsException();
65          if ((src == dst) && (src_position < dst_position) && (src_position+length > dst_position)) {
66              // overlapping case
67              for (int i=length-1; i>=0; --i) {
68                  dst[dst_position+i] = src[src_position+i];
69              }
70              return;
71          }
72          for (int i=0; i<length; ++i) {
73              dst[dst_position+i] = src[src_position+i];
74          }
75      }
76      public static void arraycopy(byte[] src, int src_position,
77                                   byte[] dst, int dst_position,
78                                   int length) {
79          if ((src_position < 0) || (dst_position < 0) || (length < 0) ||
80              (src_position+length > src.length) || (dst_position+length > dst.length))
81              throw new ArrayIndexOutOfBoundsException();
82          if ((src == dst) && (src_position < dst_position) && (src_position+length > dst_position)) {
83              // overlapping case
84              for (int i=length-1; i>=0; --i) {
85                  dst[dst_position+i] = src[src_position+i];
86              }
87              return;
88          }
89          for (int i=0; i<length; ++i) {
90              dst[dst_position+i] = src[src_position+i];
91          }
92      }
93      public static void arraycopy(char[] src, int src_position,
94                                   char[] dst, int dst_position,
95                                   int length) {
96          if ((src_position < 0) || (dst_position < 0) || (length < 0) ||
97              (src_position+length > src.length) || (dst_position+length > dst.length))
98              throw new ArrayIndexOutOfBoundsException();
99          if ((src == dst) && (src_position < dst_position) && (src_position+length > dst_position)) {
100             // overlapping case
101             for (int i=length-1; i>=0; --i) {
102                 dst[dst_position+i] = src[src_position+i];
103             }
104             return;
105         }
106         for (int i=0; i<length; ++i) {
107             dst[dst_position+i] = src[src_position+i];
108         }
109     }
110     public static void arraycopy(short[] src, int src_position,
111                                  short[] dst, int dst_position,
112                                  int length) {
113         if ((src_position < 0) || (dst_position < 0) || (length < 0) ||
114             (src_position+length > src.length) || (dst_position+length > dst.length))
115             throw new ArrayIndexOutOfBoundsException();
116         if ((src == dst) && (src_position < dst_position) && (src_position+length > dst_position)) {
117             // overlapping case
118             for (int i=length-1; i>=0; --i) {
119                 dst[dst_position+i] = src[src_position+i];
120             }
121             return;
122         }
123         for (int i=0; i<length; ++i) {
124             dst[dst_position+i] = src[src_position+i];
125         }
126     }
127     public static void arraycopy(int[] src, int src_position,
128                                  int[] dst, int dst_position,
129                                  int length) {
130         if ((src_position < 0) || (dst_position < 0) || (length < 0) ||
131             (src_position+length > src.length) || (dst_position+length > dst.length))
132             throw new ArrayIndexOutOfBoundsException();
133         if ((src == dst) && (src_position < dst_position) && (src_position+length > dst_position)) {
134             // overlapping case
135             for (int i=length-1; i>=0; --i) {
136                 dst[dst_position+i] = src[src_position+i];
137             }
138             return;
139         }
140         for (int i=0; i<length; ++i) {
141             dst[dst_position+i] = src[src_position+i];
142         }
143     }
144     public static void arraycopy(float[] src, int src_position,
145                                  float[] dst, int dst_position,
146                                  int length) {
147         if ((src_position < 0) || (dst_position < 0) || (length < 0) ||
148             (src_position+length > src.length) || (dst_position+length > dst.length))
149             throw new ArrayIndexOutOfBoundsException();
150         if ((src == dst) && (src_position < dst_position) && (src_position+length > dst_position)) {
151             // overlapping case
152             for (int i=length-1; i>=0; --i) {
153                 dst[dst_position+i] = src[src_position+i];
154             }
155             return;
156         }
157         for (int i=0; i<length; ++i) {
158             dst[dst_position+i] = src[src_position+i];
159         }
160     }
161     public static void arraycopy(long[] src, int src_position,
162                                  long[] dst, int dst_position,
163                                  int length) {
164         if ((src_position < 0) || (dst_position < 0) || (length < 0) ||
165             (src_position+length > src.length) || (dst_position+length > dst.length))
166             throw new ArrayIndexOutOfBoundsException();
167         if ((src == dst) && (src_position < dst_position) && (src_position+length > dst_position)) {
168             // overlapping case
169             for (int i=length-1; i>=0; --i) {
170                 dst[dst_position+i] = src[src_position+i];
171             }
172             return;
173         }
174         for (int i=0; i<length; ++i) {
175             dst[dst_position+i] = src[src_position+i];
176         }
177     }
178     public static void arraycopy(double[] src, int src_position,
179                                  double[] dst, int dst_position,
180                                  int length) {
181         if ((src_position < 0) || (dst_position < 0) || (length < 0) ||
182             (src_position+length > src.length) || (dst_position+length > dst.length))
183             throw new ArrayIndexOutOfBoundsException();
184         if ((src == dst) && (src_position < dst_position) && (src_position+length > dst_position)) {
185             // overlapping case
186             for (int i=length-1; i>=0; --i) {
187                 dst[dst_position+i] = src[src_position+i];
188             }
189             return;
190         }
191         for (int i=0; i<length; ++i) {
192             dst[dst_position+i] = src[src_position+i];
193         }
194     }
195     
196 }