PR target/27599
[official-gcc.git] / libjava / classpath / test / java.io / PipedStreamTest.java
blobd30bfa3e69a657ee8ec9cc105810e9bbaba40fac
1 /*************************************************************************
2 /* PipedStreamTest.java -- Tests Piped{Input,Output}Stream's
3 /*
4 /* Copyright (c) 1998 Free Software Foundation, Inc.
5 /* Written by Aaron M. Renn (arenn@urbanophile.com)
6 /*
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 /*************************************************************************/
22 import java.io.*;
24 public class PipedStreamTest
27 public static void
28 main(String[] argv) throws InterruptedException
30 // Set up a reasonable buffer size for this test if one is not already
31 // specified
32 String prop = System.getProperty("gnu.java.io.pipe_size");
33 // if (prop == null)
34 // System.setProperty("gnu.java.io.pipe_size", "32");
36 try
38 System.out.println("Started test of PipedInputStream and " +
39 "PipedOutputStream");
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();
50 pis.connect(pos);
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));
58 System.out.flush();
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");
65 else
66 System.out.println("FAILED: Basic piped stream test");
68 catch (IOException e)
70 System.out.println("FAILED: Basic piped stream test: " + e);
74 } // class PipedStreamTest
76 class PipedStreamTestWriter implements Runnable
79 String str;
80 StringBufferInputStream sbis;
81 PipedOutputStream out;
83 public
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
98 getStream()
100 return(out);
103 public String
104 getStr()
106 return(str);
109 public void
110 run()
112 byte[] buf = new byte[32];
114 int bytes_read;
118 int b = sbis.read();
119 out.write(b);
121 while ((bytes_read = sbis.read(buf)) != -1)
122 out.write(buf, 0, bytes_read);
124 out.close();
126 catch(IOException e)
128 System.out.println("FAILED: Basic piped stream test: " + e);