1055 |
|
|
1056 |
|
private <T> T[] toArray(Class<T[]> klazz) { |
1057 |
|
final Object[] es = elements; |
1058 |
– |
final int capacity = es.length; |
1059 |
– |
final int head = this.head, end = head + size; |
1058 |
|
final T[] a; |
1059 |
< |
if (end >= 0) { |
1059 |
> |
final int head, len, end, todo; |
1060 |
> |
todo = size - (len = Math.min(size, es.length - (head = this.head))); |
1061 |
> |
if ((end = head + size) >= 0) { |
1062 |
|
a = Arrays.copyOfRange(es, head, end, klazz); |
1063 |
|
} else { |
1064 |
|
// integer overflow! |
1065 |
|
a = Arrays.copyOfRange(es, 0, size, klazz); |
1066 |
< |
System.arraycopy(es, head, a, 0, capacity - head); |
1066 |
> |
System.arraycopy(es, head, a, 0, len); |
1067 |
|
} |
1068 |
< |
if (end - capacity > 0) |
1069 |
< |
System.arraycopy(es, 0, a, capacity - head, end - capacity); |
1068 |
> |
if (todo > 0) |
1069 |
> |
System.arraycopy(es, 0, a, len, todo); |
1070 |
|
return a; |
1071 |
|
} |
1072 |
|
|