src/java.base/share/classes/java/lang/Integer.java
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 54952 a978d86ac389
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
   345         // assert shift > 0 && shift <=5 : "Illegal shift value";
   345         // assert shift > 0 && shift <=5 : "Illegal shift value";
   346         int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val);
   346         int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val);
   347         int chars = Math.max(((mag + (shift - 1)) / shift), 1);
   347         int chars = Math.max(((mag + (shift - 1)) / shift), 1);
   348         if (COMPACT_STRINGS) {
   348         if (COMPACT_STRINGS) {
   349             byte[] buf = new byte[chars];
   349             byte[] buf = new byte[chars];
   350             formatUnsignedInt(val, shift, buf, 0, chars);
   350             formatUnsignedInt(val, shift, buf, chars);
   351             return new String(buf, LATIN1);
   351             return new String(buf, LATIN1);
   352         } else {
   352         } else {
   353             byte[] buf = new byte[chars * 2];
   353             byte[] buf = new byte[chars * 2];
   354             formatUnsignedIntUTF16(val, shift, buf, 0, chars);
   354             formatUnsignedIntUTF16(val, shift, buf, chars);
   355             return new String(buf, UTF16);
   355             return new String(buf, UTF16);
   356         }
   356         }
   357     }
   357     }
   358 
   358 
   359     /**
   359     /**
   360      * Format an {@code int} (treated as unsigned) into a character buffer. If
   360      * Format an {@code int} (treated as unsigned) into a byte buffer (LATIN1 version). If
   361      * {@code len} exceeds the formatted ASCII representation of {@code val},
   361      * {@code len} exceeds the formatted ASCII representation of {@code val},
   362      * {@code buf} will be padded with leading zeroes.
   362      * {@code buf} will be padded with leading zeroes.
   363      *
   363      *
   364      * @param val the unsigned int to format
   364      * @param val the unsigned int to format
   365      * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
   365      * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
   366      * @param buf the character buffer to write to
   366      * @param buf the byte buffer to write to
   367      * @param offset the offset in the destination buffer to start at
       
   368      * @param len the number of characters to write
   367      * @param len the number of characters to write
   369      */
   368      */
   370     static void formatUnsignedInt(int val, int shift, char[] buf, int offset, int len) {
   369     private static void formatUnsignedInt(int val, int shift, byte[] buf, int len) {
   371         // assert shift > 0 && shift <=5 : "Illegal shift value";
   370         int charPos = len;
   372         // assert offset >= 0 && offset < buf.length : "illegal offset";
       
   373         // assert len > 0 && (offset + len) <= buf.length : "illegal length";
       
   374         int charPos = offset + len;
       
   375         int radix = 1 << shift;
       
   376         int mask = radix - 1;
       
   377         do {
       
   378             buf[--charPos] = Integer.digits[val & mask];
       
   379             val >>>= shift;
       
   380         } while (charPos > offset);
       
   381     }
       
   382 
       
   383     /** byte[]/LATIN1 version    */
       
   384     static void formatUnsignedInt(int val, int shift, byte[] buf, int offset, int len) {
       
   385         int charPos = offset + len;
       
   386         int radix = 1 << shift;
   371         int radix = 1 << shift;
   387         int mask = radix - 1;
   372         int mask = radix - 1;
   388         do {
   373         do {
   389             buf[--charPos] = (byte)Integer.digits[val & mask];
   374             buf[--charPos] = (byte)Integer.digits[val & mask];
   390             val >>>= shift;
   375             val >>>= shift;
   391         } while (charPos > offset);
   376         } while (charPos > 0);
   392     }
   377     }
   393 
   378 
   394     /** byte[]/UTF16 version    */
   379     /**
   395     private static void formatUnsignedIntUTF16(int val, int shift, byte[] buf, int offset, int len) {
   380      * Format an {@code int} (treated as unsigned) into a byte buffer (UTF16 version). If
   396         int charPos = offset + len;
   381      * {@code len} exceeds the formatted ASCII representation of {@code val},
       
   382      * {@code buf} will be padded with leading zeroes.
       
   383      *
       
   384      * @param val the unsigned int to format
       
   385      * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
       
   386      * @param buf the byte buffer to write to
       
   387      * @param len the number of characters to write
       
   388      */
       
   389     private static void formatUnsignedIntUTF16(int val, int shift, byte[] buf, int len) {
       
   390         int charPos = len;
   397         int radix = 1 << shift;
   391         int radix = 1 << shift;
   398         int mask = radix - 1;
   392         int mask = radix - 1;
   399         do {
   393         do {
   400             StringUTF16.putChar(buf, --charPos, Integer.digits[val & mask]);
   394             StringUTF16.putChar(buf, --charPos, Integer.digits[val & mask]);
   401             val >>>= shift;
   395             val >>>= shift;
   402         } while (charPos > offset);
   396         } while (charPos > 0);
   403     }
   397     }
   404 
   398 
   405     static final byte[] DigitTens = {
   399     static final byte[] DigitTens = {
   406         '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
   400         '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
   407         '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
   401         '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
   602      * @param      s   the {@code String} containing the integer
   596      * @param      s   the {@code String} containing the integer
   603      *                  representation to be parsed
   597      *                  representation to be parsed
   604      * @param      radix   the radix to be used while parsing {@code s}.
   598      * @param      radix   the radix to be used while parsing {@code s}.
   605      * @return     the integer represented by the string argument in the
   599      * @return     the integer represented by the string argument in the
   606      *             specified radix.
   600      *             specified radix.
   607      * @exception  NumberFormatException if the {@code String}
   601      * @throws     NumberFormatException if the {@code String}
   608      *             does not contain a parsable {@code int}.
   602      *             does not contain a parsable {@code int}.
   609      */
   603      */
   610     public static int parseInt(String s, int radix)
   604     public static int parseInt(String s, int radix)
   611                 throws NumberFormatException
   605                 throws NumberFormatException
   612     {
   606     {
   696      *             {@link java.lang.Character#MAX_RADIX}.
   690      *             {@link java.lang.Character#MAX_RADIX}.
   697      * @since  9
   691      * @since  9
   698      */
   692      */
   699     public static int parseInt(CharSequence s, int beginIndex, int endIndex, int radix)
   693     public static int parseInt(CharSequence s, int beginIndex, int endIndex, int radix)
   700                 throws NumberFormatException {
   694                 throws NumberFormatException {
   701         s = Objects.requireNonNull(s);
   695         Objects.requireNonNull(s);
   702 
   696 
   703         if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
   697         if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
   704             throw new IndexOutOfBoundsException();
   698             throw new IndexOutOfBoundsException();
   705         }
   699         }
   706         if (radix < Character.MIN_RADIX) {
   700         if (radix < Character.MIN_RADIX) {
   767      * int)} method.
   761      * int)} method.
   768      *
   762      *
   769      * @param s    a {@code String} containing the {@code int}
   763      * @param s    a {@code String} containing the {@code int}
   770      *             representation to be parsed
   764      *             representation to be parsed
   771      * @return     the integer value represented by the argument in decimal.
   765      * @return     the integer value represented by the argument in decimal.
   772      * @exception  NumberFormatException  if the string does not contain a
   766      * @throws     NumberFormatException  if the string does not contain a
   773      *               parsable integer.
   767      *               parsable integer.
   774      */
   768      */
   775     public static int parseInt(String s) throws NumberFormatException {
   769     public static int parseInt(String s) throws NumberFormatException {
   776         return parseInt(s,10);
   770         return parseInt(s,10);
   777     }
   771     }
   879      *             {@link java.lang.Character#MAX_RADIX}.
   873      *             {@link java.lang.Character#MAX_RADIX}.
   880      * @since  9
   874      * @since  9
   881      */
   875      */
   882     public static int parseUnsignedInt(CharSequence s, int beginIndex, int endIndex, int radix)
   876     public static int parseUnsignedInt(CharSequence s, int beginIndex, int endIndex, int radix)
   883                 throws NumberFormatException {
   877                 throws NumberFormatException {
   884         s = Objects.requireNonNull(s);
   878         Objects.requireNonNull(s);
   885 
   879 
   886         if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
   880         if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
   887             throw new IndexOutOfBoundsException();
   881             throw new IndexOutOfBoundsException();
   888         }
   882         }
   889         int start = beginIndex, len = endIndex - beginIndex;
   883         int start = beginIndex, len = endIndex - beginIndex;
   954      * @param      s   the string to be parsed.
   948      * @param      s   the string to be parsed.
   955      * @param      radix the radix to be used in interpreting {@code s}
   949      * @param      radix the radix to be used in interpreting {@code s}
   956      * @return     an {@code Integer} object holding the value
   950      * @return     an {@code Integer} object holding the value
   957      *             represented by the string argument in the specified
   951      *             represented by the string argument in the specified
   958      *             radix.
   952      *             radix.
   959      * @exception NumberFormatException if the {@code String}
   953      * @throws    NumberFormatException if the {@code String}
   960      *            does not contain a parsable {@code int}.
   954      *            does not contain a parsable {@code int}.
   961      */
   955      */
   962     public static Integer valueOf(String s, int radix) throws NumberFormatException {
   956     public static Integer valueOf(String s, int radix) throws NumberFormatException {
   963         return Integer.valueOf(parseInt(s,radix));
   957         return Integer.valueOf(parseInt(s,radix));
   964     }
   958     }
   980      * </blockquote>
   974      * </blockquote>
   981      *
   975      *
   982      * @param      s   the string to be parsed.
   976      * @param      s   the string to be parsed.
   983      * @return     an {@code Integer} object holding the value
   977      * @return     an {@code Integer} object holding the value
   984      *             represented by the string argument.
   978      *             represented by the string argument.
   985      * @exception  NumberFormatException  if the string cannot be parsed
   979      * @throws     NumberFormatException  if the string cannot be parsed
   986      *             as an integer.
   980      *             as an integer.
   987      */
   981      */
   988     public static Integer valueOf(String s) throws NumberFormatException {
   982     public static Integer valueOf(String s) throws NumberFormatException {
   989         return Integer.valueOf(parseInt(s, 10));
   983         return Integer.valueOf(parseInt(s, 10));
   990     }
   984     }
  1397      * permitted in the {@code String}.
  1391      * permitted in the {@code String}.
  1398      *
  1392      *
  1399      * @param     nm the {@code String} to decode.
  1393      * @param     nm the {@code String} to decode.
  1400      * @return    an {@code Integer} object holding the {@code int}
  1394      * @return    an {@code Integer} object holding the {@code int}
  1401      *             value represented by {@code nm}
  1395      *             value represented by {@code nm}
  1402      * @exception NumberFormatException  if the {@code String} does not
  1396      * @throws    NumberFormatException  if the {@code String} does not
  1403      *            contain a parsable integer.
  1397      *            contain a parsable integer.
  1404      * @see java.lang.Integer#parseInt(java.lang.String, int)
  1398      * @see java.lang.Integer#parseInt(java.lang.String, int)
  1405      */
  1399      */
  1406     public static Integer decode(String nm) throws NumberFormatException {
  1400     public static Integer decode(String nm) throws NumberFormatException {
  1407         int radix = 10;
  1401         int radix = 10;
  1861     public Integer resolveConstantDesc(MethodHandles.Lookup lookup) {
  1855     public Integer resolveConstantDesc(MethodHandles.Lookup lookup) {
  1862         return this;
  1856         return this;
  1863     }
  1857     }
  1864 
  1858 
  1865     /** use serialVersionUID from JDK 1.0.2 for interoperability */
  1859     /** use serialVersionUID from JDK 1.0.2 for interoperability */
       
  1860     @java.io.Serial
  1866     @Native private static final long serialVersionUID = 1360826667806852920L;
  1861     @Native private static final long serialVersionUID = 1360826667806852920L;
  1867 }
  1862 }