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.ByteArrayOutputStream;
029: import java.io.IOException;
030: import java.io.ObjectOutputStream;
031: import java.io.Serializable;
032:
033: import org.cougaar.core.util.UID;
034:
035: public abstract class LineWriterBase implements LineWriter {
036: public abstract void writeLine(String s) throws IOException;
037:
038: public abstract void flush() throws IOException;
039:
040: public abstract void close() throws IOException;
041:
042: public void writeUTF(String s) throws IOException {
043: writeLine(s);
044: }
045:
046: public void writeBoolean(boolean b) throws IOException {
047: writeLine(b ? "true" : "false");
048: }
049:
050: public void writeByte(byte b) throws IOException {
051: writeLine(Byte.toString(b));
052: }
053:
054: public void writeChar(char c) throws IOException {
055: writeLine("" + c);
056: }
057:
058: public void writeShort(short c) throws IOException {
059: writeLine(Short.toString(c));
060: }
061:
062: public void writeInt(int i) throws IOException {
063: writeLine(Integer.toString(i));
064: }
065:
066: public void writeLong(long l) throws IOException {
067: writeLine(Long.toString(l));
068: }
069:
070: public void writeDouble(double d) throws IOException {
071: writeLine(Double.toString(d));
072: }
073:
074: public void writeFloat(float f) throws IOException {
075: writeLine(Float.toString(f));
076: }
077:
078: public void writeEGObject(EGObject o) throws IOException {
079: writeInt(o.getClassIndex());
080: o.write(this );
081: }
082:
083: public void writeUID(UID uid) throws IOException {
084: writeUTF(uid.getOwner());
085: writeLong(uid.getId());
086: }
087:
088: /**
089: * Base64 encode the serialized object. This may not be the official
090: * encoding by that name, but it suffices.
091: **/
092: public void writeObject(Serializable o) throws IOException {
093: ByteArrayOutputStream baos = new ByteArrayOutputStream();
094: ObjectOutputStream os = new ObjectOutputStream(baos);
095: os.writeObject(o);
096: int nb = baos.size();
097: while (nb % 3 != 0) {
098: baos.write(0); // pad with zeros to a multiple of three
099: nb++;
100: }
101: writeInt(nb);
102: byte[] bytes = baos.toByteArray(); // The bytes to encode
103: // writeByteArray(bytes);
104: char[] line = new char[80]; // Encode into this array
105: int pb = 0;
106: while (pb < nb) {
107: int eb = Math.min(nb, pb + 60);
108: int nc = 0;
109: while (pb < eb) {
110: int threeBytes = 0;
111: threeBytes = (((bytes[pb] & 0xff) << 16)
112: | ((bytes[pb + 1] & 0xff) << 8) | ((bytes[pb + 2] & 0xff)));
113: pb += 3;
114: line[nc++] = base64Encoding[(threeBytes >> 18) & 0x3f];
115: line[nc++] = base64Encoding[(threeBytes >> 12) & 0x3f];
116: line[nc++] = base64Encoding[(threeBytes >> 6) & 0x3f];
117: line[nc++] = base64Encoding[(threeBytes) & 0x3f];
118: }
119: writeLine(new String(line, 0, nc));
120: }
121: }
122:
123: public static void writeByteArray(byte[] bytes) {
124: for (int i = 0; i < bytes.length; i++) {
125: if (i > 0) {
126: if (i % 20 == 0) {
127: System.out.println();
128: } else {
129: System.out.print(" ");
130: }
131: }
132: String hex = Integer.toHexString(bytes[i] & 0xff);
133: if (hex.length() == 1)
134: hex = "0" + hex;
135: System.out.print(hex);
136: }
137: }
138:
139: public static final char[] base64Encoding = { 'A', 'B', 'C', 'D',
140: 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
141: 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b',
142: 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
143: 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
144: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
145: }
|