Path: blob/master/test/jdk/java/io/BufferedInputStream/CountUpdate.java
41149 views
/*1* Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 405404326* @summary Test bufferedinputstream when stream is interrupted27*28*/2930import java.io.*;3132/**33* This class tests to see if bufferinputstream updates count34* when the stream is interrupted and restarted35* It was adapted from a test class provided in the bug report36*37*/3839public class CountUpdate {4041public static void main(String[] args) throws Exception {42BufferBreaker breaker = new BufferBreaker();43BufferedInputStream in = new BufferedInputStream(breaker, 1000);4445byte b[] = new byte[100];46int total = 0;4748for (int i=0; i<5; i++) {4950if (i>0) breaker.breakIt = true;51try {52int n = in.read(b);53total += n;54//System.out.print("read "+n+" bytes: [");55//System.out.write(b, 0, n);56//System.out.println("]");57}58catch (IOException e) {59//System.out.println(e);60}61}6263if (total>7)64throw new RuntimeException(65"BufferedInputStream did not reset count.");66}67}6869class BufferBreaker extends InputStream {70public boolean breakIt = false;7172public int read() {73return 'x';74}7576public static final byte[] buffer = {(byte)'a',77(byte)'b',78(byte)'c',79(byte)'d',80(byte)'e',81(byte)'f',82(byte)'g'};8384public int read(byte b[]) throws IOException {85return read(b, 0, b.length);86}8788public int read(byte b[], int off, int len) throws IOException {89if (breakIt) throw new IOException("BREAK");90if (len > buffer.length) len = buffer.length;91System.arraycopy(buffer, 0, b, off, len);92return len;93}9495public long skip(long n) {96return 0;97}9899public int available() {100return 0;101}102103}104105106