5270 |
15 Mar 10 |
nicklas |
1 |
/* |
5270 |
15 Mar 10 |
nicklas |
$Id $ |
5270 |
15 Mar 10 |
nicklas |
3 |
|
5270 |
15 Mar 10 |
nicklas |
Copyright (C) 2010 Nicklas Nordborg |
5270 |
15 Mar 10 |
nicklas |
5 |
|
5270 |
15 Mar 10 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
5270 |
15 Mar 10 |
nicklas |
Available at http://base.thep.lu.se/ |
5270 |
15 Mar 10 |
nicklas |
8 |
|
5270 |
15 Mar 10 |
nicklas |
BASE is free software; you can redistribute it and/or |
5270 |
15 Mar 10 |
nicklas |
modify it under the terms of the GNU General Public License |
5270 |
15 Mar 10 |
nicklas |
as published by the Free Software Foundation; either version 3 |
5270 |
15 Mar 10 |
nicklas |
of the License, or (at your option) any later version. |
5270 |
15 Mar 10 |
nicklas |
13 |
|
5270 |
15 Mar 10 |
nicklas |
BASE is distributed in the hope that it will be useful, |
5270 |
15 Mar 10 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
5270 |
15 Mar 10 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5270 |
15 Mar 10 |
nicklas |
GNU General Public License for more details. |
5270 |
15 Mar 10 |
nicklas |
18 |
|
5270 |
15 Mar 10 |
nicklas |
You should have received a copy of the GNU General Public License |
5270 |
15 Mar 10 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
5270 |
15 Mar 10 |
nicklas |
21 |
*/ |
5375 |
03 Aug 10 |
nicklas |
22 |
import java.io.File; |
5375 |
03 Aug 10 |
nicklas |
23 |
import java.io.IOException; |
5270 |
15 Mar 10 |
nicklas |
24 |
import java.io.InputStream; |
5270 |
15 Mar 10 |
nicklas |
25 |
import java.io.OutputStream; |
5375 |
03 Aug 10 |
nicklas |
26 |
import java.io.PrintWriter; |
5270 |
15 Mar 10 |
nicklas |
27 |
|
5270 |
15 Mar 10 |
nicklas |
28 |
/** |
5270 |
15 Mar 10 |
nicklas |
A simple program that copies what is sent on standard input to standard output. |
5270 |
15 Mar 10 |
nicklas |
In case of an error the stacktrace is printed to the standard error stream. |
5270 |
15 Mar 10 |
nicklas |
It accepts one parameter on the command line: |
5270 |
15 Mar 10 |
nicklas |
error: Simulate an error (exit code = 1) |
5270 |
15 Mar 10 |
nicklas |
slow: Simulate a long-running program by waiting |
5270 |
15 Mar 10 |
nicklas |
* 10 seconds before copying, |
5270 |
15 Mar 10 |
nicklas |
* 1 second for each kb of input |
5270 |
15 Mar 10 |
nicklas |
* 10 seconds at the end |
5375 |
03 Aug 10 |
nicklas |
extra: Create some extra random files |
5270 |
15 Mar 10 |
nicklas |
@author nicklas |
5270 |
15 Mar 10 |
nicklas |
@since 2.15 |
5270 |
15 Mar 10 |
nicklas |
40 |
*/ |
5270 |
15 Mar 10 |
nicklas |
41 |
public class CopyStdin2Stdout |
5270 |
15 Mar 10 |
nicklas |
42 |
{ |
5270 |
15 Mar 10 |
nicklas |
43 |
|
5270 |
15 Mar 10 |
nicklas |
44 |
public static void main(String[] args) |
5270 |
15 Mar 10 |
nicklas |
45 |
{ |
5270 |
15 Mar 10 |
nicklas |
46 |
String cmd = args.length > 0 ? args[0] : null; |
5270 |
15 Mar 10 |
nicklas |
47 |
boolean simulateError = "error".equals(cmd); |
5270 |
15 Mar 10 |
nicklas |
48 |
boolean isSlow = "slow".equals(cmd); |
5375 |
03 Aug 10 |
nicklas |
49 |
boolean extra = "extra".equals(cmd); |
5270 |
15 Mar 10 |
nicklas |
50 |
byte[] buffer = new byte[1024]; |
5270 |
15 Mar 10 |
nicklas |
51 |
InputStream in = System.in; |
5270 |
15 Mar 10 |
nicklas |
52 |
OutputStream out = System.out; |
5270 |
15 Mar 10 |
nicklas |
53 |
int bytes = 0; |
5270 |
15 Mar 10 |
nicklas |
54 |
try |
5270 |
15 Mar 10 |
nicklas |
55 |
{ |
5270 |
15 Mar 10 |
nicklas |
56 |
if (isSlow) Thread.sleep(10000); |
5270 |
15 Mar 10 |
nicklas |
57 |
if (simulateError) throw new RuntimeException("Simulating an error"); |
5270 |
15 Mar 10 |
nicklas |
58 |
while (bytes != -1) // -1 = end of stream |
5270 |
15 Mar 10 |
nicklas |
59 |
{ |
5270 |
15 Mar 10 |
nicklas |
60 |
bytes = in.read(buffer, 0, buffer.length); |
5270 |
15 Mar 10 |
nicklas |
61 |
if (bytes > 0) |
5270 |
15 Mar 10 |
nicklas |
62 |
{ |
5270 |
15 Mar 10 |
nicklas |
63 |
out.write(buffer, 0, bytes); |
5270 |
15 Mar 10 |
nicklas |
64 |
} |
5270 |
15 Mar 10 |
nicklas |
65 |
if (isSlow) Thread.sleep(1000); |
5270 |
15 Mar 10 |
nicklas |
66 |
} |
5270 |
15 Mar 10 |
nicklas |
67 |
if (isSlow) Thread.sleep(10000); |
5375 |
03 Aug 10 |
nicklas |
68 |
|
5375 |
03 Aug 10 |
nicklas |
69 |
if (extra) |
5375 |
03 Aug 10 |
nicklas |
70 |
{ |
5375 |
03 Aug 10 |
nicklas |
71 |
createFile("file1.txt"); |
5375 |
03 Aug 10 |
nicklas |
72 |
createFile("file2.txt"); |
5375 |
03 Aug 10 |
nicklas |
73 |
createFile("subdir/file3.txt"); |
5375 |
03 Aug 10 |
nicklas |
74 |
if (isSlow) Thread.sleep(10000); |
5375 |
03 Aug 10 |
nicklas |
75 |
} |
5375 |
03 Aug 10 |
nicklas |
76 |
|
5270 |
15 Mar 10 |
nicklas |
77 |
} |
5270 |
15 Mar 10 |
nicklas |
78 |
catch (Throwable t) |
5270 |
15 Mar 10 |
nicklas |
79 |
{ |
5270 |
15 Mar 10 |
nicklas |
80 |
t.printStackTrace(); |
5270 |
15 Mar 10 |
nicklas |
81 |
System.exit(1); |
5270 |
15 Mar 10 |
nicklas |
82 |
} |
5270 |
15 Mar 10 |
nicklas |
83 |
} |
5375 |
03 Aug 10 |
nicklas |
84 |
|
5375 |
03 Aug 10 |
nicklas |
85 |
private static void createFile(String name) |
5375 |
03 Aug 10 |
nicklas |
86 |
throws IOException |
5375 |
03 Aug 10 |
nicklas |
87 |
{ |
5375 |
03 Aug 10 |
nicklas |
88 |
File f = new File(name); |
5375 |
03 Aug 10 |
nicklas |
89 |
if (f.getParentFile() != null) f.getParentFile().mkdirs(); |
5375 |
03 Aug 10 |
nicklas |
90 |
f.createNewFile(); |
5375 |
03 Aug 10 |
nicklas |
91 |
PrintWriter pw = new PrintWriter(f); |
5375 |
03 Aug 10 |
nicklas |
92 |
for (int i = 0; i < 100; ++i) |
5375 |
03 Aug 10 |
nicklas |
93 |
{ |
5375 |
03 Aug 10 |
nicklas |
94 |
pw.println(Math.random()); |
5375 |
03 Aug 10 |
nicklas |
95 |
} |
5375 |
03 Aug 10 |
nicklas |
96 |
pw.close(); |
5375 |
03 Aug 10 |
nicklas |
97 |
} |
5270 |
15 Mar 10 |
nicklas |
98 |
|
5270 |
15 Mar 10 |
nicklas |
99 |
} |