001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * 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, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package java.lang;
017:
018: import java.io.PrintStream;
019:
020: /**
021: * General-purpose low-level utility methods. GWT only supports a limited subset
022: * of these methods due to browser limitations. Only the documented methods are
023: * available.
024: */
025: public final class System {
026:
027: /**
028: * Does nothing in web mode. To get output in web mode, subclass PrintStream
029: * and call {@link #setErr(PrintStream)}.
030: */
031: public static final PrintStream err = new PrintStream(null);
032:
033: /**
034: * Does nothing in web mode. To get output in web mode, subclass
035: * {@link PrintStream} and call {@link #setOut(PrintStream)}.
036: */
037: public static final PrintStream out = new PrintStream(null);
038:
039: public static void arraycopy(Object src, int srcOfs, Object dest,
040: int destOfs, int len) {
041: if (src == null || dest == null) {
042: throw new NullPointerException();
043: }
044:
045: // TODO: use Class objects when Class.getComponentType() is supported.
046: String srcTypeName = src.getClass().getName();
047: String destTypeName = dest.getClass().getName();
048: if (srcTypeName.charAt(0) != '['
049: || destTypeName.charAt(0) != '[') {
050: throw new ArrayStoreException("Must be array types");
051: }
052: if (srcTypeName.charAt(1) != destTypeName.charAt(1)) {
053: throw new ArrayStoreException("Array types must match");
054: }
055: int srclen = getArrayLength(src);
056: int destlen = getArrayLength(dest);
057: if (srcOfs < 0 || destOfs < 0 || len < 0
058: || srcOfs + len > srclen || destOfs + len > destlen) {
059: throw new IndexOutOfBoundsException();
060: }
061: /*
062: * If the arrays are not references or if they are exactly the same type, we
063: * can copy them in native code for speed. Otherwise, we have to copy them
064: * in Java so we get appropriate errors.
065: */
066: if ((srcTypeName.charAt(1) == 'L' || srcTypeName.charAt(1) == '[')
067: && !srcTypeName.equals(destTypeName)) {
068: // copy in Java to make sure we get ArrayStoreExceptions if the values
069: // aren't compatible
070: Object[] srcArray = (Object[]) src;
071: Object[] destArray = (Object[]) dest;
072: if (src == dest && srcOfs < destOfs) {
073: // TODO(jat): how does backward copies handle failures in the middle?
074: // copy backwards to avoid destructive copies
075: srcOfs += len;
076: for (int destEnd = destOfs + len; destEnd-- > destOfs;) {
077: destArray[destEnd] = srcArray[--srcOfs];
078: }
079: } else {
080: for (int destEnd = destOfs + len; destOfs < destEnd;) {
081: destArray[destOfs++] = srcArray[srcOfs++];
082: }
083: }
084: } else {
085: nativeArraycopy(src, srcOfs, dest, destOfs, len);
086: }
087: }
088:
089: public static native long currentTimeMillis() /*-{
090: return (new Date()).getTime();
091: }-*/;
092:
093: /**
094: * Has no effect; just here for source compatibility.
095: *
096: * @skip
097: */
098: public static native void gc() /*-{
099: }-*/;
100:
101: public static native int identityHashCode(Object o) /*-{
102: return @com.google.gwt.core.client.Impl::getHashCode(Ljava/lang/Object;)(o);
103: }-*/;
104:
105: public static native void setErr(PrintStream err) /*-{
106: @java.lang.System::err = err;
107: }-*/;
108:
109: public static native void setOut(PrintStream out) /*-{
110: @java.lang.System::out = out;
111: }-*/;
112:
113: /**
114: * Returns the length of an array via Javascript.
115: */
116: private static native int getArrayLength(Object array) /*-{
117: return array.length;
118: }-*/;
119:
120: /**
121: * Copy an array using native Javascript. The destination array must be a real
122: * Java array (ie, already has the GWT type info on it). No error checking is
123: * performed -- the caller is expected to have verified everything first.
124: *
125: * @param src source array for copy
126: * @param srcOfs offset into source array
127: * @param dest destination array for copy
128: * @param destOfs offset into destination array
129: * @param len number of elements to copy
130: */
131: private static native void nativeArraycopy(Object src, int srcOfs,
132: Object dest, int destOfs, int len) /*-{
133: Array.prototype.splice.apply(dest, [destOfs, len].concat(src.slice(srcOfs, srcOfs + len)));
134: }-*/;
135:
136: }
|