00001
package com.quadcap.io;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.*;
00042
import com.quadcap.util.*;
00043
00044
00045
00046
00047
00048
00049 public class Test extends com.quadcap.util.
Test {
00050 void testDot(String s)
throws IOException {
00051 ByteArrayOutputStream b1 =
new ByteArrayOutputStream();
00052
DotStuffOutputStream os =
new DotStuffOutputStream(b1);
00053 os.
write(s.getBytes());
00054 os.close();
00055 String dotrep = b1.toString();
00056
00057 ByteArrayInputStream b2 =
new ByteArrayInputStream(dotrep.getBytes());
00058
DotStuffInputStream is =
new DotStuffInputStream(b2);
00059 ByteArrayOutputStream b3 =
new ByteArrayOutputStream();
00060
int c;
00061
while ((c = is.
read()) >= 0) { b3.write(c); }
00062 String act = b3.toString();
00063 testAssert(s.equals(act));
00064 }
00065
00066 public void testDotStuffStreams(String[] args)
throws IOException {
00067 testDot(
"a\r\n.b\n");
00068 testDot(
".\r\n");
00069 testDot(
00070
"hello\r\n.goodbye\r\nlala\r\n\r\n..\r\n\r\n..ar\r\nfoo\r\n.\r\n"
00071 );
00072
00073 testDot(
"");
00074 testDot(
".");
00075 testDot(
".\n");
00076 testDot(
"\n");
00077 testDot(
"\r\n");
00078
00079
00080
00081
00082 }
00083
00084 public void testLimitedOutputStream(String[] args) {
00085 ByteArrayOutputStream bos =
new ByteArrayOutputStream();
00086 ByteArrayOutputStream bos2 =
new ByteArrayOutputStream();
00087
LimitedOutputStream los =
new LimitedOutputStream(bos, 25);
00088
boolean hitLimit =
false;
00089
try {
00090
for (
int i = 0; i < 10; i++) {
00091 bos2.write(
Util.bytes(
" " + i));
00092 los.
write(
Util.bytes(
" " + i));
00093 }
00094 }
catch (IOException e) {
00095
if (e instanceof
LimitExceededException) {
00096 hitLimit =
true;
00097 String b1 = bos.toString();
00098
if (b1.length() != 25) {
00099 testError(
"wrong length: " + b1.length());
00100 }
00101 String b2 = bos2.toString().substring(0, 25);
00102
if (!b1.equals(b2)) {
00103 testError(
"'" + b1 +
"' != '" + b2 +
"'");
00104 }
00105 }
else {
00106 testError(
"wrong exception: " + e.toString());
00107
Debug.print(e);
00108 }
00109 }
00110
if (!hitLimit) {
00111 testError(
"no exception");
00112 }
00113
00114 }
00115
00116 public void testNullOutputStream(String[] args) {
00117
for (
int i = 0; i < 10; i++) {
00118
NullOutputStream.Null.println(
" " + i);
00119 }
00120 }
00121
00122 public void testBase64(String[] args)
throws IOException {
00123
for (
int i = 0; i < 5; i++) {
00124
SaveRestoreStream sr =
new SaveRestoreStream();
00125 OutputStream out = sr.
getOutputStream();
00126
Base64OutputStream bout =
new Base64OutputStream(out);
00127
for (
int j = 0; j < i; j++) {
00128 bout.
write(j + 16);
00129 }
00130 bout.
finish();
00131 out.close();
00132 InputStream is = sr.
getInputStream();
00133
Base64InputStream bin =
new Base64InputStream(is);
00134
for (
int j = 0; j <= i; j++) {
00135 writer.println(
"" + i +
": " + j +
": " + bin.
read());
00136 }
00137 bin.
close();
00138 sr.
close();
00139 }
00140 }
00141
00142 public void testSaveRestore(String[] args)
throws IOException {
00143
for (
int size = 1; size < (1 << 16); size <<= 1) {
00144
SaveRestoreStream so =
new SaveRestoreStream(1000);
00145 OutputStream os = so.
getOutputStream();
00146
for (
int i = 0; i < size; i += 4) {
00147 os.write((i >> 24) & 0xff);
00148 os.write((i >> 16) & 0xff);
00149 os.write((i >> 8) & 0xff);
00150 os.write(i & 0xff);
00151 }
00152 InputStream is = so.
getInputStream();
00153
for (
int i = 0; i < size; i += 4) {
00154
int t = is.read();
00155 t <<= 8; t |= (is.read() & 0xff);
00156 t <<= 8; t |= (is.read() & 0xff);
00157 t <<= 8; t |= (is.read() & 0xff);
00158 testAssert(t == i);
00159 }
00160 }
00161 }
00162
00163 public void testRecursiveFileIterator(String[] args)
00164
throws Exception
00165 {
00166 FileFilter filter =
new FileFilter() {
00167
public boolean accept(File f) {
00168
if (f.isDirectory()) {
00169
return !f.getName().equals(
"CVS");
00170 }
else {
00171
return (f.getName().endsWith(
".java"));
00172 }
00173 }
00174 };
00175
RecursiveFileIterator f =
00176
new RecursiveFileIterator(
new File(
"."), filter);
00177
while (f.
hasNext()) {
00178 File fil = (File)f.
next();
00179 writer.println(fil.getName());
00180 }
00181 }
00182
00183 public static void main(String args[]) {
00184
Test t =
new Test();
00185 t.
test(args);
00186 }
00187
00188 }
00189