Index: trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/IOUtil.java =================================================================== --- trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/IOUtil.java (revision 424155) +++ trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/IOUtil.java (working copy) @@ -84,9 +84,7 @@ if (length == 0) { return 0; } - if (offset < 0 || length < 0 || offset + length > buf.length) { - throw new IndexOutOfBoundsException(); - } + Util.assertArrayIndex(buf, offset, length); // read at least once if (chars.limit() == chars.position()) { fillBuf(in, bytes, chars, decoder); @@ -153,10 +151,7 @@ public static void writeOutputStreamWriter(String str, int offset, int count, OutputStream out, ByteBuffer bytes, CharsetEncoder encoder, Object lock) throws IOException { - // avoid int overflow - if (offset < 0 || count < 0 || offset + count > str.length()) { - throw new StringIndexOutOfBoundsException(); - } + Util.assertArrayIndex(str.length(), offset, count); CharBuffer chars = CharBuffer.wrap(str, offset, count + offset); convert(lock, encoder, bytes, chars, out); } @@ -182,9 +177,7 @@ public static void writeOutputStreamWriter(char[] buf, int offset, int count, OutputStream out, ByteBuffer bytes, CharsetEncoder encoder, Object lock) throws IOException { - if (offset < 0 || count < 0 || offset + count > buf.length) { - throw new ArrayIndexOutOfBoundsException(); - } + Util.assertArrayIndex(buf, offset, count); CharBuffer chars = CharBuffer.wrap(buf, offset, count); convert(lock, encoder, bytes, chars, out); } Index: trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/Util.java =================================================================== --- trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/Util.java (revision 0) +++ trunk/modules/nio/src/main/java/org/apache/harmony/nio/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.nio.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 IndexOutOfBoundsException for negative index, + * NullPointerException for null array, IndexOutOfBoundsException for offset+length > array.length + */ + public static void assertArrayIndex(Object[] array, int offset, int length) { + if (offset < 0 || length < 0) { + throw new IndexOutOfBoundsException(Msg.getString("K0006")); + } + if ((long)offset + (long)length > array.length) { + throw new IndexOutOfBoundsException(Msg.getString("K00ae")); + } + } + public static void assertArrayIndex(boolean[] array, int offset, int length) { + if (offset < 0 || length < 0) { + throw new IndexOutOfBoundsException(Msg.getString("K0006")); + } + if ((long)offset + (long)length > array.length) { + throw new IndexOutOfBoundsException(Msg.getString("K00ae")); + } + } + public static void assertArrayIndex(byte[] array, int offset, int length) { + if (offset < 0 || length < 0) { + throw new IndexOutOfBoundsException(Msg.getString("K0006")); + } + if ((long)offset + (long)length > array.length) { + throw new IndexOutOfBoundsException(Msg.getString("K00ae")); + } + } + public static void assertArrayIndex(short[] array, int offset, int length) { + if (offset < 0 || length < 0) { + throw new IndexOutOfBoundsException(Msg.getString("K0006")); + } + if ((long)offset + (long)length > array.length) { + throw new IndexOutOfBoundsException(Msg.getString("K00ae")); + } + } + public static void assertArrayIndex(int[] array, int offset, int length) { + if (offset < 0 || length < 0) { + throw new IndexOutOfBoundsException(Msg.getString("K0006")); + } + if ((long)offset + (long)length > array.length) { + throw new IndexOutOfBoundsException(Msg.getString("K00ae")); + } + } + public static void assertArrayIndex(long[] array, int offset, int length) { + if (offset < 0 || length < 0) { + throw new IndexOutOfBoundsException(Msg.getString("K0006")); + } + if ((long)offset + (long)length > array.length) { + throw new IndexOutOfBoundsException(Msg.getString("K00ae")); + } + } + public static void assertArrayIndex(float[] array, int offset, int length) { + if (offset < 0 || length < 0) { + throw new IndexOutOfBoundsException(Msg.getString("K0006")); + } + if ((long)offset + (long)length > array.length) { + throw new IndexOutOfBoundsException(Msg.getString("K00ae")); + } + } + public static void assertArrayIndex(double[] array, int offset, int length) { + if (offset < 0 || length < 0) { + throw new IndexOutOfBoundsException(Msg.getString("K0006")); + } + if ((long)offset + (long)length > array.length) { + throw new IndexOutOfBoundsException(Msg.getString("K00ae")); + } + } + public static void assertArrayIndex(char[] array, int offset, int length) { + if (offset < 0 || length < 0) { + throw new IndexOutOfBoundsException(Msg.getString("K0006")); + } + if ((long)offset + (long)length > array.length) { + throw new IndexOutOfBoundsException(Msg.getString("K00ae")); + } + } + + /* + * Check array bounds method for methods like doSomething(Object[], offset, length). + * Exception throws order is IndexOutOfBoundsException for negative index, + * IndexOutOfBoundsException for offset+length > array.length + */ + public static void assertArrayIndex(int arrayLength, int offset, int length) { + if (offset < 0 || length < 0) { + throw new IndexOutOfBoundsException(Msg.getString("K0006")); + } + if ((long)offset + (long)length > arrayLength) { + throw new IndexOutOfBoundsException(Msg.getString("K00ae")); + } + } +}