Path: blob/master/test/jdk/java/io/BufferedInputStream/SkipTest.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* @test 1.1 98/01/1225* @bug 402229426* @summary Test bufferedinputstream for data loss during skip27*28*/2930import java.io.*;31import java.util.*;3233/**34* This class tests to see if bufferinputstream can be reset35* to recover data that was skipped over when the buffer did36* not contain all the bytes to be skipped37*/38public class SkipTest {3940public static void main(String[] args) throws Exception {41long skipped = 0;4243// Create a tiny buffered stream so it can be easily44// set up to contain only some of the bytes to skip45DataSupplier source = new DataSupplier();46BufferedInputStream in = new BufferedInputStream(source, 4);4748// Set up data to be skipped and recovered49// the skip must be longer than the buffer size50in.mark(30);51while (skipped < 15) {52skipped += in.skip(15-skipped);53}54int nextint = in.read();55in.reset();5657// Resume reading and see if data was lost58nextint = in.read();5960if (nextint != 'a')61throw new RuntimeException("BufferedInputStream skip lost data");62}63}646566class DataSupplier extends InputStream {6768private int aposition=0;6970public int read() {71return 'x';72}7374public long skip(long n) {75aposition += (int) n;76return n;77}7879public static final byte[] buffer = {(byte)'a',(byte)'b',(byte)'c',80(byte)'d',(byte)'e',(byte)'f',(byte)'g',(byte)'h',(byte)'i',81(byte)'j',(byte)'k',(byte)'l',(byte)'m',(byte)'n',(byte)'o',82(byte)'p',(byte)'q',(byte)'r',(byte)'s',(byte)'t',(byte)'u',83(byte)'v',(byte)'w',(byte)'x',(byte)'y',(byte)'z'84};8586public int read(byte b[]) throws IOException {87return read(b, 0, b.length);88}8990public int read(byte b[], int off, int len) throws IOException {91if (len > buffer.length) len = buffer.length;92System.arraycopy(buffer, aposition, b, off, len);93return len;94}9596}979899