001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.tools.server.rmi;
028:
029: import java.io.IOException;
030: import java.io.InputStream;
031: import java.rmi.RemoteException;
032:
033: /**
034: * @see java.io.InputStream
035: */
036: class InputStreamStub extends InputStream {
037:
038: private InputStreamDecl isd;
039:
040: public InputStreamStub(InputStreamDecl isd) {
041: this .isd = isd;
042: }
043:
044: //
045: // all the methods of InputStream
046: //
047: // RemoteException is an IOException, so for most of this code we can
048: // just delegation and throw the RMI Exception.
049: //
050: // We could instead re-throw the original "RemoteException.detail" if we
051: // carefully cast the exception:
052: //
053: // try {
054: // // some "isd.*" code
055: // } catch (RemoteException re) {
056: // Throwable te = re.detail;
057: // if (te instanceof IOException) {
058: // // throw the inner IOException (e.g. EOFException)
059: // throw (IOException)te;
060: // } else if (te instanceof RuntimeException) {
061: // // throw the inner RuntimeException (?)
062: // throw (RuntimeException)te;
063: // } else {
064: // // throw the raw RMI exception (?)
065: // throw te;
066: // }
067: // }
068: //
069: // For now we'll not introduce the clutter...
070: //
071:
072: public int read(byte[] b) throws IOException {
073: return this .read(b, 0, b.length);
074: }
075:
076: // special-case implementation for "read(byte[] ..)"
077: public int read(byte[] b, int off, int length) throws IOException {
078: byte[] tmpBuf = isd.read(length - off);
079: int n;
080: if (tmpBuf != null) {
081: n = tmpBuf.length;
082: System.arraycopy(tmpBuf, 0, b, off, n);
083: } else {
084: n = -1;
085: }
086: return n;
087: }
088:
089: //
090: // all the other methods of InputStream:
091: //
092:
093: public int read() throws IOException {
094: return isd.read();
095: }
096:
097: public long skip(long n) throws IOException {
098: return isd.skip(n);
099: }
100:
101: public int available() throws IOException {
102: return isd.available();
103: }
104:
105: public void close() throws IOException {
106: isd.close();
107: }
108:
109: public void mark(int readlimit) {
110: try {
111: isd.mark(readlimit);
112: } catch (RemoteException re) {
113: throw (RuntimeException) re.detail;
114: }
115: }
116:
117: public void reset() throws IOException {
118: isd.reset();
119: }
120:
121: public boolean markSupported() {
122: try {
123: return isd.markSupported();
124: } catch (RemoteException re) {
125: throw (RuntimeException) re.detail;
126: }
127: }
128:
129: }
|