001: /* Licensed to the Apache Software Foundation (ASF) under one or more
002: * contributor license agreements. See the NOTICE file distributed with
003: * this work for additional information regarding copyright ownership.
004: * The ASF licenses this file to You under the Apache License, Version 2.0
005: * (the "License"); you may not use this file except in compliance with
006: * the License. You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.harmony.nio;
018:
019: import org.apache.harmony.nio.internal.nls.Messages;
020:
021: /*
022: * Static methods. Used by io and nio packages.
023: *
024: */
025: public final class Util {
026:
027: // -------------------------------------------------------------------
028: // Constructor
029: // -------------------------------------------------------------------
030:
031: /*
032: * No instance.
033: */
034: private Util() {
035: super ();
036: }
037:
038: // -------------------------------------------------------------------
039: // Routine methods.
040: // -------------------------------------------------------------------
041:
042: /*
043: * Check array bounds method for methods like doSomething(Object[], offset,
044: * length). Exception throws order is IndexOutOfBoundsException for negative
045: * index, NullPointerException for null array, IndexOutOfBoundsException for
046: * offset+length > array.length
047: */
048: public static void assertArrayIndex(Object[] array, int offset,
049: int length) {
050: if (offset < 0 || length < 0) {
051: // nio.05=Negative index specified
052: throw new IndexOutOfBoundsException(Messages
053: .getString("nio.05")); //$NON-NLS-1$
054: }
055: if ((long) offset + (long) length > array.length) {
056: // nio.04=Size mismatch
057: throw new IndexOutOfBoundsException(Messages
058: .getString("nio.04")); //$NON-NLS-1$
059: }
060: }
061:
062: public static void assertArrayIndex(boolean[] array, int offset,
063: int length) {
064: if (offset < 0 || length < 0) {
065: // nio.05=Negative index specified
066: throw new IndexOutOfBoundsException(Messages
067: .getString("nio.05")); //$NON-NLS-1$
068: }
069: if ((long) offset + (long) length > array.length) {
070: // nio.04=Size mismatch
071: throw new IndexOutOfBoundsException(Messages
072: .getString("nio.04")); //$NON-NLS-1$
073: }
074: }
075:
076: public static void assertArrayIndex(byte[] array, int offset,
077: int length) {
078: if (offset < 0 || length < 0) {
079: // nio.05=Negative index specified
080: throw new IndexOutOfBoundsException(Messages
081: .getString("nio.05")); //$NON-NLS-1$
082: }
083: if ((long) offset + (long) length > array.length) {
084: // nio.04=Size mismatch
085: throw new IndexOutOfBoundsException(Messages
086: .getString("nio.04")); //$NON-NLS-1$
087: }
088: }
089:
090: public static void assertArrayIndex(short[] array, int offset,
091: int length) {
092: if (offset < 0 || length < 0) {
093: // nio.05=Negative index specified
094: throw new IndexOutOfBoundsException(Messages
095: .getString("nio.05")); //$NON-NLS-1$
096: }
097: if ((long) offset + (long) length > array.length) {
098: // nio.04=Size mismatch
099: throw new IndexOutOfBoundsException(Messages
100: .getString("nio.04")); //$NON-NLS-1$
101: }
102: }
103:
104: public static void assertArrayIndex(int[] array, int offset,
105: int length) {
106: if (offset < 0 || length < 0) {
107: // nio.05=Negative index specified
108: throw new IndexOutOfBoundsException(Messages
109: .getString("nio.05")); //$NON-NLS-1$
110: }
111: if ((long) offset + (long) length > array.length) {
112: // nio.04=Size mismatch
113: throw new IndexOutOfBoundsException(Messages
114: .getString("nio.04")); //$NON-NLS-1$
115: }
116: }
117:
118: public static void assertArrayIndex(long[] array, int offset,
119: int length) {
120: if (offset < 0 || length < 0) {
121: // nio.05=Negative index specified
122: throw new IndexOutOfBoundsException(Messages
123: .getString("nio.05")); //$NON-NLS-1$
124: }
125: if ((long) offset + (long) length > array.length) {
126: // nio.04=Size mismatch
127: throw new IndexOutOfBoundsException(Messages
128: .getString("nio.04")); //$NON-NLS-1$
129: }
130: }
131:
132: public static void assertArrayIndex(float[] array, int offset,
133: int length) {
134: if (offset < 0 || length < 0) {
135: // nio.05=Negative index specified
136: throw new IndexOutOfBoundsException(Messages
137: .getString("nio.05")); //$NON-NLS-1$
138: }
139: if ((long) offset + (long) length > array.length) {
140: // nio.04=Size mismatch
141: throw new IndexOutOfBoundsException(Messages
142: .getString("nio.04")); //$NON-NLS-1$
143: }
144: }
145:
146: public static void assertArrayIndex(double[] array, int offset,
147: int length) {
148: if (offset < 0 || length < 0) {
149: // nio.05=Negative index specified
150: throw new IndexOutOfBoundsException(Messages
151: .getString("nio.05")); //$NON-NLS-1$
152: }
153: if ((long) offset + (long) length > array.length) {
154: // nio.04=Size mismatch
155: throw new IndexOutOfBoundsException(Messages
156: .getString("nio.04")); //$NON-NLS-1$
157: }
158: }
159:
160: public static void assertArrayIndex(char[] array, int offset,
161: int length) {
162: if (offset < 0 || length < 0) {
163: // nio.05=Negative index specified
164: throw new IndexOutOfBoundsException(Messages
165: .getString("nio.05")); //$NON-NLS-1$
166: }
167: if ((long) offset + (long) length > array.length) {
168: // nio.04=Size mismatch
169: throw new IndexOutOfBoundsException(Messages
170: .getString("nio.04")); //$NON-NLS-1$
171: }
172: }
173:
174: /*
175: * Check array bounds method for methods like doSomething(Object[], offset,
176: * length). Exception throws order is IndexOutOfBoundsException for negative
177: * index, IndexOutOfBoundsException for offset+length > array.length
178: */
179: public static void assertArrayIndex(int arrayLength, int offset,
180: int length) {
181: if (offset < 0 || length < 0) {
182: // nio.05=Negative index specified
183: throw new IndexOutOfBoundsException(Messages
184: .getString("nio.05")); //$NON-NLS-1$
185: }
186: if ((long) offset + (long) length > arrayLength) {
187: // nio.04=Size mismatch
188: throw new IndexOutOfBoundsException(Messages
189: .getString("nio.04")); //$NON-NLS-1$
190: }
191: }
192: }
|