jdk/test/java/io/charStreams/LineSink.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 1997-2000 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /**/
       
    25 
       
    26 import java.io.*;
       
    27 
       
    28 
       
    29 class LineSink implements Runnable {
       
    30 
       
    31     DataInputStream ui;
       
    32     BufferedReader ti;
       
    33     int count;
       
    34     PrintWriter log;
       
    35 
       
    36     public LineSink(InputStream us, BufferedReader ts,
       
    37                     int count, PrintWriter log)
       
    38         throws IOException
       
    39         {
       
    40             this(us, ts, log);
       
    41             this.count = count;
       
    42         }
       
    43 
       
    44     public LineSink(InputStream us, BufferedReader ts, PrintWriter log)
       
    45         throws IOException
       
    46         {
       
    47             ui = new DataInputStream(us);
       
    48             ti = ts;
       
    49             this.count = Integer.MAX_VALUE;
       
    50             this.log = log;
       
    51         }
       
    52 
       
    53     private String readUTFLine() throws IOException {
       
    54         String s;
       
    55         try {
       
    56             s = ui.readUTF();
       
    57         }
       
    58         catch (EOFException x) {
       
    59             return null;
       
    60         }
       
    61         return s;
       
    62     }
       
    63 
       
    64     public void run() {
       
    65         try {
       
    66             for (int ln = 0; ln < count; ln++) {
       
    67                 String us = readUTFLine();
       
    68                 if (us == null) {
       
    69                     if (count < Integer.MAX_VALUE)
       
    70                         throw new RuntimeException("Premature EOF on UTF stream");
       
    71                     log.println("EOF on UTF stream");
       
    72                     break;
       
    73                 }
       
    74 
       
    75                 String ts = ti.readLine();
       
    76                 if (ts == null) {
       
    77                     if (count < Integer.MAX_VALUE)
       
    78                         throw new RuntimeException("Premature EOF on char stream");
       
    79                     log.println("EOF on char stream");
       
    80                     break;
       
    81                 }
       
    82 
       
    83                 if (us.length() != ts.length()) {
       
    84                     log.println("Length mismatch: us = \""
       
    85                                 + us + "\", ts = \""
       
    86                                 + ts + "\"");
       
    87                     throw new RuntimeException("Line " + ln +
       
    88                                                ": Length mismatch: " +
       
    89                                                us.length() + " " + ts.length());
       
    90                 }
       
    91 
       
    92                 for (int i = 0; i < us.length(); i++) {
       
    93                     if (us.charAt(i) != ts.charAt(i))
       
    94                         throw new RuntimeException("Line " + ln +
       
    95                                                    ": Char mismatch: [" + i + "] " +
       
    96                                                    Integer.toHexString(us.charAt(i)) +
       
    97                                                    " " + Integer.toHexString(ts.charAt(i)));
       
    98                 }
       
    99                 log.println(ln + " " + ts.length());
       
   100             }
       
   101 
       
   102             if (readUTFLine() != null)
       
   103                 throw new RuntimeException("Expected EOF on UTF stream");
       
   104             if (ti.readLine() != null)
       
   105                 throw new RuntimeException("Expected EOF on char stream");
       
   106         } catch (IOException x) {
       
   107             throw new RuntimeException("Unexpected IOException: " + x);
       
   108         }
       
   109     }
       
   110 
       
   111 }