D
Dennis
Dear all,
This has been puzzling me all morning: There is reasonable elegant way (see example below) to log InputStreams and OutputStreams, without consuming the streams. However, I cannot find any reference to it or a good implementation of it anywhere. I tested it and it seems to work, but I have the nagging idea that I'm missing something, or I did miss all the references to it on the internet.
public class ForkedInputStream extends InputStream {
private InputStream m_source;
private OutputStream m_fork;
public ForkedInputStream(InputStream source, OutputStream fork) {
m_source = source;
m_fork = fork;
}
public int read() throws IOException {
int b = m_source.read();
if (b==-1) {
m_fork.flush();
} else {
m_fork.write(b);
}
return b;
}
public int available() throws IOException {
return m_source.available();
}
public void close() throws IOException {
m_fork.close();
}
}
You can feed in your original InputStream to ForkedInputStream and use the instance of ForkedInputStream as you would the original stream. The OutputStream will now contain the same information as read from the InputStream.
Off course you could enhance it with Threading, Buffers and a check to only write at certain (log) levels
This would take care of the only two issues I can think of, but the principle stays the same:
- A blocked output would block the read() operation.
- Writing to the OutputStream takes time, so you might not have it 'on' in every situation.
Or as the title suggests: Am I missing something?
Kind regards,
Dennis
Brains2B.org
This has been puzzling me all morning: There is reasonable elegant way (see example below) to log InputStreams and OutputStreams, without consuming the streams. However, I cannot find any reference to it or a good implementation of it anywhere. I tested it and it seems to work, but I have the nagging idea that I'm missing something, or I did miss all the references to it on the internet.
public class ForkedInputStream extends InputStream {
private InputStream m_source;
private OutputStream m_fork;
public ForkedInputStream(InputStream source, OutputStream fork) {
m_source = source;
m_fork = fork;
}
public int read() throws IOException {
int b = m_source.read();
if (b==-1) {
m_fork.flush();
} else {
m_fork.write(b);
}
return b;
}
public int available() throws IOException {
return m_source.available();
}
public void close() throws IOException {
m_fork.close();
}
}
You can feed in your original InputStream to ForkedInputStream and use the instance of ForkedInputStream as you would the original stream. The OutputStream will now contain the same information as read from the InputStream.
Off course you could enhance it with Threading, Buffers and a check to only write at certain (log) levels
This would take care of the only two issues I can think of, but the principle stays the same:
- A blocked output would block the read() operation.
- Writing to the OutputStream takes time, so you might not have it 'on' in every situation.
Or as the title suggests: Am I missing something?
Kind regards,
Dennis
Brains2B.org