if (listener == null) {
return originalLoggableWriter;
}
final OutputStream originalOutputStream = originalLoggableWriter.getOutputStream();
final OutputStream wrappedOutputStream = new OutputStream() {
private final ByteToCharConverter byteToCharConverter = new ByteToCharConverter(charset);
@Override
public void close() throws IOException {
originalOutputStream.close();
}
@Override
public void flush() throws IOException {
originalOutputStream.flush();
}
@Override
public void write(int b) throws IOException {
originalOutputStream.write(b);
writeToListener(ByteBuffer.wrap(new byte[] { (byte) b }));
}
@Override
public void write(byte[] b) throws IOException {
writeImpl(b, 0, b.length);
}
@Override
public void write(byte[] buf, int off, int len) throws IOException {
writeImpl(buf, off, len);
}
private void writeImpl(byte[] buf, int off, int len) throws IOException {
originalOutputStream.write(buf, off, len);
writeToListener(ByteBuffer.wrap(buf, off, len));
}
private void writeToListener(ByteBuffer byteBuffer) {
CharBuffer charBuffer = byteToCharConverter.convert(byteBuffer);
listener.addContent(charBuffer);
}
};
return new LoggableOutputStream() {
@Override public OutputStream getOutputStream() {