1 package nmp.io;
2
3 import java.io.*;
4 import java.text.*;
5 import java.util.*;
6
7 public final class DuoOutputStream
8 extends OutputStream
9 {
10 protected static final SimpleDateFormat dateFormat;
11
12 static
13 {
14 dateFormat = new SimpleDateFormat("dd-MMM-yyyy---HH_mm_ss");
15 TimeZone tz = TimeZone.getTimeZone("GMT");
16 dateFormat.setTimeZone(tz);
17 }
18
19 private OutputStream out1, out2;
20 private String logFilename = "unknown";
21
22 /***
23 * Transform the given string into a valid filename
24 */
25 private String validFilename( String name) {
26 StringBuffer buf = new StringBuffer();
27 for( int i = 0; i < name.length(); i++) {
28 char ch = name.charAt(i);
29 if( Character.isLetterOrDigit( ch))
30 buf.append( ch);
31 else
32 switch( ch) {
33 case '//':
34 buf.append( ch);
35 break;
36 case '-':
37 buf.append( ch);
38 break;
39 default:
40 buf.append('_');
41 break;
42 }
43 }
44 return buf.toString();
45 }
46
47 /***
48 * Base bane us
49 */
50 public DuoOutputStream( String baseName0, OutputStream out1) throws IOException {
51 String baseName = validFilename( baseName0);
52 this.out1 = out1;
53 this.out2 = new FileOutputStream( logFilename = baseName + "-debug-" + dateFormat.format( new java.util.Date()) + ".log");
54 }
55
56 public DuoOutputStream( OutputStream out1) throws IOException {
57 this.out1 = out1;
58 this.out2 = new FileOutputStream( logFilename = "debug-" + dateFormat.format( new java.util.Date()) + ".log");
59 }
60
61 public DuoOutputStream( File basedir, String baseName0, OutputStream out1) throws IOException {
62 String baseName = validFilename( baseName0);
63 this.out1 = out1;
64 this.out2 = new FileOutputStream( new File( basedir, logFilename = baseName + "-debug-" + dateFormat.format( new java.util.Date()) + ".log"));
65 }
66
67 public DuoOutputStream( File basedir, OutputStream out1) throws IOException {
68 this.out1 = out1;
69 this.out2 = new FileOutputStream( new File( basedir, logFilename = "debug-" + dateFormat.format( new java.util.Date()) + ".log"));
70 }
71
72 public DuoOutputStream( OutputStream out1, OutputStream out2) {
73 this.out1 = out1;
74 this.out2 = out2;
75 }
76
77 public void write( int b ) throws IOException {
78 if( out1 != null) {
79 out1.write( b);
80 out1.flush();
81 }
82 if( out2 != null) {
83 out2.write( b);
84 out2.flush();
85 }
86 }
87
88 public void write( byte b[], int off, int len ) throws IOException {
89 if( out1 != null) {
90 out1.write( b, off, len);
91 out1.flush();
92 }
93 if( out2 != null) {
94 out2.write( b, off, len);
95 out2.flush();
96 }
97 }
98
99 public String logFilename() {
100 return logFilename;
101 }
102 }
This page was automatically generated by Maven