You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by py...@apache.org on 2006/08/07 08:05:45 UTC
svn commit: r429248 - in
/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio:
Util.java internal/IOUtil.java
Author: pyang
Date: Sun Aug 6 23:05:45 2006
New Revision: 429248
URL: http://svn.apache.org/viewvc?rev=429248&view=rev
Log:
Fix for HARMONY-942 ([classlib][nio] improvment of array bound checks for util classes)
Added:
incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/Util.java
Modified:
incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/IOUtil.java
Added: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/Util.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/Util.java?rev=429248&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/Util.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/Util.java Sun Aug 6 23:05:45 2006
@@ -0,0 +1,141 @@
+/* Copyright 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;
+
+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"));
+ }
+ }
+}
Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/IOUtil.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/IOUtil.java?rev=429248&r1=429247&r2=429248&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/IOUtil.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/IOUtil.java Sun Aug 6 23:05:45 2006
@@ -24,6 +24,7 @@
import java.nio.charset.CoderResult;
import org.apache.harmony.luni.util.Msg;
+import org.apache.harmony.nio.Util;
/*
@@ -84,9 +85,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 +152,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 +178,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);
}