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: import java.rmi.server.UnicastRemoteObject;
033:
034: /**
035: */
036: class InputStreamImpl extends UnicastRemoteObject implements
037: InputStreamDecl {
038:
039: private InputStream in;
040: private byte[] tmpBuf;
041:
042: public InputStreamImpl(InputStream in) throws RemoteException {
043: // buffer? for now assume that caller creates a buffer...
044: this .in = in;
045: }
046:
047: /**
048: * Special "read(byte[])" equivalent for RMI.
049: * <p>
050: * This replaces:<ul>
051: * <li>public int read(byte[] b) throws IOException</li>
052: * <li>public int read(byte[] b, int off, int len) throws IOException</li>
053: * </ul>
054: * The typical "read(byte[] ..)" methods will not work for RMI, since
055: * the result must be serialized back as a separate Object.
056: * <p>
057: * This method uses the <tt>tmpBuf</tt> as the temporary <tt>byte[]</tt>
058: * for reading from the stream. Typically the client reads in constant
059: * amounts, such as "read(1024)", so this tmpBuf will match this size.
060: *
061: * @param len number of bytes to read
062: *
063: * @return a byte[] of data, or null if the end of the stream has been
064: * reached
065: */
066: public byte[] read(int len) throws IOException {
067: if (len < 0) {
068: throw new IndexOutOfBoundsException();
069: }
070: byte[] b = this .tmpBuf;
071: if ((b == null) || (b.length < len)) {
072: b = new byte[len];
073: this .tmpBuf = b;
074: }
075: int n = in.read(b, 0, len);
076: byte[] ret;
077: if (n == b.length) {
078: ret = b;
079: } else if (n >= 0) {
080: ret = new byte[n];
081: System.arraycopy(b, 0, ret, 0, n);
082: } else {
083: ret = null;
084: }
085: return ret;
086: }
087:
088: //
089: // all the other methods of InputStream:
090: //
091:
092: public int read() throws IOException {
093: return in.read();
094: }
095:
096: public long skip(long n) throws IOException {
097: return in.skip(n);
098: }
099:
100: public int available() throws IOException {
101: return in.available();
102: }
103:
104: public void close() throws IOException {
105: in.close();
106: }
107:
108: public void mark(int readlimit) {
109: in.mark(readlimit);
110: }
111:
112: public void reset() throws IOException {
113: in.reset();
114: }
115:
116: public boolean markSupported() {
117: return in.markSupported();
118: }
119:
120: }
|