Scanner.java 65.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
/* java.util.Scanner -- Parses primitive types and strings using regexps
   Copyright (C) 2007  Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

package java.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import java.math.BigDecimal;
import java.math.BigInteger;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.ReadableByteChannel;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.text.ParseException;

import java.util.Iterator;
import java.util.Locale;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author E0327023 Hernadi Laszlo
67 68
*/
public class Scanner
69 70
  implements Iterator <String>
{
71
  private static final String NOT_LONG = "\" is not a long";    //$NON-NLS-1$
72

73
  private static final String ERR_PREFIX = "\"";        //$NON-NLS-1$
74

75
  private static final String NOT_INT = "\" is not an integer"; //$NON-NLS-1$
76

77
  private static final String NOT_DOUBLE = "\" is not a double";        //$NON-NLS-1$
78

79
  private static final String NOT_BYTE = "\" is not a byte";    //$NON-NLS-1$
80

81
  private static final String NOT_BOOLEAN = "\" is not a boolean";      //$NON-NLS-1$
82

83
  private static final String IS_NOT = "\" is not ";    //$NON-NLS-1$
84

85
  private static final String DEFAULT_PATTERN_S = "\\p{javaWhitespace}+";       //$NON-NLS-1$
86 87 88 89

  private static final Pattern DEFAULT_PATTERN =
    Pattern.compile (DEFAULT_PATTERN_S);

90
  private static final String BIG_INTEGER = "BigInteger";       //$NON-NLS-1$
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

  private final static String NEW_LINE =
    System.getProperty ("line.separator");

  private IOException lastIOException = null;

  /**
   * An InputStream source if a Constructor with an InputStream source is called, otherwise it
   * stays <source> null </source>.
   */
  private InputStream bIS = null;

  /**
   * Length of the input Buffer, which is the maximum bytes to be read at once.
   */
  private final int MaxBufferLen = 1000000;

  /**
   * Minimum buffer length. If there are less chars in the Buffer than this value reading from
   * source is tried.
   */
  private final int MIN_BUF_LEN = 100;

  /**
   * Maximum number of processed chars in the Buffer. If exeeded, all processed chars from the
   * beginning of the Buffer will be discarded to save space. The bytes left are copyed into a new
   * Buffer.
   */
  private final int MAX_PREFIX = 10000;

  /**
   * The Buffer which is used by the Matcher to find given patterns. It is filled up when matcher
   * hits end or <code> MIN_BUF_LEN </code> is reached.
   */
  private String actBuffer = new String ();

  /**
   * The current radix to use by the methods getNextXXX and hasNextXXX.
   */
  private int currentRadix = 10;

  /**
   * The current locale.
134
   *
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
   * @see #useLocale(Locale)
   * @see #locale()
   */
  private Locale actLocale = Locale.getDefault ();

  /**
   * The current pattern for the matcher.
   */
  private Pattern p = DEFAULT_PATTERN;

  /**
   * The current position in the Buffer, at which the next match should start.
   */
  private int actPos = 0;

  /**
   * A global buffer to save new allocations by reading from source.
   */
  private final byte[] tmpBuffer = new byte[this.MaxBufferLen];

  /**
   * The charsetName to use with the source.
   */
  private String charsetName = null;

  /**
   * The Matcher which is used.
   */
  private Matcher myMatcher = this.p.matcher (this.actBuffer);

  /**
   * The MatchResult is generated at each match, even if match() isn't called.
   */
  private MatchResult actResult = null;

  /**
   * A Readable source if a Constructor with a Readable source is called, otherwise it stays
   * <source> null </source>.
   */
  private Readable readableSource = null;
175

176 177 178 179 180 181 182 183 184 185
  /**
   * A ReadableByteChannel source if a Constructor with a ReadableByteChannel source is called,
   * otherwise it stays <source> null </source>.
   */
  private ReadableByteChannel rbcSource = null;

  /**
   * Indicates if the close() method was called.
   */
  private boolean isClosed = false;
186

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
  /**
   * For performance reasons the last Found is saved, if a hasNextXXX method was called.
   */
  private String lastFound = null;

  private boolean lastFoundPresent = false;

  private int lastNextPos = 0;

  private int lastPatternHash = 0;

  private int last_RegionStart = 0;

  private int last_RegionEnd = 0;

  private boolean last_anchor = false;

  private boolean last_transparent = false;

  private MatchResult lastResult = null;

  /**
   * To keep track of the current position in the stream for the toString method, each time
   * processed chars are removed the amount is added to processedChars.
   */
  private int procesedChars = 0;

  /**
   * needInput is set <code> true </code> before a read method, and if there is no input it blocks
   * and stays <code>true</code>. Right after a read it is set to <code>false</code>.
   */
  private boolean needInput = false;

  private boolean skipped = false;

  /**
   * <code> {@link #doSkipp} </code> indicates that the found pattern belongs to the result. If
   * <code> {@link #doSkipp} </code> is false the match result ends at the beginning of the match.
   * In both cases the current position is set after the pattern, if the found pattern has to be
   * removed, a nextXXX method is called.
   */
  private boolean doSkipp = false;

  /**
   * Indicates if the last match was valid or not.
   */
  private boolean matchValid = false;

  private NumberFormat actFormat = NumberFormat.getInstance (this.actLocale);

  private DecimalFormat df = (DecimalFormat) this.actFormat;

  /**
   * Indicates if current Locale should be used at the input.
   */
  private boolean useLocale = true;

  private DecimalFormatSymbols dfs =
    new DecimalFormatSymbols (this.actLocale);

  /**
   * Constructs a new Scanner with the given File as source.
   * {@link #Scanner(InputStream, String)} is called with <code> null </code> as charsetName.
250
   *
251 252 253 254 255
   * @param source
   *            The File to use as source.
   * @throws FileNotFoundException
   *             If the file is not found an Exception is thrown.
   */
256
  public Scanner (final File source) throws FileNotFoundException       // TESTED
257 258 259
  {
    this (source, null);
  }
260

261 262 263
  /**
   * Constructs a new Scanner with the given File as source. <br>
   * {@link #Scanner(InputStream, String)} is called with the given charsetName.
264
   *
265 266 267 268 269 270 271 272 273
   * @param source
   *            The File to use as source.
   * @param charsetName
   *            Current charset name of the file. If charsetName is null it behaves if it was not
   *            set.
   * @throws FileNotFoundException
   *             If the file is not found an Exception is thrown.
   */
  public Scanner (final File source,
274
                  final String charsetName) throws FileNotFoundException
275 276 277 278 279 280 281
  {
    this (new FileInputStream (source), charsetName);
  }

  /**
   * Constructs a new Scanner with the given inputStream. <br>
   * {@link #Scanner(InputStream, String)} is called with <code> null </code> as charsetName.
282
   *
283 284 285
   * @param source
   *            The InputStream to use as source.
   */
286
  public Scanner (final InputStream source)     // TESTED
287 288 289 290 291 292 293
  {
    this (source, null);
  }

  /**
   * Constructs a new Scanner with the InputSream and a charsetName. Afterwards the Buffer is
   * filled.
294
   *
295 296 297 298 299 300 301 302 303 304 305
   * @param source
   *            The InputStream to use as source.
   * @param charsetName
   *            The charsetName to apply on the source's data.
   */
  public Scanner (final InputStream source, final String charsetName)
  {
    this.bIS = (new BufferedInputStream (source));
    this.charsetName = charsetName;
    myFillBuffer ();
  }
306

307 308
  /**
   * Constructs a new Scanner with a Readable input as source.
309
   *
310 311 312 313 314 315 316 317 318 319 320 321 322
   * @param source
   *            The Readable to use as source.
   */
  public Scanner (final Readable source)
  {
    this.readableSource = source;
    myFillBuffer ();
  }

  /**
   * Constructs a new Scanner with a ReadableByteChannel as
   * source. Therfore the {@link #Scanner(ReadableByteChannel,
   * String)} is called with <code> null </code> as charsetName.
323
   *
324 325 326 327 328 329 330 331 332 333 334 335
   * @param source
   *            The ReadableByteChannel to use as source.
   */
  public Scanner (final ReadableByteChannel source)
  {
    this (source, null);
  }

  /**
   * Constructs a new Scanner with a ReadableByteChannel as source and
   * a given charsetName, which is to be applied on it. <br> It also
   * initiates the main Buffer.
336
   *
337 338 339 340 341 342 343 344 345 346 347 348 349 350
   * @param source
   *            The ReadableByteChannel to use as source.
   * @param charsetName
   *            The charsetName to be applied on the source.
   */
  public Scanner (final ReadableByteChannel source, final String charsetName)
  {
    this.charsetName = charsetName;
    this.rbcSource = source;
    myFillBuffer ();
  }

  /**
   * Constructs a new Scanner using the given String as input only.
351
   *
352 353 354
   * @param source
   *            The whole String to be used as source.
   */
355
  public Scanner (final String source)  // TESTED
356 357 358 359 360 361 362 363 364 365 366
  {
    this.actBuffer = new String (source);
    this.myMatcher.reset (this.actBuffer);
  }

  /**
   * Closes this Scanner. If an {@link IOException} occurs it is
   * catched and is available under {@link #ioException()}.<br> After
   * the Scanner is closed, all searches will lead to a {@link
   * IllegalStateException}.
   */
367
  public void close ()
368 369 370 371
  {
    try
    {
      if (this.bIS != null)
372
        this.bIS.close ();
373
      if (this.rbcSource != null)
374
        this.rbcSource.close ();
375 376 377 378 379 380 381 382 383 384
      this.isClosed = true;
    }
    catch (IOException ioe)
    {
      this.lastIOException = ioe;
    }
  }

  /**
   * Returns the current delimiter.
385
   *
386 387
   * @return the current delimiter.
   */
388
  public Pattern delimiter ()   // TESTED
389 390 391 392 393 394
  {
    return this.p;
  }

  /**
   * Tries to find the pattern in the current line.
395
   *
396 397 398 399 400 401 402
   * @param pattern The pattern which should be searched in the
   * current line of the input.
   * @throws NoSuchElementException
   *             If the pattern was not found.
   * @return If the search was successful, the result or otherwise a
   *         {@link NoSuchElementException} is thrown.
   */
403
  public String findInLine (final Pattern pattern) throws NoSuchElementException        // TESTED
404 405 406 407
  {
    String tmpStr = myNextLine (false);
    return myFindPInStr (pattern, tmpStr, 0);
  }
408

409 410 411 412
  /**
   * Compiles the given pattern into a {@link Pattern} and calls
   * {@link #findInLine(Pattern)} with the compiled pattern and
   * returns whatever it returns.
413
   *
414 415 416 417 418 419
   * @param pattern
   *            The pattern which should be matched in the input.
   * @throws NoSuchElementException
   *             If the pattern was not found.
   * @return The match in the current line.
   */
420
  public String findInLine (final String pattern)       // TESTED
421 422 423 424 425 426
  {
    return findInLine (Pattern.compile (pattern));
  }

  /**
   * Trys to match the pattern within the given horizon.
427
   *
428 429 430 431 432 433 434 435 436 437 438 439 440 441
   * @param pattern
   *            Pattern to search.
   * @param horizon
   * @return The result of the match.
   * @throws IllegalArgumentException
   *             if the horizon is negative.
   * @throws IllegalStateException
   *             if the Scanner is closed.
   */
  public String findWithinHorizon (final Pattern pattern, final int horizon)
    throws IllegalArgumentException, IllegalStateException
  {
    if (horizon < 0)
      {
442
        throw new IllegalArgumentException (horizon + " is negative");
443 444 445 446
      }

    if (this.isClosed)
      {
447
        throw new IllegalStateException ("Scanner is closed");
448 449 450 451 452 453 454 455
      }

    // doSkipp is set true to get the matching patern together with the found String
    this.doSkipp = true;
    String rc = myFindPInStr (pattern, this.actBuffer, horizon);

    if (rc != null)
      {
456
        this.actPos += rc.length ();
457 458 459 460 461 462 463 464
      }

    return rc;
  }

  /**
   * Compile the pattern and call {@link #findWithinHorizon(Pattern,
   * int)}.
465
   *
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
   * @param pattern
   *            Pattern to search.
   * @param horizon
   * @return The result of the match.
   * @throws IllegalArgumentException
   *             if the horizon is negative.
   * @throws IllegalStateException
   *             if the Scanner is closed.
   */
  public String findWithinHorizon (final String pattern, final int horizon)
    throws IllegalArgumentException, IllegalStateException
  {
    return findWithinHorizon (Pattern.compile (pattern), horizon);
  }

  /**
   * Checks if there is any next String using the current
   * delimiter. Therefore the string must not be <code> null </code>
   * and the length must be greater then 0. If a {@link
   * NoSuchElementException} is thrown by the search method, it is
   * catched and false is returned.
487
   *
488 489 490 491 492
   * @return <code> true </code> if there is any result using the current delimiter. This wouldn't
   *         lead to a {@link NoSuchElementException}.
   * @throws IllegalStateException
   *             if the Scanner is closed.
   */
493
  public boolean hasNext () throws IllegalStateException        // TESTED
494 495
  {
    String tmpStr = null;
496

497 498 499 500 501 502 503 504 505 506
    try
    {
      tmpStr = myCoreNext (false, this.p);
    }
    catch (NoSuchElementException nf)
    {
    }

    if (tmpStr == null || tmpStr.length () <= 0)
      {
507
        return false;
508 509 510 511 512 513 514
      }
    return true;
  }

  /**
   * Searches the pattern in the next subString before the next
   * current delimiter.
515
   *
516 517 518 519 520 521
   * @param pattern
   *            The pattern to search for.
   * @return <code> true </code> if the pattern is found before the current delimiter.
   * @throws IllegalStateException
   *             if the Scanner is closed.
   */
522
  public boolean hasNext (final Pattern pattern) throws IllegalStateException   // TESTED
523 524 525 526 527 528 529
  {
    String tmpStr;

      tmpStr = myNext (pattern, false);

    if (tmpStr == null || tmpStr.length () <= 0)
      {
530
        return false;
531 532 533 534 535 536 537
      }
    return true;
  }

  /**
   * Compiles the pattern to a {@link Pattern} and calls {@link
   * #hasNext(Pattern)}.
538
   *
539 540 541 542 543 544 545
   * @see #hasNext(Pattern)
   * @param pattern
   *            The pattern as string to search for.
   * @return <code> true </code> if the pattern is found before the current delimiter.
   * @throws IllegalStateException
   *             if the Scanner is closed.
   */
546
  public boolean hasNext (final String pattern) throws IllegalStateException    // TESTED
547 548 549 550 551 552 553 554
  {
    return hasNext (Pattern.compile (pattern));
  }

  /**
   * Checks if the string to the next delimiter can be interpreted as
   * a BigDecimal number. <br> BigDecimal numbers are always tryed
   * with radix 10.
555
   *
556 557 558 559 560
   * @see #nextBigDecimal()
   * @return <code> true </code> if the next string is a BigDecimal number.
   * @throws IllegalStateException
   *             if the Scanner is closed.
   */
561
  public boolean hasNextBigDecimal () throws IllegalStateException      // TESTED
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
  {
    try
    {
      myBigDecimal (false);
      return true;
    }
    catch (InputMismatchException nfe)
    {
      return false;
    }
  }

  /**
   * Checks if the string to the next delimiter can be interpreted as
   * a BigInteger number. <br> Call {@link #hasNextBigInteger(int)}
   * with the current radix.
578
   *
579 580 581 582 583
   * @see #nextBigInteger()
   * @return <code> true </code> if the next string is a BigInteger number.
   * @throws IllegalStateException
   *             if the Scanner is closed.
   */
584
  public boolean hasNextBigInteger () throws IllegalStateException      // TESTED
585 586 587 588 589 590 591
  {
    return hasNextBigInteger (this.currentRadix);
  }

  /**
   * Checks if the string to the next delimiter can be interpreted as
   * a BigInteger number. <br>
592
   *
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
   * @param radix
   *            The radix to use for this check. The global radix of the Scanner will not be
   *            changed.
   * @return <code> true </code> if the next string is a BigInteger number.
   * @throws IllegalStateException
   *             if the Scanner is closed.
   */
  public boolean hasNextBigInteger (final int radix) throws
    IllegalStateException
  {
    try
    {
      myNextBigInteger (radix, false, BIG_INTEGER);
      return true;
    }
    catch (InputMismatchException ime)
    {
      return false;
    }
  }

  /**
   * Checks if the next string could be a boolean. The method handles
   * the input not case sensitiv, so "true" and "TRUE" and even "tRuE"
   * are <code> true </code>.
618
   *
619 620 621 622 623
   * @see #nextBoolean()
   * @return Return <code> true </code> if the next string is a boolean.
   * @throws IllegalStateException
   *             if the Scanner is closed.
   */
624
  public boolean hasNextBoolean () throws IllegalStateException // TESTED
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
  {
    try
    {
      myNextBoolean (false);
      return true;
    }
    catch (InputMismatchException ime)
    {
      return false;
    }
  }

  /**
   * Checks if the string to the next delimiter can be interpreted as
   * a byte number. <br> Calls {@link #hasNextByte(int)} with the
   * current radix.
641
   *
642 643 644 645 646
   * @see #nextByte()
   * @return <code> true </code> if the next string is a byte number.
   * @throws IllegalStateException
   *             if the Scanner is closed.
   */
647
  public boolean hasNextByte () throws IllegalStateException    // TESTED
648 649 650 651 652 653 654 655 656
  {
    return hasNextByte (this.currentRadix);
  }

  /**
   * Checks if the string to the next delimiter can be interpreted as
   * a byte number with the given radix. <br> To check, the private
   * method {@link #myNextByte(int, boolean)} is called, and if no
   * error occurs the next string could be a byte.
657
   *
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
   * @see #nextByte(int)
   * @param radix The radix to use for this check. The global radix of
   * the Scanner will not be changed.
   * @return <code> true </code> if the next string is a byte number.
   * @throws IllegalStateException
   *             if the Scanner is closed.
   */
  public boolean hasNextByte (final int radix) throws IllegalStateException
  {
    try
    {
      myNextByte (radix, false);
      return true;
    }
    catch (InputMismatchException ime)
    {
      return false;
    }
  }

  /**
   * Checks if the string to the next delimiter can be interpreted as
   * a double number. <br> To check, the private method {@link
   * #myNextDouble(boolean)} is called, and if no error occurs the
   * next string could be a double.
683
   *
684 685 686 687 688
   * @see #nextDouble()
   * @return <code> true </code> if the next string is a double number.
   * @throws IllegalStateException
   *             if the Scanner is closed.
   */
689
  public boolean hasNextDouble () throws IllegalStateException  // TESTED
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
  {
    try
    {
      myNextDouble (false);
      return true;
    }
    catch (InputMismatchException ime)
    {
      return false;
    }
  }

  /**
   * Checks if the string to the next delimiter can be interpreted as
   * a double number. Because every float is a double this is
   * checked.<br> To check, the private method {@link
   * #myNextDouble(boolean)} is called, and if no error occurs the
   * next string could be a double.
708
   *
709 710 711 712 713
   * @see #nextFloat()
   * @return <code> true </code> if the next string is a double number.
   * @throws IllegalStateException
   *             if the Scanner is closed.
   */
714
  public boolean hasNextFloat () throws IllegalStateException   // TESTED
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
  {
    try
    {
      myNextDouble (false);
      // myNextFloat(false);
      return true;
    }
    catch (InputMismatchException ime)
    {
      return false;
    }
  }

  /**
   * Checks if the string to the next delimiter can be interpreted as
   * an int number. <br> To check, the private method {@link
   * #myNextInt(int, boolean)} is called, and if no error occurs the
   * next string could be an int.
733
   *
734 735 736 737 738
   * @see #nextInt(int)
   * @return <code> true </code> if the next string is an int number.
   * @throws IllegalStateException
   *             if the Scanner is closed.
   */
739
  public boolean hasNextInt () throws IllegalStateException     // TESTED
740 741 742 743 744 745 746 747 748
  {
    return hasNextInt (this.currentRadix);
  }

  /**
   * Checks if the string to the next delimiter can be interpreted as
   * an int number with the given radix. <br> To check, the private
   * method {@link #myNextInt(int, boolean)} is called, and if no
   * error occurs the next string could be an int.
749
   *
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
   * @see #nextInt(int)
   * @param radix
   *            The radix to use for this check. The global radix of the Scanner will not be
   *            changed.
   * @return <code> true </code> if the next string is an int number.
   * @throws IllegalStateException
   *             if the Scanner is closed.
   */
  public boolean hasNextInt (final int radix) throws IllegalStateException
  {
    try
    {
      myNextInt (radix, false);
      return true;
    }
    catch (InputMismatchException ime)
    {
      return false;
    }
  }

  /**
   * Checks if there is a current line, which ends at the next line
   * break or the end of the input.
774
   *
775 776 777 778
   * @return <code> true </code> if there is a current line.
   * @throws IllegalStateException
   *             if the Scanner is closed.
   */
779
  public boolean hasNextLine () throws IllegalStateException    // TESTED
780 781 782 783 784 785 786 787 788
  {
    return (myNextLine (false) != null);
  }

  /**
   * Checks if the string to the next delimiter can be interpreted as
   * a long number. <br> To check, the private method {@link
   * #myNextLong(int, boolean)} is called, and if no error occurs the
   * next string could be a long.
789
   *
790 791 792 793 794
   * @see #nextLong()
   * @return <code> true </code> if the next string is a long number.
   * @throws IllegalStateException
   *             if the Scanner is closed.
   */
795
  public boolean hasNextLong () throws IllegalStateException    // TESTED
796 797 798 799 800 801 802 803 804
  {
    return hasNextLong (this.currentRadix);
  }

  /**
   * Checks if the string to the next delimiter can be interpreted as
   * a long number with the given radix. <br> To check, the private
   * method {@link #myNextLong(int, boolean)} is called, and if no
   * error occurs the next string could be a long.
805
   *
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
   * @see #nextLong(int)
   * @param radix
   *            The radix to use for this check. The global radix of the Scanner will not be
   *            changed.
   * @return <code> true </code> if the next string is a long number.
   * @throws IllegalStateException
   *             if the Scanner is closed.
   */
  public boolean hasNextLong (final int radix) throws IllegalStateException
  {
    try
    {
      myNextLong (radix, false);
      return true;
    }
    catch (InputMismatchException ime)
    {
      return false;
    }
  }

  /**
   * Checks if the string to the next delimiter can be interpreted as
   * a short number with the given radix. <br> To check, the private
   * method {@link #myNextShort(int, boolean)} is called, and if no
   * error occurs the next string could be a short.
832
   *
833 834 835 836 837
   * @see #nextShort(int)
   * @return <code> true </code> if the next string is a short number.
   * @throws IllegalStateException
   *             if the Scanner is closed.
   */
838
  public boolean hasNextShort () throws IllegalStateException   // TESTED
839 840 841 842 843 844 845 846 847
  {
    return hasNextShort (this.currentRadix);
  }

  /**
   * Checks if the string to the next delimiter can be interpreted as
   * a short number. <br> To check, the private method {@link
   * #myNextShort(int, boolean)} is called, and if no error occurs the
   * next string could be a short.
848
   *
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
   * @see #nextShort(int)
   * @param radix
   *            The radix to use for this check. The global radix of the Scanner will not be
   *            changed.
   * @return <code> true </code> if the next string is a short number.
   * @throws IllegalStateException
   *             if the Scanner is closed.
   */
  public boolean hasNextShort (final int radix) throws IllegalStateException
  {
    try
    {
      myNextShort (radix, false);
      return true;
    }
    catch (InputMismatchException ime)
    {
      return false;
    }
  }

  /**
   * Returns the last {@link IOException} occured.
872
   *
873 874 875 876 877 878 879 880 881 882 883
   * @return Returns the last {@link IOException}.
   */
  public IOException ioException ()
  {
    return this.lastIOException;
  }

  /**
   * Returns the current value of {@link #useLocale}. This is used to
   * tell the Scanner if it should use the Locale format or just
   * handle numbers of the default format.
884
   *
885 886 887
   * @see #setUseLocale(boolean)
   * @return the useLoclae.
   */
888
  public boolean isUseLocale () // TESTED
889 890 891 892 893 894 895
  {
    return this.useLocale;
  }

  /**
   * Returns the current Locale. It is initialized with {@link
   * Locale#getDefault()}.
896
   *
897 898 899
   * @see #useLocale(Locale)
   * @return Returns the current Locale.
   */
900
  public Locale locale ()       // TESTED
901 902 903 904 905 906 907
  {
    return this.actLocale;
  }

  /**
   * Returns the last MatchResult found. This is updated after every
   * successfully search.
908
   *
909 910
   * @return Returns the last {@link MatchResult} found.
   */
911
  public MatchResult match ()   // TESTED
912 913 914 915 916 917 918 919 920
  {
    return this.actResult;
  }

  /**
   * Uses the current delimiter to find the next string in the
   * buffer. If a string is found the current position is set after
   * the delimiter, otherwise a {@link NoSuchElementException} is
   * thrown. A successful match sets the matchResult.
921
   *
922 923 924 925 926 927 928
   * @see #match()
   * @return Returns the next string of the buffer.
   * @throws NoSuchElementException
   *             If no element was found an exception is thrown.
   * @throws IllegalStateException
   *             If the Scanner is closed.
   */
929
  public String next () throws NoSuchElementException, IllegalStateException    // TESTED
930 931 932 933 934 935 936
  {
    return myCoreNext (true, this.p);
  }

  /**
   * Tries to match the buffer with the given pattern. The current
   * delimiter will not be changed.
937
   *
938 939 940 941 942 943 944 945
   * @param pattern
   *            The pattern to match.
   * @return Returns the next string matching the pattern.
   * @throws NoSuchElementException
   *             If no element was found an exception is thrown.
   * @throws IllegalStateException
   *             If the Scanner is closed.
   */
946
  public String next (final Pattern pattern) throws NoSuchElementException, IllegalStateException       // TESTED
947 948 949 950 951 952 953 954
  {
    return myNext (pattern, true);
  }

  /**
   * Tries to match the buffer with the given pattern. The current
   * delimiter will not be changed.  Calls the {@link #next(Pattern)}
   * with the compiled pattern.
955
   *
956 957 958 959 960 961 962 963 964
   * @see #next(Pattern)
   * @param pattern
   *            The pattern to match.
   * @return Returns the next string matching the pattern.
   * @throws NoSuchElementException
   *             If no element was found an exception is thrown.
   * @throws IllegalStateException
   *             If the Scanner is closed.
   */
965
  public String next (final String pattern) throws NoSuchElementException, IllegalStateException        // TESTED
966 967 968 969 970 971
  {
    return next (Pattern.compile (pattern));
  }

  /**
   * Tries to interpret the next string as a BigDecimal value.
972
   *
973 974 975 976 977 978
   * @return Returns the BigDecimal value of the next string.
   * @throws NoSuchElementException
   *             If no string is found or the string is not a BigDecimal.
   * @throws IllegalStateException
   *             If the Scanner is closed.
   */
979
  public BigDecimal nextBigDecimal () throws NoSuchElementException, IllegalStateException      // TESTED
980 981 982 983 984 985 986 987
  {
    return myBigDecimal (true);
  }

  /**
   * Tries to interpret the next string as a BigInteger value. Call
   * {@link #nextBigInteger(int)} with the current radix as parameter,
   * and return the value.
988
   *
989 990 991 992 993 994 995
   * @see #nextBigInteger(int)
   * @return Returns the BigInteger value of the next string.
   * @throws NoSuchElementException
   *             If no string is found or the string is not a BigInteger.
   * @throws IllegalStateException
   *             If the Scanner is closed.
   */
996
  public BigInteger nextBigInteger () throws NoSuchElementException, IllegalStateException      // TESTED
997 998 999 1000 1001 1002 1003
  {
    return nextBigInteger (this.currentRadix);
  }

  /**
   * Tries to interpret the next string as a BigInteger value with the
   * given radix.
1004
   *
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
   * @param radix
   *            The radix to be used for this BigInteger. The current radix of the Scanner is not
   *            changed.
   * @return Returns the BigInteger value of the next string.
   * @throws NoSuchElementException
   *             If no string is found or the string is not a BigInteger.
   * @throws IllegalStateException
   *             If the Scanner is closed.
   */
  public BigInteger nextBigInteger (final int radix) throws
    NoSuchElementException, IllegalStateException
  {
    return myNextBigInteger (radix, true, BIG_INTEGER);
  }

  /**
   * Tries to interpret the next string to the delimiter as a boolean
   * value, ignoring case.
1023
   *
1024 1025 1026 1027 1028 1029
   * @return Returns the boolean value of the next matching string or throws an exception.
   * @throws NoSuchElementException
   *             If no string is found or the string is not a boolean.
   * @throws IllegalStateException
   *             If the Scanner is closed.
   */
1030
  public boolean nextBoolean () throws NoSuchElementException, IllegalStateException    // TESTED
1031 1032 1033 1034 1035 1036 1037 1038
  {
    return myNextBoolean (true);
  }

  /**
   * Tries to interpret the next string as a byte value. Call {@link
   * #nextByte(int)} with the current radix as parameter, and return
   * the value.
1039
   *
1040 1041 1042 1043 1044 1045 1046
   * @see #nextByte(int)
   * @return Returns the byte value of the next string.
   * @throws NoSuchElementException
   *             If no string is found or the string is not a byte
   * @throws IllegalStateException
   *             If the Scanner is closed.
   */
1047
  public byte nextByte () throws NoSuchElementException, IllegalStateException  // TESTED
1048 1049 1050 1051 1052 1053 1054
  {
    return nextByte (this.currentRadix);
  }

  /**
   * Tries to interpret the next string as a byte value with the given
   * radix.
1055
   *
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
   * @param radix
   *            The radix to be used for this byte. The current radix of the Scanner is not
   *            changed.
   * @return Returns the byte value of the next string.
   * @throws NoSuchElementException
   *             If no string is found or the string is not a byte.
   * @throws IllegalStateException
   *             If the Scanner is closed.
   */
  public byte nextByte (final int radix) throws NoSuchElementException,
    IllegalStateException
  {
    return myNextByte (radix, true);
  }

  /**
   * Tries to interpret the next string as a double value.
1073
   *
1074 1075 1076 1077 1078 1079
   * @return Returns the int value of the next string.
   * @throws NoSuchElementException
   *             If no string is found or the string is not a double.
   * @throws IllegalStateException
   *             If the Scanner is closed.
   */
1080
  public double nextDouble () throws NoSuchElementException, IllegalStateException      // TESTED
1081 1082 1083 1084 1085 1086 1087
  {
    return myNextDouble (true);
  }

  /**
   * Tries to interpret the next string as a double value, and then
   * casts down to float.
1088
   *
1089 1090 1091 1092 1093 1094
   * @return Returns the int value of the next string.
   * @throws NoSuchElementException
   *             If no string is found or the string is not a double.
   * @throws IllegalStateException
   *             If the Scanner is closed.
   */
1095
  public float nextFloat () throws NoSuchElementException, IllegalStateException        // TESTED
1096 1097 1098 1099 1100 1101 1102 1103 1104
  {
    return (float) myNextDouble (true);
    // return myNextFloat(true);
  }

  /**
   * Tries to interpret the next string as an int value. Calls {@link
   * #nextInt(int)} with the current radix as parameter, and return
   * the value.
1105
   *
1106 1107 1108 1109 1110 1111 1112
   * @see #nextInt(int)
   * @return Returns the int value of the next string.
   * @throws NoSuchElementException
   *             If no string is found or the string is not an int.
   * @throws IllegalStateException
   *             If the Scanner is closed.
   */
1113
  public int nextInt () throws NoSuchElementException, IllegalStateException    // TESTED
1114 1115 1116 1117 1118 1119 1120
  {
    return nextInt (this.currentRadix);
  }

  /**
   * Tries to interpret the next string as an int value with the given
   * radix.
1121
   *
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
   * @param radix
   *            The radix to be used for this int. The current radix of the Scanner is not changed
   * @return Returns the int value of the next string.
   * @throws NoSuchElementException
   *             If no string is found or the string is not an int.
   * @throws IllegalStateException
   *             If the Scanner is closed.
   */
  public int nextInt (final int radix) throws NoSuchElementException,
    IllegalStateException
  {
    return myNextInt (radix, true);
  }

  /**
   * Tries to match the system line seperator, and returns the current
   * line.
1139
   *
1140 1141 1142 1143 1144 1145
   * @return Returns the current line.
   * @throws NoSuchElementException
   *             If the current delimiter is not found.
   * @throws IllegalStateException
   *             If the Scanner is closed.
   */
1146
  public String nextLine () throws NoSuchElementException, IllegalStateException        // TESTED
1147 1148 1149 1150 1151 1152 1153 1154
  {
    return myNextLine (true);
  }

  /**
   * Tries to interpret the next string as a long value. Calls {@link
   * #nextLong(int)} with the current radix as parameter, and return
   * the value.
1155
   *
1156 1157 1158 1159 1160 1161 1162
   * @see #nextLong(int)
   * @return Returns the long value of the next string.
   * @throws NoSuchElementException
   *             If no string is found or the string is not a long.
   * @throws IllegalStateException
   *             If the Scanner is closed.
   */
1163
  public long nextLong () throws NoSuchElementException, IllegalStateException  // TESTED
1164 1165 1166 1167 1168 1169 1170
  {
    return nextLong (this.currentRadix);
  }

  /**
   * Tries to interpret the next string as a long value with the given
   * radix.
1171
   *
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
   * @param radix
   *            The radix to be used for this long. The current radix of the Scanner is not
   *            changed
   * @return Returns the long value of the next string.
   * @throws NoSuchElementException
   *             If no string is found or the string is not a long.
   * @throws IllegalStateException
   *             If the Scanner is closed.
   */
  public long nextLong (final int radix) throws NoSuchElementException,
    IllegalStateException
  {
    return myNextLong (radix, true);
  }

  /**
   * Tries to interpret the next string as a short value. Calls {@link
   * #nextShort(int)} with the current radix as parameter, and return
   * the value.
1191
   *
1192 1193 1194 1195 1196
   * @see #nextShort(int)
   * @return Returns the short value of the next string.
   * @throws NoSuchElementException
   *             If no string is found or the string is not a short.
   */
1197
  public short nextShort () throws NoSuchElementException       // TESTED
1198 1199 1200 1201 1202 1203 1204
  {
    return nextShort (this.currentRadix);
  }

  /**
   * Tries to interpret the next string as a short value with the
   * given radix.
1205
   *
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
   * @param radix
   *            The radix to be used for this short. The current radix of the Scanner is not
   *            changed.
   * @return Returns the short value of the next string.
   * @throws NoSuchElementException
   *             If no string is found or the string is not a short.
   */
  public short nextShort (final int radix) throws NoSuchElementException
  {
    return myNextShort (radix, true);
  }

  /**
   * @return Returns the current radix.
   */
  public int radix ()
  {
    return this.currentRadix;
  }

  /**
   * The remove operation is not supported by this implementation of
   * Iterator.
   */
  public void remove ()
  {
  }

  /**
   * @param useLocale the useLocale to set.
   */
1237
  public void setUseLocale (final boolean useLocale)    // TESTED
1238 1239 1240 1241 1242 1243
  {
    this.useLocale = useLocale;
  }

  /**
   * Skips the given pattern. Sets skipped <code>true</code>.
1244
   *
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
   * @param pattern
   *            Pattern which should be skipped.
   * @return <code>this</code> with the skipped buffer.
   * @throws NoSuchElementException
   *             If the Pattern is not found.
   */
  public Scanner skip (final Pattern pattern) throws NoSuchElementException
  {
    this.doSkipp = true;
    int end;
    boolean found;
    Matcher matcher = pattern.matcher (this.actBuffer);
      matcher.region (this.actPos - 1, this.actBuffer.length ());

      found = matcher.find ();
      found = myFillBuffer_loop (matcher, this.actPos - 1, found);
      end = matcher.end ();

      this.actPos = end + 1;

      this.doSkipp = false;
      this.skipped = true;

      actResult = null;

    if (!found)
      {
1272
        throw new NoSuchElementException ();
1273 1274 1275 1276 1277 1278 1279
      }
    return this;
  }

  /**
   * Skips a given pattern. Calls {@link #skip(Pattern)} with the
   * compiled pattern.
1280
   *
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
   * @see #skip(Pattern)
   * @param pattern
   *            Pattern which should be skipped.
   * @return <code>this</code> with the skipped buffer.
   */
  public Scanner skip (final String pattern)
  {
    return skip (Pattern.compile (pattern));
  }

  /**
   * Returns the string representation of this Scanner.
   */
1294
  @Override
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
    public String toString ()
  {
    String tmpStr2;
    String rc = this.getClass ().getName ();
    tmpStr2 = rc;
    tmpStr2 = "[delimiters=" + this.p.pattern () + "]";
    rc += tmpStr2;
    tmpStr2 = "[position=" + (this.procesedChars + this.actPos) + "]";
    rc += tmpStr2;
    tmpStr2 = "[match valid=" + this.matchValid + "]";
    rc += tmpStr2;
    tmpStr2 = "[need input=" + this.needInput + "]";
    rc += tmpStr2;
    tmpStr2 = "[source closed=" + this.isClosed + "]";
    rc += tmpStr2;
    tmpStr2 = "[skipped=" + this.skipped + "]";
    rc += tmpStr2;
    tmpStr2 = "[group separator=\\" + this.dfs.getGroupingSeparator () + "]";
    rc += tmpStr2;
    tmpStr2 = "[decimal separator=\\" + this.dfs.getDecimalSeparator () + "]";
    rc += tmpStr2;
    tmpStr2 =
      "[positive prefix=" + myConvert (this.df.getPositivePrefix ()) + "]";
    rc += tmpStr2;
    tmpStr2 =
      "[negative prefix=" + myConvert (this.df.getNegativePrefix ()) + "]";
    rc += tmpStr2;
    tmpStr2 =
      "[positive suffix=" + myConvert (this.df.getPositiveSuffix ()) + "]";
    rc += tmpStr2;
    tmpStr2 =
      "[negative suffix=" + myConvert (this.df.getNegativeSuffix ()) + "]";
    rc += tmpStr2;
    tmpStr2 = "[NaN string=" + myConvert (this.dfs.getNaN ()) + "]";
    rc += tmpStr2;
    tmpStr2 = "[infinity string=" + myConvert (this.dfs.getInfinity ()) + "]";
    rc += tmpStr2;
    return rc;
  }

  /**
   * Sets the current pattern to the given parameter, and updates the
   * {@link Matcher} with the new pattern.
1338
   *
1339 1340 1341 1342
   * @param pattern
   *            The new pattern to use.
   * @return Returns the Scanner (<code>this</code>) with the new pattern.
   */
1343
  public Scanner useDelimiter (final Pattern pattern)   // TESTED
1344 1345 1346
  {
    if (pattern != null)
      {
1347 1348
        this.p = pattern;
        this.myMatcher = this.p.matcher (this.actBuffer);
1349 1350 1351 1352 1353 1354 1355
      }
    return this;
  }

  /**
   * Sets the current pattern to the given parameter. Compiles the
   * pattern and calls {@link #useDelimiter(Pattern)}
1356
   *
1357 1358 1359 1360 1361
   * @see #useDelimiter(Pattern)
   * @param pattern
   *            The new pattern to use.
   * @return Returns the Scanner (<code>this</code>) with the new pattern.
   */
1362
  public Scanner useDelimiter (final String pattern)    // TESTED
1363 1364 1365 1366 1367 1368 1369
  {
    return useDelimiter (Pattern.compile (pattern));
  }

  /**
   * Sets the current Locale to the given parameter. Formats and
   * Symbols are also set using the new Locale.
1370
   *
1371 1372 1373 1374
   * @param locale The new Locale to use. If it is <code>null</code>
   * nothing happens.
   * @return Returns the Scanner (<code>this</code>) with the new Locale.
   */
1375
  public Scanner useLocale (final Locale locale)        // TESTED
1376 1377 1378
  {
    if (locale != null)
      {
1379 1380 1381 1382
        this.actLocale = locale;
        this.actFormat = NumberFormat.getInstance (this.actLocale);
        this.dfs = new DecimalFormatSymbols (this.actLocale);
        this.df = (DecimalFormat) this.actFormat;
1383 1384 1385 1386 1387 1388 1389 1390
      }
    return this;
  }

  /**
   * Sets the current radix to the current value if the given radix is
   * >= 2 and <= 36 otherwise an {@link IllegalArgumentException} is
   * thrown.
1391
   *
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
   * @param radix
   *            the new radix to use as default.
   * @return <code> this </code> with the new radix value.
   * @throws IllegalArgumentException
   *             When the given radix is out of bounds.
   */
  public Scanner useRadix (final int radix) throws IllegalArgumentException
  {
    if (radix < 2 || radix > 36)
      {
1402
        throw new IllegalArgumentException ();
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
      }
    this.currentRadix = radix;
    return this;
  }

  /**
   * Checks if it is necessary to apply the current Locale on the
   * String. If so the String is converted using the {@link
   * NumberFormat#parse(String)} into a Number and then back to a
   * default stringrepresentation of that Number.
1413
   *
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
   * @see #setUseLocale(boolean)
   * @param str
   *            String to convert into another string.
   * @param radix Radix of the Number in the original string. It has
   * to be 10 for anything to happen.
   * @return Eighter the Stringrepresention of the number without the
   * Locale or an unchanged string.
   * @throws ParseException
   *             if {@link NumberFormat#parse(String)} fails to parse.
   */
  private String myApplyLocale (final String str,
1425
                                final int radix) throws ParseException
1426 1427 1428 1429 1430
  {
    String rc;

    if (this.useLocale && radix == 10)
      {
1431 1432
        rc = this.actFormat.parse (str).toString ();
        return rc;
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
      }

    return str;
  }

  /**
   * If {@link #useLocale} is set and radix is 10 the string is tryed
   * to be converted to string without Locale settings, because the
   * "normal" convert from Local has only double precision and it is
   * not enough for the about 50 digits of precision of the
   * BigDecimal. So in the first step the string is seperated into the
   * integer part which is converted to a long, and the fraction part
   * is appended afterwards. Between the integer and the fraction part
   * comes a ".". Finally the resulting string is returned.
1447
   *
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
   * @see #setUseLocale(boolean)
   * @param str String representation of a BigDecimal number.
   * @return The default String representation (without Locale) of the
   * BigInteger.
   * @throws ParseException
   *             If the String has more than one decimal seperators a parse exception is thrown.
   */
  private String myApplyLocaleBD (final String str) throws ParseException
  {
    if (!this.useLocale || this.currentRadix != 10)
      {
1459
        return str;
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
      }

    String negPrefix = this.df.getNegativePrefix ();
    String negSuffix = this.df.getNegativeSuffix ();
    String posPrefix = this.df.getPositivePrefix ();
    String posSuffix = this.df.getPositiveSuffix ();

    char d = this.dfs.getDecimalSeparator ();
    int begin1, begin2;
    boolean isNegativ = false;
    String parts = null;

    String tmpStr1 = "";

    begin1 = str.indexOf (d);
    begin2 = str.indexOf (d, begin1 + 1);

    if (begin2 > 0)
      {
1479
        throw new ParseException ("more than one Decimal seperators", begin2);
1480 1481 1482 1483 1484
      }

    parts = str.substring (0, begin1);

    if ((negPrefix.length () > 0
1485 1486 1487 1488
         && str.substring (0, negPrefix.length ()).equals (negPrefix))
        || (negSuffix.length () > 0
            && str.substring (str.length () -
                              negSuffix.length ()).equals (negSuffix)))
1489
      {
1490 1491
        parts += negSuffix;
        isNegativ = true;
1492 1493 1494
      }
    else
      if ((posPrefix.length () > 0
1495 1496 1497 1498
           && str.substring (0, posPrefix.length ()).equals (posPrefix))
          || (posSuffix.length () > 0
              && str.substring (str.length () -
                                posSuffix.length ()).equals (posSuffix)))
1499
      {
1500
        parts += posSuffix;
1501 1502 1503 1504 1505 1506
      }

    tmpStr1 = this.actFormat.parse (parts).toString ();

    if (isNegativ)
      {
1507 1508 1509
        tmpStr1 +=
          "." + str.substring (str.indexOf (d) + 1,
                               str.length () - negSuffix.length ());
1510 1511 1512
      }
    else
      {
1513 1514 1515
        tmpStr1 +=
          "." + str.substring (str.indexOf (d) + 1,
                               str.length () - posSuffix.length ());
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525
      }

    return tmpStr1;
  }

  /**
   * Tries to interpret the next String as a BigDecimal. Therfore the
   * next String is get with {@link #myCoreNext(boolean, Pattern)} and
   * then {@link #myApplyLocaleBD(String)} is called to convert the
   * String into a BigDecimal.
1526
   *
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
   * @param delete
   *            Should the found string be deleted or not.
   * @return Returns the BigDecimal value of the next string.
   * @throws InputMismatchException
   *             If the string is not a BigDecimal
   */
  private BigDecimal myBigDecimal (final boolean delete) throws
    InputMismatchException
  {
    BigDecimal rc;
    String tmp = myCoreNext (delete, this.p);
      try
    {
      tmp = myApplyLocaleBD (tmp);
    }
    catch (ParseException e)
    {
      throw new InputMismatchException (ERR_PREFIX + tmp + IS_NOT +
1545
                                        "BigDecimal!!");
1546 1547 1548 1549 1550 1551 1552 1553 1554
    }
    rc = new BigDecimal (tmp);

    return rc;
  }

  /**
   * Applies suffix ("\E") and prefix ("\Q") if str.length != 0 Used
   * by the toString method.
1555
   *
1556 1557 1558 1559 1560 1561 1562 1563
   * @param str
   *            the string on which the suffix and prefix should be applied.
   * @return The new new string with the suffix and prefix.
   */
  private String myConvert (final String str)
  {
    if (str != null && str.length () > 0)
      {
1564
        return "\\Q" + str + "\\E";
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
      }
    return str;
  }

  /**
   * Searches the current Matcher for the current Pattern. If the end
   * is reached during the search it tried to read again from the
   * source. The search results are always saved in {@link #actResult}
   * which is returned when match() is called. If doSkip is true the
   * pattern is also taken.
1575
   *
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
   * @param delete
   *            if true the aktPos is set.
   * @param pattern
   *            pattern to search for.
   * @return Returns the String which matches the pattern.
   * @throws NoSuchElementException
   *             If the search has no result.
   */
  private String myCoreNext (final boolean delete, final Pattern pattern)
    throws NoSuchElementException
  {
    if (this.isClosed)
      {
1589
        throw new IllegalStateException ("Scanner closed");
1590 1591 1592
      }
    if (shallUseLastFound (pattern != null ? pattern : this.p))
      {
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
        if (this.last_RegionEnd != this.myMatcher.regionEnd ())
          {
            System.out.println (this.last_RegionEnd + " != " +
                                this.myMatcher.regionEnd () + " (" +
                                (this.last_RegionEnd -
                                 this.myMatcher.regionEnd ()) + ")");
          }
        if (delete)
          {
            this.actPos = this.lastNextPos;
            this.lastFoundPresent = false;
            this.actResult = this.lastResult;
          }
        return this.lastFound;
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616
      }

    boolean found = false;
    int left;
    int endIndex;

    String tmp2 = null;

    if (this.actPos > this.MAX_PREFIX)
      {
1617 1618 1619 1620 1621 1622
        // skipp the processed chars so that the size of the buffer don't grow to much even with
        // huge files
        this.procesedChars += this.actPos;
        this.actBuffer = this.actBuffer.substring (this.actPos);
        this.actPos = 0;
        this.myMatcher = pattern.matcher (this.actBuffer);
1623 1624 1625 1626 1627
      }

    left = this.actBuffer.length () - this.actPos;
    if (left < this.MIN_BUF_LEN)
      {
1628
        myFillBuffer ();
1629 1630 1631 1632 1633 1634 1635 1636 1637
      }
    found = this.myMatcher.find (this.actPos);

    found = myFillBuffer_loop (this.myMatcher, this.actPos, found);

    this.needInput = false;

    if (found)
      {
1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
        if (this.doSkipp)
          {
            endIndex = this.myMatcher.end ();
          }
        else
          {
            endIndex = this.myMatcher.start ();
          }
        tmp2 = this.actBuffer.substring (this.actPos, endIndex);
        this.lastNextPos = this.myMatcher.end ();
        /*
         * if the delete flag is set, just set the current position after the end of the matched
         * pattern.
         */
        if (delete)
          {
            this.actPos = this.lastNextPos;
          }
        else
          {
            this.lastFound = tmp2;
            this.lastFoundPresent = true;
            this.lastPatternHash = pattern.hashCode ();
          }
        this.last_RegionStart = this.myMatcher.regionStart ();
        this.last_RegionEnd = this.myMatcher.regionEnd ();
        this.last_anchor = this.myMatcher.hasAnchoringBounds ();
        this.last_transparent = this.myMatcher.hasTransparentBounds ();
1666 1667 1668 1669
      }
    else if (this.myMatcher.hitEnd ())
      // the end of input is matched
      {
1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
        tmp2 = this.actBuffer.substring (this.actPos);
        if (tmp2.length() == 0)
          tmp2 = null;
        this.lastNextPos = this.actBuffer.length ();
        if (delete)
          {
            this.actPos = this.lastNextPos;
          }
        else
          {
            this.lastFound = tmp2;
            this.lastFoundPresent = true;
            this.lastPatternHash = pattern.hashCode ();
          }
        this.last_RegionStart = this.myMatcher.regionStart ();
        this.last_RegionEnd = this.myMatcher.regionEnd ();
        this.last_anchor = this.myMatcher.hasAnchoringBounds ();
        this.last_transparent = this.myMatcher.hasTransparentBounds ();
1688 1689 1690
      }
    else
      {
1691 1692 1693 1694
        /*
         * if no match found an Exception is throwed
         */
        throw new NoSuchElementException ();
1695 1696 1697 1698 1699 1700 1701
      }
    /*
     * change the Result only when a nextXXX() method was called, not if a hasNextXXX() method
     * is called
     */
    if (delete)
      {
1702
        this.actResult = this.myMatcher.toMatchResult ();
1703

1704
        this.matchValid = this.actResult != null;
1705 1706 1707
      }
    else
      {
1708
        this.lastResult = this.myMatcher.toMatchResult ();
1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732
      }

    this.skipped = this.doSkipp;
    this.doSkipp = false;

    return tmp2;
  }

  /**
   * Used to fill the String buffer from a source. Therfore the 3
   * possible sources are checked if they are not <code>null</code>
   * and this not used, otherwise the read method is called on the
   * source. If a charsetName is set and not <code>null</code> it is
   * applied to convert to String.
   */
  private void myFillBuffer ()
  {
    int len;
    String tmpStr;
    CharBuffer cb = null;
    ByteBuffer bb = null;

    if (this.bIS != null)
      {
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753
        try
        {
          len = this.bIS.read (this.tmpBuffer);
          if (len < 0)
            {
              return;
            }
          if (this.charsetName != null)
            {
              tmpStr = new String (this.tmpBuffer, 0, len, this.charsetName);
            }
          else
            {
              tmpStr = new String (this.tmpBuffer, 0, len);
            }
          this.actBuffer += tmpStr;
        }
        catch (IOException e)
        {
          this.lastIOException = e;
        }
1754 1755 1756
      }
    else if (this.readableSource != null)
      {
1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
        try
        {
          cb = CharBuffer.allocate (1000);
          this.needInput = true;
          len = this.readableSource.read (cb);
          if (len < 0)
            {
              return;
            }
          this.needInput = false;
          tmpStr = new String (cb.array ());
          this.actBuffer += tmpStr;
        }
        catch (IOException e)
        {
          this.lastIOException = e;
        }
1774 1775 1776
      }
    else if (this.rbcSource != null)
      {
1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800
        try
        {
          bb = ByteBuffer.allocate (1000);
          this.needInput = true;
          len = this.rbcSource.read (bb);
          this.needInput = false;
          if (len < 0)
            {
              return;
            }
          if (this.charsetName != null)
            {
              tmpStr = new String (bb.array (), 0, len, this.charsetName);
            }
          else
            {
              tmpStr = new String (bb.array (), 0, len);
            }
          this.actBuffer += tmpStr;
        }
        catch (IOException e)
        {
          this.lastIOException = e;
        }
1801 1802 1803 1804 1805 1806 1807 1808 1809
      }

    this.myMatcher.reset (this.actBuffer);
  }

  /**
   * A loop in which the {@link #myFillBuffer()} is called and checked
   * if the pattern is found in the matcher and if the buffersize
   * changes after the read.
1810
   *
1811 1812 1813 1814 1815 1816 1817 1818 1819
   * @param aktM
   *            The current Matcher.
   * @param pos
   *            Position from which the matcher should start matching.
   * @param found
   *            if already found.
   * @return <code> true </code> if the matcher has found a match.
   */
  private boolean myFillBuffer_loop (final Matcher aktM, final int pos,
1820
                                     boolean found)
1821 1822 1823 1824 1825
  {
    int tmp;

    tmp = this.actBuffer.length ();
    while (aktM.hitEnd ()
1826 1827
           && ((this.bIS != null) || (this.readableSource != null)
               || (this.rbcSource != null)))
1828
      {
1829 1830 1831 1832 1833 1834 1835
        myFillBuffer ();
        if (tmp == this.actBuffer.length ())
          {
            break;
          }
        found = aktM.find (pos);
        this.needInput = true;
1836 1837 1838 1839 1840 1841 1842 1843 1844 1845
      }
    return found;
  }

  /**
   * Used to find the given pattern in the given string before the
   * given horizon. Therfore the current matcher is copied, and
   * overwritten using the given pattern and the given Sting. <br>
   * After the search the original values are restored, and skipped is
   * set <code> true </code>.
1846
   *
1847 1848 1849 1850 1851 1852 1853 1854 1855
   * @param pattern
   *            Pattern which should be matched.
   * @param str
   *            The String in which the pattern should be matched.
   * @param horizon
   *            the horizon whithin the match should be, if 0 then it is ignored.
   * @return Returns the String in the given String that matches the pattern.
   */
  private String myFindPInStr (final Pattern pattern, final String str,
1856
                               final int horizon)
1857 1858 1859 1860 1861 1862 1863 1864
  {
    String rc = null;
    int curPos = this.actPos;
    Matcher aktMatcher = this.myMatcher;

    this.myMatcher = pattern.matcher (str);
    if (horizon > 0)
      {
1865 1866 1867
        this.myMatcher.useAnchoringBounds (true);
        this.myMatcher.useTransparentBounds (true);
        this.myMatcher.region (this.actPos, this.actPos + horizon);
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884
      }
    rc = myCoreNext (true, pattern);
    this.myMatcher = aktMatcher;

    this.actPos = curPos;
    this.skipped = true;

    return rc;
  }

  /**
   * Used by the {@link #hasNext(Pattern)} and {@link #next(Pattern)}
   * methods. Therfore a substring is taken first to the current
   * delimiter, afterwards the given pattern is searched in this
   * subsring.<br> Finally the current Buffer and matcher (which have
   * been temporarily changed) are set back.<br> <br> The {@link
   * #skipped} is set <code> true </code>.
1885
   *
1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921
   * @param pattern
   *            Pattern to find until the current delimiter.
   * @param delete
   *            Is <code> true </code> if a next method is called.<br>
   *            Is <code> false </code> if a hasNext method is called.
   * @return Returns the String which is returned by the public methods.
   */
  private String myNext (final Pattern pattern, final boolean delete)
  {
    String tmpStr;
    Matcher aktMatcher = this.myMatcher;
    String result;
    String currBuffer = this.actBuffer;
    int currAktPos;

    tmpStr = myCoreNext (delete, this.p);
    this.myMatcher = pattern.matcher (tmpStr);
    this.actBuffer = tmpStr;
    currAktPos = this.actPos;
    this.actPos = 0;
    result = myCoreNext (delete, pattern);
    this.actPos = currAktPos;

    this.actBuffer = currBuffer;
    this.myMatcher = aktMatcher;
    this.skipped = true;

    return result;
  }

  /**
   * Calls the next() method internally to get the next String, and
   * trys to apply a locale which is only applied if the radix is 10
   * and useLocale is <code> true </code>. Afterwards it is tried to
   * call the Constructor of a {@link BigInteger} with the given
   * radix.
1922
   *
1923 1924 1925 1926 1927 1928 1929 1930 1931
   * @param radix The radix to use.
   * @param delete If the found String should be removed from input or
   * not.
   * @param name name of "BigInteger" in case of an Error.
   * @return Returns the new BigInteger created if there is no Error.
   * @throws InputMismatchException
   *             If there is a {@link ParseException} or a {@link NumberFormatException}.
   */
  private BigInteger myNextBigInteger (final int radix, final boolean delete,
1932
                                       final String name)
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956
  {
    BigInteger rc;
    String tmp = myPrepareForNext (this.p, delete);

    try
    {
      tmp = myApplyLocale (tmp, radix);
      rc = new BigInteger (tmp, radix);
      return rc;
    }
    catch (NumberFormatException nfe)
    {
    }
    catch (ParseException e)
    {
    }
    throw new InputMismatchException (ERR_PREFIX + tmp + IS_NOT + name);
  }

  /**
   * Checks if the next String is either "true" or "false", otherwise
   * an {@link InputMismatchException} is thrown. It ignores the case
   * of the string so that "true" and "TRUE" and even "TrUe" are
   * accepted.
1957
   *
1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969
   * @param delete Should the found value be removed from the input or
   * not.
   * @return Returns the boolean value (if it is a boolean).
   * @throws InputMismatchException
   *             If the next String is not a boolean.
   */
  private boolean myNextBoolean (final boolean delete) throws
    InputMismatchException
  {
    String tmp = myPrepareForNext (this.p, delete);
    if (tmp.equalsIgnoreCase ("true"))
      {
1970
        return true;
1971 1972 1973
      }
    else if (tmp.equalsIgnoreCase ("false"))
      {
1974
        return false;
1975 1976 1977
      }
    else
      {
1978
        throw new InputMismatchException (ERR_PREFIX + tmp + NOT_BOOLEAN);
1979 1980 1981 1982 1983 1984 1985 1986 1987
      }
  }

  /**
   * Calls the {@link #myPrepareForNext(Pattern, boolean)} which calls
   * the {@link #myCoreNext(boolean, Pattern)} to return the next
   * String matching the current delimier. Afterwards it is tryed to
   * convert the String into a byte. Any Error will lead into a {@link
   * InputMismatchException}.
1988
   *
1989 1990 1991 1992 1993 1994
   * @param radix The radix to use.
   * @param delete Should the found String be removed from the input.
   * @return Returns the byte value of the String.
   * @throws InputMismatchException if the next String is not a byte.
   */
  private byte myNextByte (final int radix,
1995
                           final boolean delete) throws InputMismatchException
1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022
  {
    byte rc;
    String tmp = myPrepareForNext (this.p, delete);

      try
    {
      tmp = myApplyLocale (tmp, radix);
      rc = Byte.parseByte (tmp, radix);
      return rc;
    }
    catch (NumberFormatException nfe)
    {
    }
    catch (ParseException e)
    {
    }
    throw new InputMismatchException (ERR_PREFIX + tmp + NOT_BYTE);
  }

  /**
   * Tries to interpret the next String as a double value. To verify
   * if the double value is correct, it is converted back to a String
   * using the default Locale and this String is compared with the
   * String from which the double was converted. If the two Strings
   * don't match, an {@link InputMismatchException} is thrown.<br>
   * <br> The radix used is always 10 even if the global radix is
   * changed.
2023
   *
2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
   * @param delete Should the String be removed, if true it will be
   * also removed if the String is not a double value.
   * @return Returns the double value of the next String.
   * @throws InputMismatchException if the next String is not a
   * double.
   */
  private double myNextDouble (final boolean delete) throws
    InputMismatchException
  {
    double rc;
    String tmp = myPrepareForNext (this.p, delete);

      try
    {
      tmp = myApplyLocale (tmp, 10);
      rc = Double.parseDouble (tmp);
      if (("" + rc).equals (tmp))
2041 2042 2043
        {
          return rc;
        }
2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056
    }
    catch (ParseException e)
    {
    }
    throw new InputMismatchException (ERR_PREFIX + tmp + NOT_DOUBLE);
  }

  /**
   * Tries to interpret the next String as an int value. Therfore
   * {@link #myApplyLocale(String, int)} decides if the current Locale
   * should be applied or not and then the result is parsed using
   * {@link Integer#parseInt(String, int)}. Any Error will lead to an
   * {@link InputMismatchException}.
2057
   *
2058 2059 2060 2061 2062 2063 2064
   * @param radix The radix to use.
   * @param delete <code> true </code> if the String should be deleted
   * from the input.
   * @return Returns the int value of the String.
   * @throws InputMismatchException if the next String is not an int.
   */
  private int myNextInt (final int radix,
2065
                         final boolean delete) throws InputMismatchException
2066 2067 2068 2069 2070
  {
    int rc;
    String tmp = myPrepareForNext (this.p, delete);
    try
      {
2071 2072 2073
        tmp = myApplyLocale (tmp, radix);
        rc = Integer.parseInt (tmp, radix);
        return rc;
2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086
      }
    catch (NumberFormatException nfe)
    {
    }
    catch (ParseException e)
    {
    }
    throw new InputMismatchException (ERR_PREFIX + tmp + NOT_INT);
  }

  /**
   * Finds the next line using the {@link #NEW_LINE} constant which is
   * set to the system specific line seperator.
2087 2088 2089
   *
   * @param delete should the found line be deleted from the input.
   * @return the current line.
2090 2091 2092
   */
  private String myNextLine (final boolean delete)
  {
2093
    return myPrepareForNext (Pattern.compile (NEW_LINE), delete);
2094 2095 2096 2097 2098 2099
  }

  /**
   * Tries to interpret the next String as a long value with the given
   * radix. Therfore the {@link Long#parseLong(String, int)} is called
   * and every Error will lead into a {@link InputMismatchException}.
2100
   *
2101 2102 2103 2104 2105 2106
   * @param radix The radix to be used.
   * @param delete Should the found String be deleted from the input.
   * @return the long value of the next String.
   * @throws InputMismatchException if the next String is not a long.
   */
  private long myNextLong (final int radix,
2107
                           final boolean delete) throws InputMismatchException
2108 2109 2110 2111 2112 2113
  {
    long rc;
    String tmp = myPrepareForNext (this.p, delete);

    try
      {
2114 2115 2116
        tmp = myApplyLocale (tmp, radix);
        rc = Long.parseLong (tmp, radix);
        return rc;
2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131
      }
    catch (NumberFormatException nfe)
      {
      }
    catch (ParseException e)
      {
      }
    throw new InputMismatchException (ERR_PREFIX + tmp + NOT_LONG);
  }

  /**
   * Tries to interpret the next String as a short value with the
   * given radix. Therfore the {@link Short#parseShort(String, int)}
   * is called and every Error will lead into a {@link
   * InputMismatchException} .
2132
   *
2133 2134 2135 2136 2137 2138 2139 2140 2141
   * @param radix
   *            The radix to be used.
   * @param delete
   *            Should the found String be deleted from the input.
   * @return the long value of the next String.
   * @throws InputMismatchException
   *             if the next String is not a short.
   */
  private short myNextShort (final int radix,
2142
                             final boolean delete) throws
2143 2144 2145 2146
    InputMismatchException
  {
    short rc;
    String tmp = myPrepareForNext (this.p, delete);
2147

2148 2149
    try
      {
2150 2151 2152
        tmp = myApplyLocale (tmp, radix);
        rc = Short.parseShort (tmp, radix);
        return rc;
2153 2154 2155 2156 2157 2158 2159 2160
      }
    catch (NumberFormatException nfe)
      {
      }
    catch (ParseException e)
      {
      }
    throw new InputMismatchException (ERR_PREFIX + tmp +
2161
                                      "\" is not a short");
2162 2163 2164 2165 2166 2167
  }

  /**
   * Sets the current pattern to the given pattern and calls the
   * {@link #myCoreNext(boolean, Pattern)}. Finally sets the pattern
   * back to its old value.
2168
   *
2169 2170 2171 2172 2173 2174
   * @param aktPattern Pattern to be used for the next match.
   * @param delete Should the found String be deleted or not.
   * @return Return the String returned from {@link
   * #myCoreNext(boolean, Pattern)}.
   */
  private String myPrepareForNext (final Pattern aktPattern,
2175
                                   final boolean delete)
2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192
  {

    String rc;
    Pattern oldPattern = this.p;
    useDelimiter (aktPattern);

    rc = myCoreNext (delete, aktPattern);

    useDelimiter (oldPattern);

    return rc;
  }

  /**
   * Determinates if the last found can be used, so that after a
   * hasNextXXX the nextXXX has not to search if nothing has
   * changed.<br /> Used in {@link #myCoreNext(boolean, Pattern)}.
2193
   *
2194 2195 2196 2197 2198 2199
   * @param aktP The pattern which should be checked.
   * @return <code> true </code> if the searchresult is already ready.
   */
  private boolean shallUseLastFound (final Pattern aktP)
  {
    if (this.lastFoundPresent &&
2200 2201 2202 2203
        this.lastPatternHash == aktP.hashCode () &&
        this.last_RegionStart == this.myMatcher.regionStart () &&
        this.last_anchor == this.myMatcher.hasAnchoringBounds () &&
        this.last_transparent == this.myMatcher.hasTransparentBounds ())
2204
      {
2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218
        if (this.last_RegionEnd != this.myMatcher.regionEnd ())
          {
            int tmpVal =
              this.myMatcher.regionEnd () -
              this.last_RegionEnd - this.MAX_PREFIX;
            if (tmpVal > 0 && tmpVal < 20)
              {
                this.last_RegionEnd =
                  this.myMatcher.regionEnd ();
                return true;
              }
          }
        else
          return true;
2219 2220 2221 2222 2223
      }
    return false;
  }

}