1 18 package com.Ostermiller.util; 19 20 import java.io.*; 21 22 30 class ConcatTests { 31 public static void main(String [] args){ 32 try { 33 ConcatReader cr = new ConcatReader( 34 new Reader[]{ 35 new StringReader("1"), 36 new StringReader("two"), 37 new StringReader(""), 38 new StringReader("4"), 39 new StringReader("five"), 40 new StringReader("six"), 41 new StringReader("seven"), 42 } 43 ); 44 if (!cr.ready()) throw new Exception ("Not Ready"); 45 read(cr, '1'); 46 read(cr, 't'); 47 read(cr, 'w'); 48 read(cr, 'o'); 49 if (!cr.ready()) throw new Exception ("Not Ready"); 50 read(cr, '4'); 51 read(cr, "fiv"); 52 skip(cr, 2); 53 read(cr, "i"); 54 read(cr, "xseven"); 55 if (cr.read() != -1) throw new Exception ("Read did not terminate"); 56 if (cr.read() != -1) throw new Exception ("Didn't stay closed"); 57 cr.close(); 58 59 final ConcatReader cr1 = new ConcatReader(); 60 if (cr1.ready()) throw new Exception ("Ready"); 61 cr1.addReader(new StringReader("one")); 62 read(cr1, 'o'); 63 cr1.addReader(new StringReader("two")); 64 read(cr1, "netwo"); 65 new Thread (){ 66 public void run(){ 67 try { 68 Thread.sleep(1000); 69 } catch (InterruptedException ix){ 70 } 71 cr1.addReader(new StringReader("three")); 72 } 73 }.start(); 74 read(cr1, "three"); 75 cr1.lastReaderAdded(); 76 if (cr1.read() != -1) throw new Exception ("Read did not terminate"); 77 78 ConcatInputStream cis = new ConcatInputStream( 79 new InputStream[]{ 80 new ByteArrayInputStream(new byte[]{'1'}), 81 new ByteArrayInputStream(new byte[]{'t','w','o'}), 82 new ByteArrayInputStream(new byte[]{}), 83 new ByteArrayInputStream(new byte[]{'4'}), 84 new ByteArrayInputStream(new byte[]{'f','i','v','e'}), 85 new ByteArrayInputStream(new byte[]{'s','i','x'}), 86 new ByteArrayInputStream(new byte[]{'s','e','v','e','n'}), 87 } 88 ); 89 if (cis.available() <= 0) throw new Exception ("Not Ready"); 90 read(cis, '1'); 91 read(cis, 't'); 92 read(cis, 'w'); 93 read(cis, 'o'); 94 read(cis, '4'); 95 read(cis, "fivesi"); 96 if (cis.available() <= 0) throw new Exception ("Not Ready"); 97 read(cis, "xseven"); 98 if (cis.read() != -1) throw new Exception ("Read did not terminate"); 99 if (cis.read() != -1) throw new Exception ("Didn't stay closed"); 100 101 final ConcatInputStream cis1 = new ConcatInputStream(); 102 if (cis.available() != 0) throw new Exception ("Ready"); 103 cis1.addInputStream(new ByteArrayInputStream("one".getBytes("ASCII"))); 104 read(cis1, 'o'); 105 cis1.addInputStream(new ByteArrayInputStream("two".getBytes("ASCII"))); 106 read(cis1, "netwo"); 107 new Thread (){ 108 public void run(){ 109 try { 110 Thread.sleep(1000); 111 } catch (InterruptedException ix){ 112 } 113 try { 114 cis1.addInputStream(new ByteArrayInputStream("three".getBytes("ASCII"))); 115 } catch (Exception x){ 116 System.err.println(x.getMessage()); 117 x.printStackTrace(); 118 System.exit(1); 119 } 120 } 121 }.start(); 122 read(cis1, "three"); 123 cis1.lastInputStreamAdded(); 124 if (cis1.read() != -1) throw new Exception ("Read did not terminate"); 125 126 } catch (Exception x){ 127 System.err.println(x.getMessage()); 128 x.printStackTrace(); 129 System.exit(1); 130 } 131 System.exit(0); 132 } 133 134 private static void skip(Reader in, int n) throws Exception { 135 int s = 0; 136 while (s<n){ 137 s+=in.skip(n-s); 138 } 139 } 140 141 142 private static void read(Reader in, char expected) throws Exception { 143 int c = in.read(); 144 if (c != (int)expected) throw new Exception ("Expected to read " + expected + " but read " + (char)c); 145 } 146 147 private static void read(InputStream in, char expected) throws Exception { 148 int c = in.read(); 149 if (c != (int)expected) throw new Exception ("Expected to read " + expected + " but read " + (char)c); 150 } 151 152 private static void read(Reader in, String expected) throws Exception { 153 int totalRead = 0; 154 while (totalRead < expected.length()){ 155 char[] buffer = new char[expected.length()-totalRead]; 156 int read = in.read(buffer); 157 if (read == -1) throw new Exception ("Read terminated early"); 158 if (!expected.substring(totalRead, totalRead+read).equals(new String (buffer,0,read))){ 159 throw new Exception ("Expected to read " + expected.substring(totalRead, totalRead+read) + " but read " + new String (buffer,0,read)); 160 } 161 totalRead+=read; 162 } 163 } 164 165 private static void read(InputStream in, String expected) throws Exception { 166 int totalRead = 0; 167 while (totalRead < expected.length()){ 168 byte[] buffer = new byte[expected.length()-totalRead]; 169 int read = in.read(buffer); 170 if (read == -1) throw new Exception ("Read terminated early"); 171 if (!expected.substring(totalRead, totalRead+read).equals(new String (buffer,0,read,"ASCII"))){ 172 throw new Exception ("Expected to read " + expected.substring(totalRead, totalRead+read) + " but read " + new String (buffer,0,read,"ASCII")); 173 } 174 totalRead+=read; 175 } 176 } 177 178 } | Popular Tags |