001: // @(#)SessionCipherMsg.java 1.4 "@(#)SessionCipherMsg.java 1.4 99/09/23 Sun Microsystems"
002:
003: package com.sun.portal.netlet.econnection;
004:
005: import java.io.ByteArrayInputStream;
006: import java.io.ByteArrayOutputStream;
007: import java.io.DataInputStream;
008: import java.io.DataOutputStream;
009: import java.io.IOException;
010:
011: public class SessionCipherMsg extends CipherMsg {
012:
013: protected byte[] sessId = null;
014:
015: public SessionCipherMsg() {
016: super (VERSION, SESSION_MSG);
017: }
018:
019: public SessionCipherMsg(byte[] sid, int sidlen) {
020: super (VERSION, SESSION_MSG);
021: msgLen = sidlen;
022: sessId = new byte[sidlen];
023: System.arraycopy(sid, 0, sessId, 0, sidlen);
024: }
025:
026: public int readMsg(DataInputStream in) {
027: int rc = 0;
028:
029: rc = readHeader(in);
030: if (rc == 0) {
031: if ((msgLen > 0)) {
032: if (sessId != null) {
033: sessId = null;
034: }
035: byte[] tmpId = new byte[msgLen];
036: try {
037: in.readFully(tmpId, 0, msgLen);
038: sessId = new byte[msgLen];
039: System.arraycopy(tmpId, 0, sessId, 0, msgLen);
040: } catch (IOException e) {
041: System.out.println("SessionMsg: readMsg IOE: " + e);
042: rc = -1;
043: }
044: tmpId = null;
045: } else {
046: rc = -1;
047: }
048: }
049: return (rc);
050: }
051:
052: public int writeMsg(DataOutputStream out) {
053: int rc = 0;
054: int oldmsgLen;
055:
056: try {
057: // byte[] tmpId = new byte[sessId.length + 8]; // pad
058: byte[] tmpId = new byte[sessId.length]; // Why Pad ?
059: System.arraycopy(sessId, 0, tmpId, 0, msgLen);
060: ByteArrayOutputStream b_out = new ByteArrayOutputStream(
061: HEADER_LEN + sessId.length);
062: DataOutputStream db_out = new DataOutputStream(b_out);
063: oldmsgLen = msgLen;
064: writeHeaderToByteArray(b_out);
065: b_out.write(tmpId, 0, msgLen);
066: b_out.writeTo(out);
067: out.flush();
068: msgLen = oldmsgLen;
069: tmpId = null;
070: } catch (IOException e) {
071: System.out.println("SessionMsg: writeMsg IOE: " + e);
072: rc = -1;
073: }
074: return (rc);
075: }
076:
077: public byte[] getToDataStream() {
078: ByteArrayOutputStream b_out = new ByteArrayOutputStream();
079: b_out.write(sessId, 0, msgLen);
080: return (b_out.toByteArray());
081: }
082:
083: public int setFromDataStream(byte[] buffer, int buffer_len) {
084: int rc = 0;
085: ByteArrayInputStream b_in;
086:
087: if ((buffer_len > 0)) {
088: // now, parse rest of config message...
089: b_in = new ByteArrayInputStream(buffer);
090:
091: msgLen = buffer_len;
092: b_in.read(sessId, 0, msgLen);
093: } else {
094: rc = -1;
095: }
096: return (rc);
097: }
098:
099: public byte[] getSessionId() {
100: if ((msgLen > 0)) {
101: byte[] id = new byte[msgLen];
102: System.arraycopy(sessId, 0, id, 0, msgLen);
103: return (id);
104: } else {
105: return (null);
106: }
107: }
108:
109: public void setSessionId(byte[] id, int id_len) {
110: msgLen = 0;
111: if (sessId != null) {
112: sessId = null;
113: }
114: sessId = new byte[id_len];
115: System.arraycopy(id, 0, sessId, 0, id_len);
116: msgLen = id_len;
117: }
118:
119: }
|