Index: /luni/src/main/java/java/lang/Short.java =================================================================== --- /luni/src/main/java/java/lang/Short.java (revision 383745) +++ /luni/src/main/java/java/lang/Short.java (working copy) @@ -15,25 +15,46 @@ package java.lang; - /** - * Shorts are objects (non-base types) which represent short values. + *
+ * Short is the wrapper for the primitive type short.
+ *
+ * Constant for the maximum short value, 215-1.
+ *
+ * Constant for the minimum short value, -215.
+ *
+ * Constant for the number of bits to represent a short in
+ * two's compliment form.
+ *
+ * A cache of instances used by {@link #valueOf(short)} and auto-boxing. + *
+ */ + private static final Short[] CACHE = new Short[256]; + /** * Constructs a new instance of this class given a string. * @@ -99,7 +127,7 @@ int intValue = Integer.decode(string).intValue(); short result = (short) intValue; if (result == intValue) - return new Short(result); + return valueOf(result); throw new NumberFormatException(); } @@ -251,7 +279,7 @@ * if the argument could not be parsed as a short quantity. */ public static Short valueOf(String string) throws NumberFormatException { - return new Short(parseShort(string)); + return valueOf(parseShort(string)); } /** @@ -270,6 +298,43 @@ */ public static Short valueOf(String string, int radix) throws NumberFormatException { - return new Short(parseShort(string, radix)); + return valueOf(parseShort(string, radix)); } + + /** + *
+ * Reverses the bytes of a short.
+ *
short to reverse.
+ * @return The reversed value.
+ * @since 1.5
+ */
+ public static short reverseBytes(short s) {
+ int high = (s >> 8) & 0xFF;
+ int low = (s & 0xFF) << 8;
+ return (short) (low | high);
+ }
+
+ /**
+ *
+ * Returns a Short instance for the short
+ * value passed. This method is preferred over the constructor, as this
+ * method may maintain a cache of instances.
+ *
Short instance.
+ * @since 1.5
+ */
+ public static Short valueOf(short s) {
+ if (s < -128 || s > 127) {
+ return new Short(s);
+ }
+ synchronized (CACHE) {
+ int idx = 128 + s; // 128 matches a cache size of 256
+ Short result = CACHE[idx];
+ return (result == null ? CACHE[idx] = new Short(s) : result);
+ }
+ }
}