Lines 106-194
Link Here
|
106 |
// We're done - array will now have everything moved as required |
106 |
// We're done - array will now have everything moved as required |
107 |
} |
107 |
} |
108 |
|
108 |
|
109 |
/** |
|
|
110 |
* Copies the specified array, truncating or padding with zeros (if |
111 |
* necessary) so the copy has the specified length. This method is temporary |
112 |
* replace for Arrays.copyOf() until we start to require JDK 1.6. |
113 |
* |
114 |
* @param source |
115 |
* the array to be copied |
116 |
* @param newLength |
117 |
* the length of the copy to be returned |
118 |
* @return a copy of the original array, truncated or padded with zeros to |
119 |
* obtain the specified length |
120 |
* @throws NegativeArraySizeException |
121 |
* if <tt>newLength</tt> is negative |
122 |
* @throws NullPointerException |
123 |
* if <tt>original</tt> is null |
124 |
*/ |
125 |
public static byte[] copyOf( byte[] source, int newLength ) |
126 |
{ |
127 |
byte[] result = new byte[newLength]; |
128 |
System.arraycopy( source, 0, result, 0, |
129 |
Math.min( source.length, newLength ) ); |
130 |
return result; |
131 |
} |
132 |
|
133 |
/** |
134 |
* Copies the specified array into specified result array, truncating or |
135 |
* padding with zeros (if necessary) so the copy has the specified length. |
136 |
* This method is temporary replace for Arrays.copyOf() until we start to |
137 |
* require JDK 1.6. |
138 |
* |
139 |
* @param source |
140 |
* the array to be copied |
141 |
* @param result |
142 |
* the array to be filled and returned |
143 |
* @throws NegativeArraySizeException |
144 |
* if <tt>newLength</tt> is negative |
145 |
* @throws NullPointerException |
146 |
* if <tt>original</tt> is null |
147 |
*/ |
148 |
public static <T> T[] copyOf( T[] source, T[] result ) |
149 |
{ |
150 |
System.arraycopy( source, 0, result, 0, |
151 |
Math.min( source.length, result.length ) ); |
152 |
return result; |
153 |
} |
154 |
|
155 |
/** |
156 |
* Copies the specified range of the specified array into a new array. |
157 |
* The initial index of the range (<tt>from</tt>) must lie between zero |
158 |
* and <tt>original.length</tt>, inclusive. The value at |
159 |
* <tt>original[from]</tt> is placed into the initial element of the copy |
160 |
* (unless <tt>from == original.length</tt> or <tt>from == to</tt>). |
161 |
* Values from subsequent elements in the original array are placed into |
162 |
* subsequent elements in the copy. The final index of the range |
163 |
* (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, |
164 |
* may be greater than <tt>original.length</tt>, in which case |
165 |
* <tt>(byte)0</tt> is placed in all elements of the copy whose index is |
166 |
* greater than or equal to <tt>original.length - from</tt>. The length |
167 |
* of the returned array will be <tt>to - from</tt>. |
168 |
* |
169 |
* This method is temporary |
170 |
* replace for Arrays.copyOfRange() until we start to require JDK 1.6. |
171 |
* |
172 |
* @param original the array from which a range is to be copied |
173 |
* @param from the initial index of the range to be copied, inclusive |
174 |
* @param to the final index of the range to be copied, exclusive. |
175 |
* (This index may lie outside the array.) |
176 |
* @return a new array containing the specified range from the original array, |
177 |
* truncated or padded with zeros to obtain the required length |
178 |
* @throws ArrayIndexOutOfBoundsException if <tt>from < 0</tt> |
179 |
* or <tt>from > original.length()</tt> |
180 |
* @throws IllegalArgumentException if <tt>from > to</tt> |
181 |
* @throws NullPointerException if <tt>original</tt> is null |
182 |
* @since 1.6 |
183 |
*/ |
184 |
public static byte[] copyOfRange(byte[] original, int from, int to) { |
185 |
int newLength = to - from; |
186 |
if (newLength < 0) |
187 |
throw new IllegalArgumentException(from + " > " + to); |
188 |
byte[] copy = new byte[newLength]; |
189 |
System.arraycopy(original, from, copy, 0, |
190 |
Math.min(original.length - from, newLength)); |
191 |
return copy; |
192 |
} |
193 |
|
194 |
} |
109 |
} |