Index: trunk/modules/text/src/main/java/java/text/BreakIterator.java =================================================================== --- trunk/modules/text/src/main/java/java/text/BreakIterator.java (revision 424155) +++ trunk/modules/text/src/main/java/java/text/BreakIterator.java (working copy) @@ -16,6 +16,8 @@ package java.text; import java.util.Locale; +import org.apache.harmony.text.internal.Util; + /** * This class is used to locate the boundaries of text. Instance of this class @@ -393,9 +395,7 @@ if(null == buf){ throw new NullPointerException(); } - if(offset < 0 || offset + LONG_LENGTH > buf.length){ - throw new ArrayIndexOutOfBoundsException(); - } + Util.assertArrayIndex(buf, offset, LONG_LENGTH); long result = 0; for (int i = offset; i < offset + LONG_LENGTH; i++) { result = (result << 8) | (buf[i] & 0xff); @@ -414,9 +414,7 @@ if(null == buf){ throw new NullPointerException(); } - if(offset < 0 || offset + INT_LENGTH > buf.length){ - throw new ArrayIndexOutOfBoundsException(); - } + Util.assertArrayIndex(buf, offset, INT_LENGTH); int result = 0; for (int i = offset; i < offset + INT_LENGTH; i++) { result = (result << 8) | (buf[i] & 0xff); @@ -435,9 +433,7 @@ if(null == buf){ throw new NullPointerException(); } - if(offset < 0 || offset + SHORT_LENGTH > buf.length){ - throw new ArrayIndexOutOfBoundsException(); - } + Util.assertArrayIndex(buf, offset, SHORT_LENGTH); short result = 0; for (int i = offset; i < offset + SHORT_LENGTH; i++) { result = (short)((result << 8) | (buf[i] & 0xff)); Index: trunk/modules/text/src/test/java/org/apache/harmony/text/tests/java/text/BreakIteratorTest.java =================================================================== --- trunk/modules/text/src/test/java/org/apache/harmony/text/tests/java/text/BreakIteratorTest.java (revision 424155) +++ trunk/modules/text/src/test/java/org/apache/harmony/text/tests/java/text/BreakIteratorTest.java (working copy) @@ -309,6 +309,11 @@ fail("should throw ArrayIndexOutOfBoundsException."); } catch (ArrayIndexOutOfBoundsException e) { } + try { + MockBreakIterator.publicGetShort(new byte[] { 0, 0 }, Integer.MAX_VALUE); + fail("should throw ArrayIndexOutOfBoundsException."); + } catch (ArrayIndexOutOfBoundsException e) { + } assertEquals(0, MockBreakIterator.publicGetShort(new byte[] { 0, 0 }, 0)); assertEquals(1, MockBreakIterator.publicGetShort(new byte[] { 0, 1 }, 0)); assertEquals(-1, MockBreakIterator.publicGetShort(new byte[] { (byte)0xff, (byte)0xff }, 0)); @@ -339,6 +344,11 @@ fail("should throw ArrayIndexOutOfBoundsException."); } catch (ArrayIndexOutOfBoundsException e) { } + try { + MockBreakIterator.publicGetInt(new byte[] { 0, 0, 0, 0}, Integer.MAX_VALUE); + fail("should throw ArrayIndexOutOfBoundsException."); + } catch (ArrayIndexOutOfBoundsException e) { + } assertEquals(0, MockBreakIterator.publicGetInt(new byte[] { 0, 0, 0, 0 }, 0)); assertEquals(1, MockBreakIterator.publicGetInt(new byte[] { 0, 0, 0, 1 }, 0)); assertEquals(-1, MockBreakIterator.publicGetInt(new byte[] { (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff }, 0)); @@ -369,6 +379,11 @@ fail("should throw ArrayIndexOutOfBoundsException."); } catch (ArrayIndexOutOfBoundsException e) { } + try { + MockBreakIterator.publicGetLong(new byte[] { 0, 0, 0, 0, 0, 0, 1, 1}, Integer.MAX_VALUE); + fail("should throw ArrayIndexOutOfBoundsException."); + } catch (ArrayIndexOutOfBoundsException e) { + } assertEquals(0, MockBreakIterator.publicGetLong(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, 0)); assertEquals(1, MockBreakIterator.publicGetLong(new byte[] { 0, 0, 0, 0, 0, 0, 0, 1 }, 0)); assertEquals(-1, MockBreakIterator.publicGetLong(new byte[] { (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff }, 0)); Index: trunk/modules/text/src/main/java/org/apache/harmony/text/internal/Util.java =================================================================== --- trunk/modules/text/src/main/java/org/apache/harmony/text/internal/Util.java (revision 0) +++ trunk/modules/text/src/main/java/org/apache/harmony/text/internal/Util.java (revision 0) @@ -0,0 +1,132 @@ +/* Copyright 2005, 2006 The Apache Software Foundation or its licensors, as applicable + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.harmony.text.internal; + +import org.apache.harmony.luni.util.Msg; + + +/* + * Static methods. Used by io and nio packages. + * + */ +public final class Util { + + // ------------------------------------------------------------------- + // Constructor + // ------------------------------------------------------------------- + + /* + * No instance. + */ + private Util() { + super(); + } + + // ------------------------------------------------------------------- + // Routine methods. + // ------------------------------------------------------------------- + + /* + * Check array bounds method for methods like doSomething(Object[], offset, length). + * Exception throws order is ArrayIndexOutOfBoundsException for negative index, + * NullPointerException for null array, ArrayIndexOutOfBoundsException for offset+length > array.length + */ + public static void assertArrayIndex(Object[] array, int offset, int length) { + if (offset < 0 || length < 0) { + throw new ArrayIndexOutOfBoundsException(Msg.getString("K0006")); + } + if ((long)offset + (long)length > array.length) { + throw new ArrayIndexOutOfBoundsException(Msg.getString("K00ae")); + } + } + public static void assertArrayIndex(boolean[] array, int offset, int length) { + if (offset < 0 || length < 0) { + throw new ArrayIndexOutOfBoundsException(Msg.getString("K0006")); + } + if ((long)offset + (long)length > array.length) { + throw new ArrayIndexOutOfBoundsException(Msg.getString("K00ae")); + } + } + public static void assertArrayIndex(byte[] array, int offset, int length) { + if (offset < 0 || length < 0) { + throw new ArrayIndexOutOfBoundsException(Msg.getString("K0006")); + } + if ((long)offset + (long)length > array.length) { + throw new ArrayIndexOutOfBoundsException(Msg.getString("K00ae")); + } + } + public static void assertArrayIndex(short[] array, int offset, int length) { + if (offset < 0 || length < 0) { + throw new ArrayIndexOutOfBoundsException(Msg.getString("K0006")); + } + if ((long)offset + (long)length > array.length) { + throw new ArrayIndexOutOfBoundsException(Msg.getString("K00ae")); + } + } + public static void assertArrayIndex(int[] array, int offset, int length) { + if (offset < 0 || length < 0) { + throw new ArrayIndexOutOfBoundsException(Msg.getString("K0006")); + } + if ((long)offset + (long)length > array.length) { + throw new ArrayIndexOutOfBoundsException(Msg.getString("K00ae")); + } + } + public static void assertArrayIndex(long[] array, int offset, int length) { + if (offset < 0 || length < 0) { + throw new ArrayIndexOutOfBoundsException(Msg.getString("K0006")); + } + if ((long)offset + (long)length > array.length) { + throw new ArrayIndexOutOfBoundsException(Msg.getString("K00ae")); + } + } + public static void assertArrayIndex(float[] array, int offset, int length) { + if (offset < 0 || length < 0) { + throw new ArrayIndexOutOfBoundsException(Msg.getString("K0006")); + } + if ((long)offset + (long)length > array.length) { + throw new ArrayIndexOutOfBoundsException(Msg.getString("K00ae")); + } + } + public static void assertArrayIndex(double[] array, int offset, int length) { + if (offset < 0 || length < 0) { + throw new ArrayIndexOutOfBoundsException(Msg.getString("K0006")); + } + if ((long)offset + (long)length > array.length) { + throw new ArrayIndexOutOfBoundsException(Msg.getString("K00ae")); + } + } + public static void assertArrayIndex(char[] array, int offset, int length) { + if (offset < 0 || length < 0) { + throw new ArrayIndexOutOfBoundsException(Msg.getString("K0006")); + } + if ((long)offset + (long)length > array.length) { + throw new ArrayIndexOutOfBoundsException(Msg.getString("K00ae")); + } + } + + /* + * Check array bounds method for methods like doSomething(Object[], offset, length). + * Exception throws order is ArrayIndexOutOfBoundsException for negative index, + * ArrayIndexOutOfBoundsException for offset+length > array.length + */ + public static void assertArrayIndex(int arrayLength, int offset, int length) { + if (offset < 0 || length < 0) { + throw new ArrayIndexOutOfBoundsException(Msg.getString("K0006")); + } + if ((long)offset + (long)length > arrayLength) { + throw new ArrayIndexOutOfBoundsException(Msg.getString("K00ae")); + } + } +} Index: trunk/modules/text/make/patternset.txt =================================================================== --- trunk/modules/text/make/patternset.txt (revision 424155) +++ trunk/modules/text/make/patternset.txt (working copy) @@ -14,3 +14,4 @@ java/text/* org/apache/harmony/text/* +org/apache/harmony/text/internal/*