J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2000 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 | /* @test |
| 25 | * @bug 4329985 |
| 26 | * @summary Ensure that BufferedReader's ready() method handles the new line |
| 27 | * character following the carriage return correctly and returns the right |
| 28 | * value so that a read operation after a ready() does not block unnecessarily. |
| 29 | */ |
| 30 | import java.io.*; |
| 31 | |
| 32 | public class Ready { |
| 33 | |
| 34 | public static void main(String[] args) throws IOException { |
| 35 | BufferedReader reader; |
| 36 | String[] strings = { |
| 37 | "LF-Only\n", |
| 38 | "LF-Only\n", |
| 39 | "CR/LF\r\n", |
| 40 | "CR/LF\r\n", |
| 41 | "CR-Only\r", |
| 42 | "CR-Only\r", |
| 43 | "CR/LF line\r\nMore data.\r\n", |
| 44 | "CR/LF line\r\nMore data.\r\n" |
| 45 | }; |
| 46 | |
| 47 | // The buffer sizes are chosen such that the boundary conditions are |
| 48 | // tested. |
| 49 | int[] bufsizes = { 7, 8, 6, 5, 7, 8, 11, 10}; |
| 50 | |
| 51 | for (int i = 0; i < strings.length; i++) { |
| 52 | reader = new BufferedReader(new BoundedReader(strings[i]), |
| 53 | bufsizes[i]); |
| 54 | while (reader.ready()) { |
| 55 | String str = reader.readLine(); |
| 56 | System.out.println("read>>" + str); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | |
| 62 | |
| 63 | private static class BoundedReader extends Reader{ |
| 64 | |
| 65 | private char[] content; |
| 66 | private int limit; |
| 67 | private int pos = 0; |
| 68 | |
| 69 | public BoundedReader(String content) { |
| 70 | this.limit = content.length(); |
| 71 | this.content = new char[limit]; |
| 72 | content.getChars(0, limit, this.content, 0); |
| 73 | } |
| 74 | |
| 75 | public int read() throws IOException { |
| 76 | if (pos >= limit) |
| 77 | throw new RuntimeException("Hit infinite wait condition"); |
| 78 | return content[pos++]; |
| 79 | } |
| 80 | |
| 81 | public int read(char[] buf, int offset, int length) |
| 82 | throws IOException |
| 83 | { |
| 84 | if (pos >= limit) |
| 85 | throw new RuntimeException("Hit infinite wait condition"); |
| 86 | int oldPos = pos; |
| 87 | int readlen = (length > (limit - pos)) ? (limit - pos) : length; |
| 88 | for (int i = offset; i < readlen; i++) { |
| 89 | buf[i] = (char)read(); |
| 90 | } |
| 91 | |
| 92 | return (pos - oldPos); |
| 93 | } |
| 94 | |
| 95 | public void close() {} |
| 96 | |
| 97 | public boolean ready() { |
| 98 | if (pos < limit) |
| 99 | return true; |
| 100 | else |
| 101 | return false; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | } |