1 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 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 56 public TimestampFilter() 57 { 58 } 59 60 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 84 public boolean canWrite() 85 { 86 return _stream != null && _stream.canWrite(); 87 } 88 89 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 128 public void flush() 129 throws IOException 130 { 131 if (_stream != null) 132 _stream.flush(); 133 } 134 135 138 public void close() 139 throws IOException 140 { 141 if (_stream != null) 142 _stream.close(); 143 } 144 } 145 | Popular Tags |