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: package org.cougaar.glm.execution.common;
027:
028: import java.io.ByteArrayInputStream;
029: import java.io.IOException;
030: import java.io.ObjectInputStream;
031:
032: import org.cougaar.core.util.UID;
033:
034: /**
035: * An implementation of the LineReader interface as an abstract base
036: * class. the readLine method is abstract.
037: **/
038: public abstract class LineReaderBase implements LineReader {
039: public abstract String readLine() throws IOException;
040:
041: public EGObject readEGObject() throws IOException {
042: try {
043: int classIndex = readInt();
044: EGObject o = (EGObject) EGObject.egObjectClasses[classIndex]
045: .newInstance();
046: o.read(this );
047: return o;
048: } catch (IOException ioe) {
049: throw ioe;
050: } catch (Exception e) {
051: e.printStackTrace();
052: throw new IOException(e.toString());
053: }
054: }
055:
056: public String readUTF() throws IOException {
057: return readLine();
058: }
059:
060: public UID readUID() throws IOException {
061: String uidOwner = readUTF();
062: long uidId = readLong();
063: return new UID(uidOwner, uidId);
064: }
065:
066: public double readDouble() throws IOException {
067: try {
068: return Double.parseDouble(readLine());
069: } catch (NumberFormatException nfe) {
070: throw new RuntimeException(nfe.toString());
071: }
072: }
073:
074: public float readFloat() throws IOException {
075: try {
076: return Float.parseFloat(readLine());
077: } catch (NumberFormatException nfe) {
078: throw new RuntimeException(nfe.toString());
079: }
080: }
081:
082: public byte readByte() throws IOException {
083: try {
084: return Byte.parseByte(readLine());
085: } catch (NumberFormatException nfe) {
086: throw new RuntimeException(nfe.toString());
087: }
088: }
089:
090: public short readShort() throws IOException {
091: try {
092: return Short.parseShort(readLine());
093: } catch (NumberFormatException nfe) {
094: throw new RuntimeException(nfe.toString());
095: }
096: }
097:
098: public int readInt() throws IOException {
099: try {
100: return Integer.parseInt(readLine());
101: } catch (NumberFormatException nfe) {
102: throw new RuntimeException(nfe.toString());
103: }
104: }
105:
106: public long readLong() throws IOException {
107: try {
108: return Long.parseLong(readLine());
109: } catch (NumberFormatException nfe) {
110: throw new RuntimeException(nfe.toString());
111: }
112: }
113:
114: public boolean readBoolean() throws IOException {
115: return readLine().equals("true");
116: }
117:
118: public Object readObject() throws IOException {
119: try {
120: int nb = readInt();
121: byte[] bytes = new byte[nb];
122: int pb = 0;
123: char[] line = new char[80];
124: while (pb < nb) {
125: String s = readLine();
126: int nc = s.length();
127: s.getChars(0, nc, line, 0);
128: int pc = 0;
129: while (pc < nc) {
130: int threeBytes = ((base64Decoding[line[pc]] << 18)
131: | (base64Decoding[line[pc + 1]] << 12)
132: | (base64Decoding[line[pc + 2]] << 6) | (base64Decoding[line[pc + 3]]));
133: pc += 4;
134: bytes[pb++] = (byte) (threeBytes >> 16);
135: bytes[pb++] = (byte) (threeBytes >> 8);
136: bytes[pb++] = (byte) (threeBytes);
137: }
138: }
139: // LineWriterBase.writeByteArray(bytes);
140: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
141: ObjectInputStream is = new ObjectInputStream(bais);
142: return is.readObject();
143: } catch (IOException ioe) {
144: throw ioe;
145: } catch (Exception e) {
146: throw new IOException(e.getMessage());
147: }
148: }
149:
150: public static byte[] base64Decoding = new byte[128];
151:
152: static {
153: for (byte i = 0; i < 64; i++) {
154: char c = LineWriterBase.base64Encoding[i];
155: base64Decoding[c] = i;
156: }
157: }
158: }
|