J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1997 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 | public class ABCInputStream extends InputStream { |
| 30 | |
| 31 | int len; |
| 32 | int chunk; |
| 33 | int count = 0; |
| 34 | char next = firstChar(); |
| 35 | |
| 36 | ABCInputStream(int len) { |
| 37 | this(len, len); |
| 38 | } |
| 39 | |
| 40 | ABCInputStream(int len, int chunk) { |
| 41 | this.len = len; |
| 42 | this.chunk = chunk; |
| 43 | } |
| 44 | |
| 45 | static char firstChar() { |
| 46 | return 'a'; |
| 47 | } |
| 48 | |
| 49 | static char nextChar(char c) { |
| 50 | if (c == 'z') |
| 51 | return '0'; |
| 52 | else if (c == '9') |
| 53 | return 'a'; |
| 54 | else |
| 55 | return (char)(c + 1); |
| 56 | } |
| 57 | |
| 58 | public int read() { |
| 59 | if (count >= len) |
| 60 | return -1; |
| 61 | char c = next; |
| 62 | next = nextChar(c); |
| 63 | count++; |
| 64 | return (byte) c; |
| 65 | } |
| 66 | |
| 67 | public int read(byte buf[], int off, int len) { |
| 68 | int n = (len > chunk) ? chunk : len; |
| 69 | for (int i = off; i < off + n; i++) { |
| 70 | int c = read(); |
| 71 | if (c == -1) { |
| 72 | if (i > off) |
| 73 | return i - off; |
| 74 | else |
| 75 | return -1; |
| 76 | } |
| 77 | buf[i] = (byte) c; |
| 78 | } |
| 79 | return n; |
| 80 | } |
| 81 | |
| 82 | public int available() { |
| 83 | int remaining = len - count; |
| 84 | return (remaining > chunk) ? chunk : remaining; |
| 85 | } |
| 86 | |
| 87 | public void close() throws IOException { |
| 88 | if (len == 0) |
| 89 | throw new IOException("Already closed"); |
| 90 | len = 0; |
| 91 | } |
| 92 | |
| 93 | } |