Path: blob/master/test/jdk/java/io/charStreams/ABCInputStream.java
41149 views
/*1* Copyright (c) 1997, 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/**/2425import java.io.*;262728public class ABCInputStream extends InputStream {2930int len;31int chunk;32int count = 0;33char next = firstChar();3435ABCInputStream(int len) {36this(len, len);37}3839ABCInputStream(int len, int chunk) {40this.len = len;41this.chunk = chunk;42}4344static char firstChar() {45return 'a';46}4748static char nextChar(char c) {49if (c == 'z')50return '0';51else if (c == '9')52return 'a';53else54return (char)(c + 1);55}5657public int read() {58if (count >= len)59return -1;60char c = next;61next = nextChar(c);62count++;63return (byte) c;64}6566public int read(byte buf[], int off, int len) {67int n = (len > chunk) ? chunk : len;68for (int i = off; i < off + n; i++) {69int c = read();70if (c == -1) {71if (i > off)72return i - off;73else74return -1;75}76buf[i] = (byte) c;77}78return n;79}8081public int available() {82int remaining = len - count;83return (remaining > chunk) ? chunk : remaining;84}8586public void close() throws IOException {87if (len == 0)88throw new IOException("Already closed");89len = 0;90}9192}939495