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 |
|
|
1112 |
|
if ((size = this.size) > a.length) |
1113 |
|
return toArray((Class<T[]>) a.getClass()); |
1114 |
|
final Object[] es = elements; |
1115 |
< |
final int head = this.head, end = head + size; |
1116 |
< |
final int front = (es.length - end >= 0) ? size : es.length - head; |
1117 |
< |
System.arraycopy(es, head, a, 0, front); |
1118 |
< |
if (front < size) |
1119 |
< |
System.arraycopy(es, 0, a, front, size - front); |
1115 |
> |
int i, j, len, todo; |
1116 |
> |
todo = size - (len = Math.min(size, es.length - (i = head))); |
1117 |
> |
for (j = 0;; j += len, len = todo, todo = 0, i = 0) { |
1118 |
> |
System.arraycopy(es, i, a, j, len); |
1119 |
> |
if (todo == 0) break; |
1120 |
> |
} |
1121 |
|
if (size < a.length) |
1122 |
|
a[size] = null; |
1123 |
|
return a; |