Browse Source

add trace logging

tags/2.0.1
Jonathan Cobb 5 years ago
parent
commit
f9d652ba00
1 changed files with 19 additions and 0 deletions
  1. +19
    -0
      src/main/java/org/cobbzilla/util/io/multi/MultiStream.java

+ 19
- 0
src/main/java/org/cobbzilla/util/io/multi/MultiStream.java View File

@@ -24,31 +24,44 @@ public class MultiStream extends InputStream {
currentStream = r; currentStream = r;
} }


public int pendingStreamCount () { return streams.size() - streamIndex; }

public MultiStream (InputStream r) { this(r, false); } public MultiStream (InputStream r) { this(r, false); }


@Override public String toString () {
return "MultiStream{"+streams.size()+" streams, index="+streamIndex+", EOS="+endOfStreams+"}";
}

public void addStream (InputStream in) { public void addStream (InputStream in) {
if (endOfStreams) { if (endOfStreams) {
log.warn("addStream: endOfStreams is true, not adding InputStream"); log.warn("addStream: endOfStreams is true, not adding InputStream");
} else { } else {
streams.add(in); streams.add(in);
if (log.isTraceEnabled()) log.trace("addStream: added stream ("+in.getClass().getSimpleName()+"). this="+this);
} }
} }


public void addLastStream (InputStream in) { public void addLastStream (InputStream in) {
addStream(in); addStream(in);
endOfStreams = true; endOfStreams = true;
if (log.isTraceEnabled()) log.trace("addLastStream: added last stream ("+in.getClass().getSimpleName()+"). this="+this);
} }


@Override public int read() throws IOException { @Override public int read() throws IOException {
final int val = currentStream.read(); final int val = currentStream.read();
if (val == -1) { if (val == -1) {
if (streamIndex == streams.size()-1) { if (streamIndex == streams.size()-1) {
if (log.isTraceEnabled()) log.trace("read(byte): end of all streams? this="+this);
return endOfStreams ? -1 : 0; return endOfStreams ? -1 : 0;
} }
currentStream.close(); currentStream.close();
streamIndex++; streamIndex++;
currentStream = streams.get(streamIndex); currentStream = streams.get(streamIndex);
if (log.isTraceEnabled()) log.trace("read(byte): end of all stream, advanced to next stream ("+currentStream.getClass().getSimpleName()+"). this="+this);
return read(); return read();

} else {
if (log.isTraceEnabled()) log.trace("read(byte): one byte read. this="+this);
} }
return val; return val;
} }
@@ -57,17 +70,23 @@ public class MultiStream extends InputStream {
final int count = currentStream.read(buf, off, len); final int count = currentStream.read(buf, off, len);
if (count == -1) { if (count == -1) {
if (streamIndex == streams.size()-1) { if (streamIndex == streams.size()-1) {
if (log.isTraceEnabled()) log.trace("read(byte[]): end of all streams? this="+this);
return endOfStreams ? -1 : 0; return endOfStreams ? -1 : 0;
} }
currentStream.close(); currentStream.close();
streamIndex++; streamIndex++;
currentStream = streams.get(streamIndex); currentStream = streams.get(streamIndex);
if (log.isTraceEnabled()) log.trace("read(byte[]): end of all stream, advanced to next stream ("+currentStream.getClass().getSimpleName()+"). this="+this);
return read(buf, off, len); return read(buf, off, len);

} else {
if (log.isTraceEnabled()) log.trace("read(byte[]): "+count+" bytes read. this="+this);
} }
return count; return count;
} }


@Override public void close() throws IOException { @Override public void close() throws IOException {
if (log.isTraceEnabled()) log.trace("close: closing current stream ("+(currentStream == null ? "null" : currentStream.getClass().getSimpleName())+"). this="+this);
if (currentStream != null) currentStream.close(); if (currentStream != null) currentStream.close();
} }




Loading…
Cancel
Save