001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.so6.core.engine.util;
034:
035: import org.libresource.so6.core.StateMonitoring;
036:
037: import java.io.IOException;
038: import java.io.InputStream;
039:
040: /**
041: * @author smack
042: */
043: public class MonitoredInputStream extends InputStream {
044: private InputStream streamToMonitor;
045: private long max;
046: private long currentState = 1;
047: private long mark = 0;
048: private String finished = "Waiting for reply...";
049: private String reading = "Sending";
050:
051: public MonitoredInputStream(InputStream streamToMonitor,
052: long dataSize) {
053: this .streamToMonitor = streamToMonitor;
054: this .max = dataSize;
055: }
056:
057: public void setComment(String finished, String reading) {
058: this .reading = reading;
059: this .finished = finished;
060: }
061:
062: // inputstream
063: public int read() throws IOException {
064: currentState++;
065:
066: return streamToMonitor.read();
067: }
068:
069: public int available() throws IOException {
070: return streamToMonitor.available();
071: }
072:
073: public void close() throws IOException {
074: streamToMonitor.close();
075: }
076:
077: public synchronized void mark(int readlimit) {
078: mark = readlimit;
079: streamToMonitor.mark(readlimit);
080: }
081:
082: public boolean markSupported() {
083: return streamToMonitor.markSupported();
084: }
085:
086: public int read(byte[] b, int off, int len) throws IOException {
087: currentState += len;
088: setXMLLocalInfo();
089:
090: return streamToMonitor.read(b, off, len);
091: }
092:
093: public int read(byte[] b) throws IOException {
094: int length = streamToMonitor.read(b);
095: currentState += length;
096: setXMLLocalInfo();
097:
098: return length;
099: }
100:
101: public synchronized void reset() throws IOException {
102: currentState = mark;
103: setXMLLocalInfo();
104: streamToMonitor.reset();
105: }
106:
107: public long skip(long n) throws IOException {
108: return streamToMonitor.skip(n);
109: }
110:
111: private void setXMLLocalInfo() {
112: if (currentState >= max) {
113: StateMonitoring.getInstance().setXMLMonitoringState(0, 1,
114: 1, finished);
115: } else {
116: StateMonitoring.getInstance().setXMLMonitoringState(0, max,
117: currentState,
118: reading + " " + currentState + " / " + max);
119: }
120: }
121: }
|