You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ArrayUtil.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.util;
  16. /**
  17. * Utility classes for dealing with arrays.
  18. *
  19. * @author Glen Stampoultzis
  20. */
  21. public class ArrayUtil
  22. {
  23. /**
  24. * This is really a debugging version of <code>System.arraycopy()</code>.
  25. * Use it to provide better exception messages when copying arrays around.
  26. * For production use it's better to use the original for speed.
  27. */
  28. public static void arraycopy(byte[] src, int src_position, byte[] dst, int dst_position, int length)
  29. {
  30. if (src_position < 0)
  31. throw new IllegalArgumentException("src_position was less than 0. Actual value " + src_position);
  32. if (src_position >= src.length)
  33. throw new IllegalArgumentException( "src_position was greater than src array size. Tried to write starting at position " + src_position + " but the array length is " + src.length );
  34. if (src_position + length > src.length)
  35. throw new IllegalArgumentException("src_position + length would overrun the src array. Expected end at " + (src_position + length) + " actual end at " + src.length);
  36. if (dst_position < 0)
  37. throw new IllegalArgumentException("dst_position was less than 0. Actual value " + dst_position);
  38. if (dst_position >= dst.length)
  39. throw new IllegalArgumentException( "dst_position was greater than dst array size. Tried to write starting at position " + dst_position + " but the array length is " + dst.length );
  40. if (dst_position + length > dst.length)
  41. throw new IllegalArgumentException("dst_position + length would overrun the dst array. Expected end at " + (dst_position + length) + " actual end at " + dst.length);
  42. System.arraycopy( src, src_position, dst, dst_position, length);
  43. }
  44. /**
  45. * Moves a number of entries in an array to another point in the array,
  46. * shifting those inbetween as required.
  47. * @param array The array to alter
  48. * @param moveFrom The (0 based) index of the first entry to move
  49. * @param moveTo The (0 based) index of the positition to move to
  50. * @param numToMove The number of entries to move
  51. */
  52. public static void arrayMoveWithin(Object[] array, int moveFrom, int moveTo, int numToMove) {
  53. // If we're not asked to do anything, return now
  54. if(numToMove <= 0) { return; }
  55. if(moveFrom == moveTo) { return; }
  56. // Check that the values supplied are valid
  57. if(moveFrom < 0 || moveFrom >= array.length) {
  58. throw new IllegalArgumentException("The moveFrom must be a valid array index");
  59. }
  60. if(moveTo < 0 || moveTo >= array.length) {
  61. throw new IllegalArgumentException("The moveTo must be a valid array index");
  62. }
  63. if(moveFrom+numToMove > array.length) {
  64. throw new IllegalArgumentException("Asked to move more entries than the array has");
  65. }
  66. if(moveTo+numToMove > array.length) {
  67. throw new IllegalArgumentException("Asked to move to a position that doesn't have enough space");
  68. }
  69. // Grab the bit to move
  70. Object[] toMove = new Object[numToMove];
  71. System.arraycopy(array, moveFrom, toMove, 0, numToMove);
  72. // Grab the bit to be shifted
  73. Object[] toShift;
  74. int shiftTo;
  75. if(moveFrom > moveTo) {
  76. // Moving to an earlier point in the array
  77. // Grab everything between the two points
  78. toShift = new Object[(moveFrom-moveTo)];
  79. System.arraycopy(array, moveTo, toShift, 0, toShift.length);
  80. shiftTo = moveTo + numToMove;
  81. } else {
  82. // Moving to a later point in the array
  83. // Grab everything from after the toMove block, to the new point
  84. toShift = new Object[(moveTo-moveFrom)];
  85. System.arraycopy(array, moveFrom+numToMove, toShift, 0, toShift.length);
  86. shiftTo = moveFrom;
  87. }
  88. // Copy the moved block to its new location
  89. System.arraycopy(toMove, 0, array, moveTo, toMove.length);
  90. // And copy the shifted block to the shifted location
  91. System.arraycopy(toShift, 0, array, shiftTo, toShift.length);
  92. // We're done - array will now have everything moved as required
  93. }
  94. }