/* * Copyright (c) 2001 Matthew Feldt. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided the copyright notice above is * retained. * * THIS SOFTWARE IS PROVIDED ''AS IS'' AND WITHOUT ANY EXPRESSED OR * IMPLIED WARRANTIES. */ /** * TestReader.java * * Java Examples In A Nutshell Copyright (c) 2000 David Flanagan * Exercise 3-8: * Write a simple cubclass of Reader named TestReader that returns the same * character (a character passed to the constructor) over and over. The stream * should never reach EOF. Write a trivial subclass of Writer named NullWriter * that simply discards any output sent to it. Streams like these are occasionally * useful for testing and other purposes. Write a test program that reads from the * TestReader and sends its output to a PrintWriter wrapped around a NullWriter. * * @author Matthew Feldt * @version 1.0, 02/12/2001 11:11 */ package com.feldt.examples.io; import java.io.*; import com.feldt.examples.io.NullWriter; public class TestReader extends Reader { private char ch; TestReader(char c) { ch = c; } /** read a single character, returns ch */ public int read() { return (int)ch; } /** read characters in to an array, fill cbuf with ch */ public int read(char[] cbuf) { for (int i = 0; i < cbuf.length; i++) cbuf[i] = ch; return cbuf.length; } /** * read characters in to a portion of an array, * fills cbuf from off to len with ch */ public int read(char[] cbuf, int off, int len) throws IOException { int bytes_read = 0; // protect the user from themself if ((off < 0) || (off > cbuf.length) || (len < 0) || ((off + len) > cbuf.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return 0; } for (int pos = off, i = 0; i < len; i++, pos++) { cbuf[pos] = ch; bytes_read++; } return bytes_read; } public void close() {} public void mark(int readAheadLimit) throws IOException {} public boolean markSupported() { return false; } // don't support marking public boolean ready() { return true; } // always ready public void reset() throws IOException {} public long skip(long n) throws IOException { return n; } // always true /** test class */ static class Test { public static void main (String args[]) { Reader in; PrintWriter out = new PrintWriter(new NullWriter()); char[] buf = new char[8]; try { in = new TestReader('a'); // test read() System.out.println("in.read(): " + (char)in.read()); in.read(buf); // test read(char[]) System.out.print("in.read(buf): "); System.out.println(buf); in = new TestReader('z'); // test read(char[], int, int) in.read(buf, 7, 1); System.out.print("in.read(buf, 7, 1): "); System.out.println(buf); in = new TestReader('m'); // second test read(char[], int, int) in.read(buf, 2, 3); System.out.print("in.read(buf, 2, 3): "); System.out.println(buf); try { in.read(buf, 10, 5); // test bounds checking } catch(IndexOutOfBoundsException e) { System.out.println("Got IndexOutOfBoundsException as expected."); } } catch(IOException e) {} } } }