< prev index next >

jdk/src/java.base/share/classes/java/lang/Long.java

Print this page




 730      * <p>The method does not take steps to guard against the
 731      * {@code CharSequence} being mutated while parsing.
 732      *
 733      * @param      s   the {@code CharSequence} containing the {@code long}
 734      *                  representation to be parsed
 735      * @param      beginIndex   the beginning index, inclusive.
 736      * @param      endIndex     the ending index, exclusive.
 737      * @param      radix   the radix to be used while parsing {@code s}.
 738      * @return     the signed {@code long} represented by the subsequence in
 739      *             the specified radix.
 740      * @throws     NullPointerException  if {@code s} is null.
 741      * @throws     IndexOutOfBoundsException  if {@code beginIndex} is
 742      *             negative, or if {@code beginIndex} is greater than
 743      *             {@code endIndex} or if {@code endIndex} is greater than
 744      *             {@code s.length()}.
 745      * @throws     NumberFormatException  if the {@code CharSequence} does not
 746      *             contain a parsable {@code int} in the specified
 747      *             {@code radix}, or if {@code radix} is either smaller than
 748      *             {@link java.lang.Character#MIN_RADIX} or larger than
 749      *             {@link java.lang.Character#MAX_RADIX}.
 750      * @since  1.9
 751      */
 752     public static long parseLong(CharSequence s, int beginIndex, int endIndex, int radix)
 753                 throws NumberFormatException {
 754         s = Objects.requireNonNull(s);
 755 
 756         if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
 757             throw new IndexOutOfBoundsException();
 758         }
 759         if (radix < Character.MIN_RADIX) {
 760             throw new NumberFormatException("radix " + radix +
 761                     " less than Character.MIN_RADIX");
 762         }
 763         if (radix > Character.MAX_RADIX) {
 764             throw new NumberFormatException("radix " + radix +
 765                     " greater than Character.MAX_RADIX");
 766         }
 767 
 768         boolean negative = false;
 769         int i = beginIndex;
 770         long limit = -Long.MAX_VALUE;


 976      * <p>The method does not take steps to guard against the
 977      * {@code CharSequence} being mutated while parsing.
 978      *
 979      * @param      s   the {@code CharSequence} containing the unsigned
 980      *                 {@code long} representation to be parsed
 981      * @param      beginIndex   the beginning index, inclusive.
 982      * @param      endIndex     the ending index, exclusive.
 983      * @param      radix   the radix to be used while parsing {@code s}.
 984      * @return     the unsigned {@code long} represented by the subsequence in
 985      *             the specified radix.
 986      * @throws     NullPointerException  if {@code s} is null.
 987      * @throws     IndexOutOfBoundsException  if {@code beginIndex} is
 988      *             negative, or if {@code beginIndex} is greater than
 989      *             {@code endIndex} or if {@code endIndex} is greater than
 990      *             {@code s.length()}.
 991      * @throws     NumberFormatException  if the {@code CharSequence} does not
 992      *             contain a parsable unsigned {@code long} in the specified
 993      *             {@code radix}, or if {@code radix} is either smaller than
 994      *             {@link java.lang.Character#MIN_RADIX} or larger than
 995      *             {@link java.lang.Character#MAX_RADIX}.
 996      * @since  1.9
 997      */
 998     public static long parseUnsignedLong(CharSequence s, int beginIndex, int endIndex, int radix)
 999                 throws NumberFormatException {
1000         s = Objects.requireNonNull(s);
1001 
1002         if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
1003             throw new IndexOutOfBoundsException();
1004         }
1005         int start = beginIndex, len = endIndex - beginIndex;
1006 
1007         if (len > 0) {
1008             char firstChar = s.charAt(start);
1009             if (firstChar == '-') {
1010                 throw new NumberFormatException(String.format("Illegal leading minus sign " +
1011                         "on unsigned string %s.", s.subSequence(start, start + len)));
1012             } else {
1013                 if (len <= 12 || // Long.MAX_VALUE in Character.MAX_RADIX is 13 digits
1014                     (radix == 10 && len <= 18) ) { // Long.MAX_VALUE in base 10 is 19 digits
1015                     return parseLong(s, start, start + len, radix);
1016                 }




 730      * <p>The method does not take steps to guard against the
 731      * {@code CharSequence} being mutated while parsing.
 732      *
 733      * @param      s   the {@code CharSequence} containing the {@code long}
 734      *                  representation to be parsed
 735      * @param      beginIndex   the beginning index, inclusive.
 736      * @param      endIndex     the ending index, exclusive.
 737      * @param      radix   the radix to be used while parsing {@code s}.
 738      * @return     the signed {@code long} represented by the subsequence in
 739      *             the specified radix.
 740      * @throws     NullPointerException  if {@code s} is null.
 741      * @throws     IndexOutOfBoundsException  if {@code beginIndex} is
 742      *             negative, or if {@code beginIndex} is greater than
 743      *             {@code endIndex} or if {@code endIndex} is greater than
 744      *             {@code s.length()}.
 745      * @throws     NumberFormatException  if the {@code CharSequence} does not
 746      *             contain a parsable {@code int} in the specified
 747      *             {@code radix}, or if {@code radix} is either smaller than
 748      *             {@link java.lang.Character#MIN_RADIX} or larger than
 749      *             {@link java.lang.Character#MAX_RADIX}.
 750      * @since  9
 751      */
 752     public static long parseLong(CharSequence s, int beginIndex, int endIndex, int radix)
 753                 throws NumberFormatException {
 754         s = Objects.requireNonNull(s);
 755 
 756         if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
 757             throw new IndexOutOfBoundsException();
 758         }
 759         if (radix < Character.MIN_RADIX) {
 760             throw new NumberFormatException("radix " + radix +
 761                     " less than Character.MIN_RADIX");
 762         }
 763         if (radix > Character.MAX_RADIX) {
 764             throw new NumberFormatException("radix " + radix +
 765                     " greater than Character.MAX_RADIX");
 766         }
 767 
 768         boolean negative = false;
 769         int i = beginIndex;
 770         long limit = -Long.MAX_VALUE;


 976      * <p>The method does not take steps to guard against the
 977      * {@code CharSequence} being mutated while parsing.
 978      *
 979      * @param      s   the {@code CharSequence} containing the unsigned
 980      *                 {@code long} representation to be parsed
 981      * @param      beginIndex   the beginning index, inclusive.
 982      * @param      endIndex     the ending index, exclusive.
 983      * @param      radix   the radix to be used while parsing {@code s}.
 984      * @return     the unsigned {@code long} represented by the subsequence in
 985      *             the specified radix.
 986      * @throws     NullPointerException  if {@code s} is null.
 987      * @throws     IndexOutOfBoundsException  if {@code beginIndex} is
 988      *             negative, or if {@code beginIndex} is greater than
 989      *             {@code endIndex} or if {@code endIndex} is greater than
 990      *             {@code s.length()}.
 991      * @throws     NumberFormatException  if the {@code CharSequence} does not
 992      *             contain a parsable unsigned {@code long} in the specified
 993      *             {@code radix}, or if {@code radix} is either smaller than
 994      *             {@link java.lang.Character#MIN_RADIX} or larger than
 995      *             {@link java.lang.Character#MAX_RADIX}.
 996      * @since  9
 997      */
 998     public static long parseUnsignedLong(CharSequence s, int beginIndex, int endIndex, int radix)
 999                 throws NumberFormatException {
1000         s = Objects.requireNonNull(s);
1001 
1002         if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
1003             throw new IndexOutOfBoundsException();
1004         }
1005         int start = beginIndex, len = endIndex - beginIndex;
1006 
1007         if (len > 0) {
1008             char firstChar = s.charAt(start);
1009             if (firstChar == '-') {
1010                 throw new NumberFormatException(String.format("Illegal leading minus sign " +
1011                         "on unsigned string %s.", s.subSequence(start, start + len)));
1012             } else {
1013                 if (len <= 12 || // Long.MAX_VALUE in Character.MAX_RADIX is 13 digits
1014                     (radix == 10 && len <= 18) ) { // Long.MAX_VALUE in base 10 is 19 digits
1015                     return parseLong(s, start, start + len, radix);
1016                 }


< prev index next >