KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > vfs > TimestampFilter


1 /*
2  * Copyright (c) 1998-2004 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.vfs;
30
31 import java.io.*;
32 import java.net.*;
33 import java.util.*;
34
35 import com.caucho.util.*;
36 import com.caucho.vfs.*;
37
38 /**
39  * Automatically-rotating streams. Normally, clients will call
40  * getStream instead of using the StreamImpl interface.
41  */

42 public class TimestampFilter extends StreamImpl {
43   private WriteStream _stream;
44   
45   private String _timestamp;
46
47   private QDate _calendar = new QDate(true);
48
49   private boolean _isLineBegin = true;
50
51   /**
52    * Create listener.
53    *
54    * @param path underlying log path
55    */

56   public TimestampFilter()
57   {
58   }
59
60   /**
61    * Create listener.
62    *
63    * @param path underlying log path
64    */

65   public TimestampFilter(WriteStream out, String timestamp)
66   {
67     _stream = out;
68     _timestamp = timestamp;
69   }
70
71   public void setTimestamp(String timestamp)
72   {
73     _timestamp = timestamp;
74   }
75
76   public void setStream(WriteStream stream)
77   {
78     _stream = stream;
79   }
80
81   /**
82    * Returns true if the stream can write.
83    */

84   public boolean canWrite()
85   {
86     return _stream != null && _stream.canWrite();
87   }
88
89   /**
90    * Write data to the stream.
91    */

92   public void write(byte []buffer, int offset, int length, boolean isEnd)
93     throws IOException
94   {
95     if (_stream == null)
96       return;
97
98     if (_timestamp == null) {
99       _stream.write(buffer, offset, length);
100       return;
101     }
102     
103     long now;
104
105     if (CauchoSystem.isTesting())
106       now = Alarm.getCurrentTime();
107     else
108       now = System.currentTimeMillis();
109     
110     for (int i = 0; i < length; i++) {
111       if (_isLineBegin) {
112         _stream.print(_calendar.formatLocal(now, _timestamp));
113         _isLineBegin = false;
114       }
115
116       int ch = buffer[offset + i];
117       _stream.write(ch);
118       
119       if (ch == '\n' ||
120           ch == '\r' && i + 1 < length && buffer[offset + i + 1] != '\n')
121         _isLineBegin = true;
122     }
123   }
124
125   /**
126    * Flushes the data.
127    */

128   public void flush()
129     throws IOException
130   {
131     if (_stream != null)
132       _stream.flush();
133   }
134
135   /**
136    * Flushes the data.
137    */

138   public void close()
139     throws IOException
140   {
141     if (_stream != null)
142       _stream.close();
143   }
144 }
145
Popular Tags