jdk/test/java/io/StreamTokenizer/Comment.java
changeset 5810 e83d67ad8c96
parent 5506 202f599c92aa
child 7668 d4a77089c587
equal deleted inserted replaced
5809:6e38efd0293f 5810:e83d67ad8c96
    39         int slashIsCommentStart = 1;
    39         int slashIsCommentStart = 1;
    40         int slashSlashComment = 2;
    40         int slashSlashComment = 2;
    41         int slashStarComment = 4;
    41         int slashStarComment = 4;
    42 
    42 
    43         for (int i = 0; i < 8 ; i++) {
    43         for (int i = 0; i < 8 ; i++) {
    44             StreamTokenizer st = new StreamTokenizer(new FileReader(f));
    44             FileReader reader = new FileReader(f);
       
    45             try {
       
    46                 StreamTokenizer st = new StreamTokenizer(reader);
    45 
    47 
    46             /* decide the state of this run */
    48                 /* decide the state of this run */
    47             boolean slashCommentFlag = ((i & slashIsCommentStart) != 0);
    49                 boolean slashCommentFlag = ((i & slashIsCommentStart) != 0);
    48             boolean slashSlashCommentFlag = ((i & slashSlashComment) != 0);
    50                 boolean slashSlashCommentFlag = ((i & slashSlashComment) != 0);
    49             boolean slashStarCommentFlag = ((i & slashStarComment) != 0);
    51                 boolean slashStarCommentFlag = ((i & slashStarComment) != 0);
    50 
    52 
    51             /* set the initial state of the tokenizer */
    53                 /* set the initial state of the tokenizer */
    52             if (!slashCommentFlag) {
    54                 if (!slashCommentFlag) {
    53                 st.ordinaryChar('/');
    55                     st.ordinaryChar('/');
    54             }
    56                 }
    55             st.slashSlashComments(slashSlashCommentFlag);
    57                 st.slashSlashComments(slashSlashCommentFlag);
    56             st.slashStarComments(slashStarCommentFlag);
    58                 st.slashStarComments(slashStarCommentFlag);
    57 
    59 
    58             /* now go throgh the input file */
    60                 /* now go throgh the input file */
    59             while(st.nextToken() != StreamTokenizer.TT_EOF)
    61                 while(st.nextToken() != StreamTokenizer.TT_EOF)
    60             {
    62                 {
    61                 String token = st.sval;
    63                     String token = st.sval;
    62                 if (token == null) {
    64                     if (token == null) {
    63                     continue;
    65                         continue;
    64                 } else {
    66                     } else {
    65                     if ((token.compareTo("Error1") == 0) && slashStarCommentFlag) {
    67                         if ((token.compareTo("Error1") == 0) && slashStarCommentFlag) {
    66                         throw new Exception("Failed to pass one line C comments!");
    68                             throw new Exception("Failed to pass one line C comments!");
    67                     }
    69                         }
    68                     if ((token.compareTo("Error2") == 0) && slashStarCommentFlag) {
    70                         if ((token.compareTo("Error2") == 0) && slashStarCommentFlag) {
    69                         throw new Exception("Failed to pass multi line C comments!");
    71                             throw new Exception("Failed to pass multi line C comments!");
    70                     }
    72                         }
    71                     if ((token.compareTo("Error3") == 0) && slashSlashCommentFlag) {
    73                         if ((token.compareTo("Error3") == 0) && slashSlashCommentFlag) {
    72                         throw new Exception("Failed to pass C++ comments!");
    74                             throw new Exception("Failed to pass C++ comments!");
    73                     }
    75                         }
    74                     if ((token.compareTo("Error4") == 0) && slashCommentFlag) {
    76                         if ((token.compareTo("Error4") == 0) && slashCommentFlag) {
    75                         throw new Exception("Failed to pass / comments!");
    77                             throw new Exception("Failed to pass / comments!");
       
    78                         }
    76                     }
    79                     }
    77                 }
    80                 }
       
    81             } finally {
       
    82                 reader.close();
    78             }
    83             }
    79         }
    84         }
    80     }
    85     }
    81 }
    86 }