49afdba55e7292e648109df458990d84fcb2c5be
[IRC.git] / Robust / src / ClassLibrary / SSJava / Long.java
1 /* Long.java -- object wrapper for long
2    Copyright (C) 1998, 1999, 2001, 2002, 2004, 2005 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37
38
39 //package java.lang;
40
41 /**
42  * Instances of class <code>Long</code> represent primitive
43  * <code>long</code> values.
44  *
45  * Additionally, this class provides various helper functions and variables
46  * related to longs.
47  *
48  * @author Paul Fisher
49  * @author John Keiser
50  * @author Warren Levy
51  * @author Eric Blake (ebb9@email.byu.edu)
52  * @author Tom Tromey (tromey@redhat.com)
53  * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
54  * @author Ian Rogers
55  * @since 1.0
56  * @status updated to 1.5
57  */
58 @LATTICE("V<C, V<O")
59 @METHODDEFAULT("O<V,V<C,C<IN,C*,THISLOC=IN,RETURNLOC=O")
60 public final class Long //extends Number implements Comparable<Long>
61 {
62   /**
63    * Compatible with JDK 1.0.2+.
64    */
65   private static final long serialVersionUID = 4290774380558885855L;
66
67   /**
68    * The minimum value a <code>long</code> can represent is
69    * -9223372036854775808L (or -2<sup>63</sup>).
70    */
71   public static final long MIN_VALUE = 0x8000000000000000L;
72
73   /**
74    * The maximum value a <code>long</code> can represent is
75    * 9223372036854775807 (or 2<sup>63</sup> - 1).
76    */
77   public static final long MAX_VALUE = 0x7fffffffffffffffL;
78
79   /**
80    * The primitive type <code>long</code> is represented by this
81    * <code>Class</code> object.
82    * @since 1.1
83    */
84   //public static final Class<Long> TYPE = (Class<Long>) VMClassLoader.getPrimitiveClass ('J');
85
86   /**
87    * The number of bits needed to represent a <code>long</code>.
88    * @since 1.5
89    */
90   public static final int SIZE = 64;
91
92   // This caches some Long values, and is used by boxing
93   // conversions via valueOf().  We cache at least -128..127;
94   // these constants control how much we actually cache.
95   private static final int MIN_CACHE = -128;
96   private static final int MAX_CACHE = 127;
97   private static final Long[] longCache = new Long[MAX_CACHE - MIN_CACHE + 1];
98   static
99   {
100     for (int i=MIN_CACHE; i <= MAX_CACHE; i++)
101       longCache[i - MIN_CACHE] = new Long(i);
102   }
103
104   /**
105    * The immutable value of this Long.
106    *
107    * @serial the wrapped long
108    */
109   @LOC("V") private final long value;
110
111   /**
112    * Create a <code>Long</code> object representing the value of the
113    * <code>long</code> argument.
114    *
115    * @param value the value to use
116    */
117   public Long(long value)
118   {
119     this.value = value;
120   }
121
122   /**
123    * Create a <code>Long</code> object representing the value of the
124    * argument after conversion to a <code>long</code>.
125    *
126    * @param s the string to convert
127    * @throws NumberFormatException if the String does not contain a long
128    * @see #valueOf(String)
129    */
130   public Long(String s)
131   {
132     value = parseLong(s, 10, false);
133   }
134
135   /**
136    * Return the size of a string large enough to hold the given number
137    *
138    * @param num the number we want the string length for (must be positive)
139    * @param radix the radix (base) that will be used for the string
140    * @return a size sufficient for a string of num
141    */
142   private static int stringSize(long num, int radix) {
143     int exp;
144     if (radix < 4)
145       {
146         exp = 1;
147       }
148     else if (radix < 8)
149       {
150         exp = 2;
151       }
152     else if (radix < 16)
153       {
154         exp = 3;
155       }
156     else if (radix < 32)
157       {
158         exp = 4;
159       }
160     else
161       {
162         exp = 5;
163       }
164     int size=0;
165     do
166       {
167         num >>>= exp;
168         size++;
169       }
170     while(num != 0);
171     return size;
172   }
173
174   /**
175    * Converts the <code>long</code> to a <code>String</code> using
176    * the specified radix (base). If the radix exceeds
177    * <code>Character.MIN_RADIX</code> or <code>Character.MAX_RADIX</code>, 10
178    * is used instead. If the result is negative, the leading character is
179    * '-' ('\\u002D'). The remaining characters come from
180    * <code>Character.forDigit(digit, radix)</code> ('0'-'9','a'-'z').
181    *
182    * @param num the <code>long</code> to convert to <code>String</code>
183    * @param radix the radix (base) to use in the conversion
184    * @return the <code>String</code> representation of the argument
185    */
186   /*public static String toString(long num, int radix)
187   {
188     if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
189       radix = 10;
190
191     // Is the value negative?
192     boolean isNeg = num < 0;
193
194     // Is the string a single character?
195     if (!isNeg && num < radix)
196       return new String(digits, (int)num, 1, true);
197
198     // Compute string size and allocate buffer
199     // account for a leading '-' if the value is negative
200     int size;
201     int i;
202     char[] buffer;
203     if (isNeg)
204       {
205         num = -num;
206
207         // When the value is MIN_VALUE, it overflows when made positive
208         if (num < 0)
209           {
210             i = size = stringSize(MAX_VALUE, radix) + 2;
211             buffer = new char[size];
212             buffer[--i] = digits[(int) (-(num + radix) % radix)];
213             num = -(num / radix);
214           }
215         else
216           {
217             i = size = stringSize(num, radix) + 1;
218             buffer = new char[size];
219           }
220       }
221     else
222       {
223         i = size = stringSize(num, radix);
224         buffer = new char[size];
225       }
226
227     do
228       {
229         buffer[--i] = digits[(int) (num % radix)];
230         num /= radix;
231       }
232     while (num > 0);
233
234     if (isNeg)
235       buffer[--i] = '-';
236
237     // Package constructor avoids an array copy.
238     return new String(buffer, i, size - i, true);
239   }*/
240
241   /**
242    * Converts the <code>long</code> to a <code>String</code> assuming it is
243    * unsigned in base 16.
244    *
245    * @param l the <code>long</code> to convert to <code>String</code>
246    * @return the <code>String</code> representation of the argument
247    */
248   /*public static String toHexString(long l)
249   {
250     return toUnsignedString(l, 4);
251   }*/
252
253   /**
254    * Converts the <code>long</code> to a <code>String</code> assuming it is
255    * unsigned in base 8.
256    *
257    * @param l the <code>long</code> to convert to <code>String</code>
258    * @return the <code>String</code> representation of the argument
259    */
260   /*public static String toOctalString(long l)
261   {
262     return toUnsignedString(l, 3);
263   }*/
264
265   /**
266    * Converts the <code>long</code> to a <code>String</code> assuming it is
267    * unsigned in base 2.
268    *
269    * @param l the <code>long</code> to convert to <code>String</code>
270    * @return the <code>String</code> representation of the argument
271    */
272   /*public static String toBinaryString(long l)
273   {
274     return toUnsignedString(l, 1);
275   }*/
276
277   /**
278    * Converts the <code>long</code> to a <code>String</code> and assumes
279    * a radix of 10.
280    *
281    * @param num the <code>long</code> to convert to <code>String</code>
282    * @return the <code>String</code> representation of the argument
283    * @see #toString(long, int)
284    */
285   public static String toString(long num)
286   {
287     //return toString(num, 10);
288     return String.valueOf(num);
289   }
290
291   /**
292    * Converts the specified <code>String</code> into an <code>int</code>
293    * using the specified radix (base). The string must not be <code>null</code>
294    * or empty. It may begin with an optional '-', which will negate the answer,
295    * provided that there are also valid digits. Each digit is parsed as if by
296    * <code>Character.digit(d, radix)</code>, and must be in the range
297    * <code>0</code> to <code>radix - 1</code>. Finally, the result must be
298    * within <code>MIN_VALUE</code> to <code>MAX_VALUE</code>, inclusive.
299    * Unlike Double.parseDouble, you may not have a leading '+'; and 'l' or
300    * 'L' as the last character is only valid in radices 22 or greater, where
301    * it is a digit and not a type indicator.
302    *
303    * @param str the <code>String</code> to convert
304    * @param radix the radix (base) to use in the conversion
305    * @return the <code>String</code> argument converted to <code>long</code>
306    * @throws NumberFormatException if <code>s</code> cannot be parsed as a
307    *         <code>long</code>
308    */
309   public static long parseLong(String str, int radix)
310   {
311     return parseLong(str, radix, false);
312   }
313
314   /**
315    * Converts the specified <code>String</code> into a <code>long</code>.
316    * This function assumes a radix of 10.
317    *
318    * @param s the <code>String</code> to convert
319    * @return the <code>int</code> value of <code>s</code>
320    * @throws NumberFormatException if <code>s</code> cannot be parsed as a
321    *         <code>long</code>
322    * @see #parseLong(String, int)
323    */
324   public static long parseLong(String s)
325   {
326     return parseLong(s, 10, false);
327   }
328
329   /**
330    * Creates a new <code>Long</code> object using the <code>String</code>
331    * and specified radix (base).
332    *
333    * @param s the <code>String</code> to convert
334    * @param radix the radix (base) to convert with
335    * @return the new <code>Long</code>
336    * @throws NumberFormatException if <code>s</code> cannot be parsed as a
337    *         <code>long</code>
338    * @see #parseLong(String, int)
339    */
340   public static Long valueOf(String s, int radix)
341   {
342     return valueOf(parseLong(s, radix, false));
343   }
344
345   /**
346    * Creates a new <code>Long</code> object using the <code>String</code>,
347    * assuming a radix of 10.
348    *
349    * @param s the <code>String</code> to convert
350    * @return the new <code>Long</code>
351    * @throws NumberFormatException if <code>s</code> cannot be parsed as a
352    *         <code>long</code>
353    * @see #Long(String)
354    * @see #parseLong(String)
355    */
356   public static Long valueOf(String s)
357   {
358     return valueOf(parseLong(s, 10, false));
359   }
360
361   /**
362    * Returns a <code>Long</code> object wrapping the value.
363    *
364    * @param val the value to wrap
365    * @return the <code>Long</code>
366    * @since 1.5
367    */
368   public static Long valueOf(long val)
369   {
370     if (val < MIN_CACHE || val > MAX_CACHE)
371       return new Long(val);
372     else
373       return longCache[((int)val) - MIN_CACHE];
374   }
375
376   /**
377    * Convert the specified <code>String</code> into a <code>Long</code>.
378    * The <code>String</code> may represent decimal, hexadecimal, or
379    * octal numbers.
380    *
381    * <p>The extended BNF grammar is as follows:<br>
382    * <pre>
383    * <em>DecodableString</em>:
384    *      ( [ <code>-</code> ] <em>DecimalNumber</em> )
385    *    | ( [ <code>-</code> ] ( <code>0x</code> | <code>0X</code>
386    *              | <code>#</code> ) <em>HexDigit</em> { <em>HexDigit</em> } )
387    *    | ( [ <code>-</code> ] <code>0</code> { <em>OctalDigit</em> } )
388    * <em>DecimalNumber</em>:
389    *        <em>DecimalDigit except '0'</em> { <em>DecimalDigit</em> }
390    * <em>DecimalDigit</em>:
391    *        <em>Character.digit(d, 10) has value 0 to 9</em>
392    * <em>OctalDigit</em>:
393    *        <em>Character.digit(d, 8) has value 0 to 7</em>
394    * <em>DecimalDigit</em>:
395    *        <em>Character.digit(d, 16) has value 0 to 15</em>
396    * </pre>
397    * Finally, the value must be in the range <code>MIN_VALUE</code> to
398    * <code>MAX_VALUE</code>, or an exception is thrown. Note that you cannot
399    * use a trailing 'l' or 'L', unlike in Java source code.
400    *
401    * @param str the <code>String</code> to interpret
402    * @return the value of the String as a <code>Long</code>
403    * @throws NumberFormatException if <code>s</code> cannot be parsed as a
404    *         <code>long</code>
405    * @throws NullPointerException if <code>s</code> is null
406    * @since 1.2
407    */
408   public static Long decode(String str)
409   {
410     return valueOf(parseLong(str, 10, true));
411   }
412
413   /**
414    * Return the value of this <code>Long</code> as a <code>byte</code>.
415    *
416    * @return the byte value
417    */
418   public byte byteValue()
419   {
420     return (byte) value;
421   }
422
423   /**
424    * Return the value of this <code>Long</code> as a <code>short</code>.
425    *
426    * @return the short value
427    */
428   public short shortValue()
429   {
430     return (short) value;
431   }
432
433   /**
434    * Return the value of this <code>Long</code> as an <code>int</code>.
435    *
436    * @return the int value
437    */
438   public int intValue()
439   {
440     return (int) value;
441   }
442
443   /**
444    * Return the value of this <code>Long</code>.
445    *
446    * @return the long value
447    */
448   public long longValue()
449   {
450     return value;
451   }
452
453   /**
454    * Return the value of this <code>Long</code> as a <code>float</code>.
455    *
456    * @return the float value
457    */
458   public float floatValue()
459   {
460     return value;
461   }
462
463   /**
464    * Return the value of this <code>Long</code> as a <code>double</code>.
465    *
466    * @return the double value
467    */
468   public double doubleValue()
469   {
470     return value;
471   }
472
473   /**
474    * Converts the <code>Long</code> value to a <code>String</code> and
475    * assumes a radix of 10.
476    *
477    * @return the <code>String</code> representation
478    */
479   public String toString()
480   {
481     //return toString(value, 10);
482     return String.valueOf(value);
483   }
484
485   /**
486    * Return a hashcode representing this Object. <code>Long</code>'s hash
487    * code is calculated by <code>(int) (value ^ (value &gt;&gt; 32))</code>.
488    *
489    * @return this Object's hash code
490    */
491   public int hashCode()
492   {
493     return (int) (value ^ (value >>> 32));
494   }
495
496   /**
497    * Returns <code>true</code> if <code>obj</code> is an instance of
498    * <code>Long</code> and represents the same long value.
499    *
500    * @param obj the object to compare
501    * @return whether these Objects are semantically equal
502    */
503   public boolean equals(Object obj)
504   {
505     return obj instanceof Long && value == ((Long) obj).value;
506   }
507
508   /**
509    * Get the specified system property as a <code>Long</code>. The
510    * <code>decode()</code> method will be used to interpret the value of
511    * the property.
512    *
513    * @param nm the name of the system property
514    * @return the system property as a <code>Long</code>, or null if the
515    *         property is not found or cannot be decoded
516    * @throws SecurityException if accessing the system property is forbidden
517    * @see System#getProperty(String)
518    * @see #decode(String)
519    */
520   public static Long getLong(String nm)
521   {
522     return getLong(nm, null);
523   }
524
525   /**
526    * Get the specified system property as a <code>Long</code>, or use a
527    * default <code>long</code> value if the property is not found or is not
528    * decodable. The <code>decode()</code> method will be used to interpret
529    * the value of the property.
530    *
531    * @param nm the name of the system property
532    * @param val the default value
533    * @return the value of the system property, or the default
534    * @throws SecurityException if accessing the system property is forbidden
535    * @see System#getProperty(String)
536    * @see #decode(String)
537    */
538   public static Long getLong(String nm, long val)
539   {
540     Long result = getLong(nm, null);
541     return result == null ? valueOf(val) : result;
542   }
543
544   /**
545    * Get the specified system property as a <code>Long</code>, or use a
546    * default <code>Long</code> value if the property is not found or is
547    * not decodable. The <code>decode()</code> method will be used to
548    * interpret the value of the property.
549    *
550    * @param nm the name of the system property
551    * @param def the default value
552    * @return the value of the system property, or the default
553    * @throws SecurityException if accessing the system property is forbidden
554    * @see System#getProperty(String)
555    * @see #decode(String)
556    */
557   public static Long getLong(String nm, Long def)
558   {
559     if (nm == null || "".equals(nm))
560       return def;
561     nm = null;//System.getProperty(nm);
562     if (nm == null)
563       return def;
564     /*try
565       {
566         return decode(nm);
567       }
568     catch (NumberFormatException e)
569       {
570         return def;
571       }*/
572   }
573
574   /**
575    * Compare two Longs numerically by comparing their <code>long</code>
576    * values. The result is positive if the first is greater, negative if the
577    * second is greater, and 0 if the two are equal.
578    *
579    * @param l the Long to compare
580    * @return the comparison
581    * @since 1.2
582    */
583   public int compareTo(Long l)
584   {
585     if (value == l.value)
586       return 0;
587     // Returns just -1 or 1 on inequality; doing math might overflow the long.
588     return value > l.value ? 1 : -1;
589   }
590
591   /**
592    * Return the number of bits set in x.
593    * @param x value to examine
594    * @since 1.5
595    */
596   public static int bitCount(long x)
597   {
598     // Successively collapse alternating bit groups into a sum.
599     x = ((x >> 1) & 0x5555555555555555L) + (x & 0x5555555555555555L);
600     x = ((x >> 2) & 0x3333333333333333L) + (x & 0x3333333333333333L);
601     int v = (int) ((x >>> 32) + x);
602     v = ((v >> 4) & 0x0f0f0f0f) + (v & 0x0f0f0f0f);
603     v = ((v >> 8) & 0x00ff00ff) + (v & 0x00ff00ff);
604     return ((v >> 16) & 0x0000ffff) + (v & 0x0000ffff);
605   }
606
607   /**
608    * Rotate x to the left by distance bits.
609    * @param x the value to rotate
610    * @param distance the number of bits by which to rotate
611    * @since 1.5
612    */
613   public static long rotateLeft(long x, int distance)
614   {
615     // This trick works because the shift operators implicitly mask
616     // the shift count.
617     return (x << distance) | (x >>> - distance);
618   }
619
620   /**
621    * Rotate x to the right by distance bits.
622    * @param x the value to rotate
623    * @param distance the number of bits by which to rotate
624    * @since 1.5
625    */
626   public static long rotateRight(long x, int distance)
627   {
628     // This trick works because the shift operators implicitly mask
629     // the shift count.
630     return (x << - distance) | (x >>> distance);
631   }
632
633   /**
634    * Find the highest set bit in value, and return a new value
635    * with only that bit set.
636    * @param value the value to examine
637    * @since 1.5
638    */
639   public static long highestOneBit(long value)
640   {
641     value |= value >>> 1;
642     value |= value >>> 2;
643     value |= value >>> 4;
644     value |= value >>> 8;
645     value |= value >>> 16;
646     value |= value >>> 32;
647     return value ^ (value >>> 1);
648   }
649
650   /**
651    * Return the number of leading zeros in value.
652    * @param value the value to examine
653    * @since 1.5
654    */
655   public static int numberOfLeadingZeros(long value)
656   {
657     value |= value >>> 1;
658     value |= value >>> 2;
659     value |= value >>> 4;
660     value |= value >>> 8;
661     value |= value >>> 16;
662     value |= value >>> 32;
663     return bitCount(~value);
664   }
665
666   /**
667    * Find the lowest set bit in value, and return a new value
668    * with only that bit set.
669    * @param value the value to examine
670    * @since 1.5
671    */
672   public static long lowestOneBit(long value)
673   {
674     // Classic assembly trick.
675     return value & - value;
676   }
677
678   /**
679    * Find the number of trailing zeros in value.
680    * @param value the value to examine
681    * @since 1.5
682    */
683   public static int numberOfTrailingZeros(long value)
684   {
685     return bitCount((value & -value) - 1);
686   }
687
688   /**
689    * Return 1 if x is positive, -1 if it is negative, and 0 if it is
690    * zero.
691    * @param x the value to examine
692    * @since 1.5
693    */
694   public static int signum(long x)
695   {
696     return (int) ((x >> 63) | (-x >>> 63));
697
698     // The LHS propagates the sign bit through every bit in the word;
699     // if X < 0, every bit is set to 1, else 0.  if X > 0, the RHS
700     // negates x and shifts the resulting 1 in the sign bit to the
701     // LSB, leaving every other bit 0.
702
703     // Hacker's Delight, Section 2-7
704   }
705
706   /**
707    * Reverse the bytes in val.
708    * @since 1.5
709    */
710   /*public static long reverseBytes(long val)
711   {
712     int hi = Integer.reverseBytes((int) val);
713     int lo = Integer.reverseBytes((int) (val >>> 32));
714     return (((long) hi) << 32) | lo;
715   }*/
716
717   /**
718    * Reverse the bits in val.
719    * @since 1.5
720    */
721   /*public static long reverse(long val)
722   {
723     long hi = Integer.reverse((int) val) & 0xffffffffL;
724     long lo = Integer.reverse((int) (val >>> 32)) & 0xffffffffL;
725     return (hi << 32) | lo;
726   }*/
727
728   /**
729    * Helper for converting unsigned numbers to String.
730    *
731    * @param num the number
732    * @param exp log2(digit) (ie. 1, 3, or 4 for binary, oct, hex)
733    */
734   /*private static String toUnsignedString(long num, int exp)
735   {
736     // Compute string length
737     int size = 1;
738     long copy = num >>> exp;
739     while (copy != 0)
740       {
741         size++;
742         copy >>>= exp;
743       }
744     // Quick path for single character strings
745     if (size == 1)
746       return new String(digits, (int)num, 1, true);
747
748     // Encode into buffer
749     int mask = (1 << exp) - 1;
750     char[] buffer = new char[size];
751     int i = size;
752     do
753       {
754         buffer[--i] = digits[(int) num & mask];
755         num >>>= exp;
756       }
757     while (num != 0);
758
759     // Package constructor avoids an array copy.
760     return new String(buffer, i, size - i, true);
761   }*/
762
763   /**
764    * Helper for parsing longs.
765    *
766    * @param str the string to parse
767    * @param radix the radix to use, must be 10 if decode is true
768    * @param decode if called from decode
769    * @return the parsed long value
770    * @throws NumberFormatException if there is an error
771    * @throws NullPointerException if decode is true and str is null
772    * @see #parseLong(String, int)
773    * @see #decode(String)
774    */
775   private static long parseLong(String str, int radix, boolean decode)
776   {
777     if (! decode && str == null)
778       throw new /*NumberFormat*/Exception("NumberFormatException");
779     int index = 0;
780     int len = str.length();
781     boolean isNeg = false;
782     if (len == 0)
783       throw new /*NumberFormat*/Exception("NumberFormatException");
784     int ch = str.charAt(index);
785     if (ch == '-')
786       {
787         if (len == 1)
788           throw new /*NumberFormat*/Exception("NumberFormatException");
789         isNeg = true;
790         ch = str.charAt(++index);
791       }
792     if (decode)
793       {
794         if (ch == '0')
795           {
796             if (++index == len)
797               return 0;
798             if ((str.charAt(index) & ~('x' ^ 'X')) == 'X')
799               {
800                 radix = 16;
801                 index++;
802               }
803             else
804               radix = 8;
805           }
806         else if (ch == '#')
807           {
808             radix = 16;
809             index++;
810           }
811       }
812     if (index == len)
813       throw new /*NumberFormat*/Exception("NumberFormatException");
814
815     long max = MAX_VALUE / radix;
816     // We can't directly write `max = (MAX_VALUE + 1) / radix'.
817     // So instead we fake it.
818     if (isNeg && MAX_VALUE % radix == radix - 1)
819       ++max;
820
821     long val = 0;
822     while (index < len)
823       {
824         if (val < 0 || val > max)
825           throw new /*NumberFormat*/Exception("NumberFormatException");
826
827         ch = Character.digit(str.charAt(index++), radix);
828         val = val * radix + ch;
829         if (ch < 0 || (val < 0 && (! isNeg || val != MIN_VALUE)))
830           throw new /*NumberFormat*/Exception("NumberFormatException");
831       }
832     return isNeg ? -val : val;
833   }
834 }