1 package com.quadcap.io; 2 3 40 41 import java.io.*; 42 import com.quadcap.util.*; 43 44 49 public class Test extends com.quadcap.util.Test { 50 void testDot(String s) throws IOException { 51 ByteArrayOutputStream b1 = new ByteArrayOutputStream(); 52 DotStuffOutputStream os = new DotStuffOutputStream(b1); 53 os.write(s.getBytes()); 54 os.close(); 55 String dotrep = b1.toString(); 56 57 ByteArrayInputStream b2 = new ByteArrayInputStream(dotrep.getBytes()); 58 DotStuffInputStream is = new DotStuffInputStream(b2); 59 ByteArrayOutputStream b3 = new ByteArrayOutputStream(); 60 int c; 61 while ((c = is.read()) >= 0) { b3.write(c); } 62 String act = b3.toString(); 63 testAssert(s.equals(act)); 64 } 65 66 public void testDotStuffStreams(String [] args) throws IOException { 67 testDot("a\r\n.b\n"); 68 testDot(".\r\n"); 69 testDot( 70 "hello\r\n.goodbye\r\nlala\r\n\r\n..\r\n\r\n..ar\r\nfoo\r\n.\r\n" 71 ); 72 73 testDot(""); 74 testDot("."); 75 testDot(".\n"); 76 testDot("\n"); 77 testDot("\r\n"); 78 } 83 84 public void testLimitedOutputStream(String [] args) { 85 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 86 ByteArrayOutputStream bos2 = new ByteArrayOutputStream(); 87 LimitedOutputStream los = new LimitedOutputStream(bos, 25); 88 boolean hitLimit = false; 89 try { 90 for (int i = 0; i < 10; i++) { 91 bos2.write(Util.bytes(" " + i)); 92 los.write(Util.bytes(" " + i)); 93 } 94 } catch (IOException e) { 95 if (e instanceof LimitExceededException) { 96 hitLimit = true; 97 String b1 = bos.toString(); 98 if (b1.length() != 25) { 99 testError("wrong length: " + b1.length()); 100 } 101 String b2 = bos2.toString().substring(0, 25); 102 if (!b1.equals(b2)) { 103 testError("'" + b1 + "' != '" + b2 + "'"); 104 } 105 } else { 106 testError("wrong exception: " + e.toString()); 107 Debug.print(e); 108 } 109 } 110 if (!hitLimit) { 111 testError("no exception"); 112 } 113 114 } 115 116 public void testNullOutputStream(String [] args) { 117 for (int i = 0; i < 10; i++) { 118 NullOutputStream.Null.println(" " + i); 119 } 120 } 121 122 public void testBase64(String [] args) throws IOException { 123 for (int i = 0; i < 5; i++) { 124 SaveRestoreStream sr = new SaveRestoreStream(); 125 OutputStream out = sr.getOutputStream(); 126 Base64OutputStream bout = new Base64OutputStream(out); 127 for (int j = 0; j < i; j++) { 128 bout.write(j + 16); 129 } 130 bout.finish(); 131 out.close(); 132 InputStream is = sr.getInputStream(); 133 Base64InputStream bin = new Base64InputStream(is); 134 for (int j = 0; j <= i; j++) { 135 writer.println("" + i + ": " + j + ": " + bin.read()); 136 } 137 bin.close(); 138 sr.close(); 139 } 140 } 141 142 public void testSaveRestore(String [] args) throws IOException { 143 for (int size = 1; size < (1 << 16); size <<= 1) { 144 SaveRestoreStream so = new SaveRestoreStream(1000); 145 OutputStream os = so.getOutputStream(); 146 for (int i = 0; i < size; i += 4) { 147 os.write((i >> 24) & 0xff); 148 os.write((i >> 16) & 0xff); 149 os.write((i >> 8) & 0xff); 150 os.write(i & 0xff); 151 } 152 InputStream is = so.getInputStream(); 153 for (int i = 0; i < size; i += 4) { 154 int t = is.read(); 155 t <<= 8; t |= (is.read() & 0xff); 156 t <<= 8; t |= (is.read() & 0xff); 157 t <<= 8; t |= (is.read() & 0xff); 158 testAssert(t == i); 159 } 160 } 161 } 162 163 public void testRecursiveFileIterator(String [] args) 164 throws Exception 165 { 166 FileFilter filter = new FileFilter() { 167 public boolean accept(File f) { 168 if (f.isDirectory()) { 169 return !f.getName().equals("CVS"); 170 } else { 171 return (f.getName().endsWith(".java")); 172 } 173 } 174 }; 175 RecursiveFileIterator f = 176 new RecursiveFileIterator(new File("."), filter); 177 while (f.hasNext()) { 178 File fil = (File)f.next(); 179 writer.println(fil.getName()); 180 } 181 } 182 183 public static void main(String args[]) { 184 Test t = new Test(); 185 t.test(args); 186 } 187 188 } 189 190 | Popular Tags |