1 /*************************************************************************
2 /* PipedStreamTest.java -- Tests Piped{Input,Output}Stream's
4 /* Copyright (c) 1998 Free Software Foundation, Inc.
5 /* Written by Aaron M. Renn (arenn@urbanophile.com)
7 /* This program is free software; you can redistribute it and/or modify
8 /* it under the terms of the GNU General Public License as published
9 /* by the Free Software Foundation, either version 2 of the License, or
10 /* (at your option) any later version.
12 /* This program is distributed in the hope that it will be useful, but
13 /* WITHOUT ANY WARRANTY; without even the implied warranty of
14 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 /* GNU General Public License for more details.
17 /* You should have received a copy of the GNU General Public License
18 /* along with this program; if not, write to the Free Software Foundation
19 /* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 /*************************************************************************/
24 public class PipedStreamTest
28 main(String
[] argv
) throws InterruptedException
30 // Set up a reasonable buffer size for this test if one is not already
32 String prop
= System
.getProperty("gnu.java.io.pipe_size");
34 // System.setProperty("gnu.java.io.pipe_size", "32");
38 System
.out
.println("Started test of PipedInputStream and " +
41 System
.out
.println("Test 1: Basic piped stream test");
43 // Set up the thread to write
44 PipedStreamTestWriter pstw
= new PipedStreamTestWriter();
45 String str
= pstw
.getStr();
46 PipedOutputStream pos
= pstw
.getStream();
48 // Now set up our reader
49 PipedInputStream pis
= new PipedInputStream();
51 new Thread(pstw
).start();
53 byte[] buf
= new byte[12];
54 int bytes_read
, total_read
= 0;
55 while((bytes_read
= pis
.read(buf
)) != -1)
57 System
.out
.print(new String(buf
, 0, bytes_read
));
59 Thread
.sleep(10); // A short delay
60 total_read
+= bytes_read
;
63 if (total_read
== str
.length())
64 System
.out
.println("PASSED: Basic piped stream test");
66 System
.out
.println("FAILED: Basic piped stream test");
70 System
.out
.println("FAILED: Basic piped stream test: " + e
);
74 } // class PipedStreamTest
76 class PipedStreamTestWriter
implements Runnable
80 StringBufferInputStream sbis
;
81 PipedOutputStream out
;
84 PipedStreamTestWriter()
86 str
= "I went to work for Andersen Consulting after I graduated\n" +
87 "from college. They sent me to their training facility in St. Charles,\n" +
88 "Illinois and tried to teach me COBOL. I didn't want to learn it.\n" +
89 "The instructors said I had a bad attitude and I got a green sheet\n" +
90 "which is a nasty note in your file saying what a jerk you are.\n";
92 sbis
= new StringBufferInputStream(str
);
94 out
= new PipedOutputStream();
97 public PipedOutputStream
112 byte[] buf
= new byte[32];
121 while ((bytes_read
= sbis
.read(buf
)) != -1)
122 out
.write(buf
, 0, bytes_read
);
128 System
.out
.println("FAILED: Basic piped stream test: " + e
);