|
|
|
@@ -1,91 +0,0 @@ |
|
|
|
package altk.comm.engine; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.io.InputStreamReader; |
|
|
|
import java.nio.CharBuffer; |
|
|
|
import java.nio.charset.CharsetDecoder; |
|
|
|
|
|
|
|
import org.apache.log4j.Logger; |
|
|
|
|
|
|
|
/** |
|
|
|
* Wrapper of InputStreamReader to add logging capability. |
|
|
|
* @author Yuk-Ming |
|
|
|
* |
|
|
|
*/ |
|
|
|
public class LoggingInputStreamReader extends InputStreamReader |
|
|
|
{ |
|
|
|
private static final int CharBuffer_SIZE = 200; |
|
|
|
|
|
|
|
private CharBuffer buf = CharBuffer.allocate(CharBuffer_SIZE); |
|
|
|
|
|
|
|
private static Logger myLogger = Logger.getLogger(LoggingInputStreamReader.class); |
|
|
|
|
|
|
|
public LoggingInputStreamReader(InputStream in, CharsetDecoder decoder) |
|
|
|
{ |
|
|
|
super(in, decoder); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public int read() throws IOException |
|
|
|
{ |
|
|
|
int c = super.read(); |
|
|
|
if (c != -1) |
|
|
|
{ |
|
|
|
append((char)c); |
|
|
|
} |
|
|
|
return c; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public int read(char[] cbuf, int offset, int length) throws IOException |
|
|
|
{ |
|
|
|
int n = super.read(cbuf, offset, length); |
|
|
|
for (int i = offset; i < offset + n; i++) |
|
|
|
{ |
|
|
|
append(cbuf[i]); |
|
|
|
} |
|
|
|
return n; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void append(char c) |
|
|
|
{ |
|
|
|
if ((char)c == '\n') |
|
|
|
{ |
|
|
|
logPartial(); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
buf.put((char)c); |
|
|
|
if (!buf.hasRemaining()) logPartial(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void logPartial() |
|
|
|
{ |
|
|
|
if (buf.position() == 0) return; |
|
|
|
String logContent = new String(buf.array(), 0, buf.position()); |
|
|
|
CommonLogger.activity.info(logContent); |
|
|
|
buf.clear(); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void close() |
|
|
|
{ |
|
|
|
// Read any remaining characters for logging purpose. |
|
|
|
try |
|
|
|
{ |
|
|
|
while (read() != -1); |
|
|
|
super.close(); |
|
|
|
logPartial(); |
|
|
|
} |
|
|
|
catch (IOException e) |
|
|
|
{ |
|
|
|
logPartial(); |
|
|
|
myLogger.warn("Caught exception while reaading unwanted trailing chars in stream"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |