jdk/test/java/io/BufferedInputStream/CountUpdate.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 1998 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  * @test
       
    26  * @bug 4054043
       
    27  * @summary Test bufferedinputstream when stream is interrupted
       
    28  *
       
    29  */
       
    30 
       
    31 import java.io.*;
       
    32 
       
    33 /**
       
    34  * This class tests to see if bufferinputstream updates count
       
    35  * when the stream is interrupted and restarted
       
    36  * It was adapted from a test class provided in the bug report
       
    37  *
       
    38  */
       
    39 
       
    40 public class CountUpdate {
       
    41 
       
    42     public static void main(String[] args) throws Exception {
       
    43         BufferBreaker breaker = new BufferBreaker();
       
    44         BufferedInputStream in = new BufferedInputStream(breaker, 1000);
       
    45 
       
    46         byte b[] = new byte[100];
       
    47         int total = 0;
       
    48 
       
    49         for (int i=0; i<5; i++) {
       
    50 
       
    51             if (i>0) breaker.breakIt = true;
       
    52             try {
       
    53                 int n = in.read(b);
       
    54                 total += n;
       
    55                 //System.out.print("read "+n+" bytes: [");
       
    56                 //System.out.write(b, 0, n);
       
    57                 //System.out.println("]");
       
    58             }
       
    59             catch (IOException e) {
       
    60                 //System.out.println(e);
       
    61             }
       
    62         }
       
    63 
       
    64         if (total>7)
       
    65             throw new RuntimeException(
       
    66                             "BufferedInputStream did not reset count.");
       
    67     }
       
    68 }
       
    69 
       
    70 class BufferBreaker extends InputStream {
       
    71     public boolean breakIt = false;
       
    72 
       
    73     public int read() {
       
    74         return 'x';
       
    75     }
       
    76 
       
    77     public static final byte[] buffer = {(byte)'a',
       
    78                                          (byte)'b',
       
    79                                          (byte)'c',
       
    80                                          (byte)'d',
       
    81                                          (byte)'e',
       
    82                                          (byte)'f',
       
    83                                          (byte)'g'};
       
    84 
       
    85     public int read(byte b[]) throws IOException {
       
    86         return read(b, 0, b.length);
       
    87     }
       
    88 
       
    89     public int read(byte b[], int off, int len) throws IOException {
       
    90         if (breakIt) throw new IOException("BREAK");
       
    91         if (len > buffer.length) len = buffer.length;
       
    92         System.arraycopy(buffer, 0, b, off, len);
       
    93         return len;
       
    94     }
       
    95 
       
    96     public long skip(long n) {
       
    97         return 0;
       
    98     }
       
    99 
       
   100     public int available() {
       
   101         return 0;
       
   102     }
       
   103 
       
   104 }