001: // @(#)DataCipherMsg.java 1.3 "@(#)DataCipherMsg.java 1.3 99/09/23 Sun Microsystems"
002:
003: package com.sun.portal.netlet.econnection;
004:
005: import java.io.ByteArrayOutputStream;
006: import java.io.DataInputStream;
007: import java.io.DataOutputStream;
008: import java.io.IOException;
009:
010: public class DataCipherMsg extends CipherMsg implements
011: MessageConstants, SizeConstants {
012:
013: protected static final int MAX_DATA_LEN = 8 * 1024; // somewhat arbitrary
014:
015: protected byte[] data = null;
016:
017: public DataCipherMsg() {
018: super (VERSION, DATA_MSG);
019: }
020:
021: public DataCipherMsg(byte[] buffer, int buffer_len) {
022: super (VERSION, DATA_MSG);
023: if (buffer_len <= MAX_DATA_LEN) {
024: data = new byte[buffer_len];
025: msgLen = buffer_len;
026: System.arraycopy(buffer, 0, data, 0, buffer_len);
027: }
028: msgLen = 0;
029: }
030:
031: public int readMsg(DataInputStream in) {
032: int rc = 0;
033: int outLen;
034: DataInputStream db_in;
035: rc = readHeader(in);
036: if (rc == 0) {
037: if ((msgLen >= 0) && (msgLen <= MAX_DATA_LEN)) {
038: if (data == null) {
039: data = new byte[msgLen];
040: } else if (msgLen > data.length) {
041: data = null;
042: data = new byte[msgLen];
043: }
044: try {
045: in.readFully(data, 0, msgLen);
046: } catch (IOException e) {
047: System.out.println("DataMsg: readMsg IOE:" + e);
048: rc = -1;
049: }
050: }
051: }
052: return (rc);
053: }
054:
055: public int writeMsg(DataOutputStream out) {
056: int rc = 0;
057: int outLen;
058: int oldmsgLen;
059: if ((msgLen >= 0) && (msgLen < MAX_DATA_LEN)) {
060: try {
061: ByteArrayOutputStream b_out = new ByteArrayOutputStream(
062: HEADER_LEN + MAX_DATA_LEN);
063: oldmsgLen = msgLen;
064: writeHeaderToByteArray(b_out);
065: b_out.write(data, 0, msgLen);
066: b_out.writeTo(out);
067: out.flush();
068: msgLen = oldmsgLen;
069: } catch (IOException e) {
070: System.out.println("DataMsg: writeMsg IOException");
071: rc = -1;
072: }
073: } else {
074: rc = -1;
075: }
076: return (rc);
077: }
078:
079: public byte[] getData() {
080: byte[] tempBuffer = null;
081:
082: if ((msgLen >= 0) && (msgLen <= MAX_DATA_LEN)) {
083: tempBuffer = new byte[msgLen];
084: System.arraycopy(data, 0, tempBuffer, 0, msgLen);
085: }
086: return (tempBuffer);
087:
088: }
089:
090: public void setData(byte[] buffer, int buffer_len) {
091: if ((buffer_len > 0) && (buffer_len <= MAX_DATA_LEN)) {
092: msgLen = buffer_len;
093:
094: data = new byte[buffer_len];
095: System.arraycopy(buffer, 0, data, 0, buffer_len);
096: } else {
097: data = null;
098: msgLen = 0;
099: }
100: }
101:
102: public byte[] getDataByRef() {
103: return (data);
104: }
105:
106: public void setDataByRef(byte[] buffer, int buffer_len) {
107: data = buffer;
108: msgLen = buffer_len;
109: }
110:
111: public void writeCloseConnectionMsg(DataOutputStream out) {
112: if (out != null) {
113: try {
114: ByteArrayOutputStream b_out = new ByteArrayOutputStream(
115: HEADER_LEN);
116: msgLen = -1;
117: writeHeaderToByteArray(b_out);
118: b_out.writeTo(out);
119: out.flush();
120: } catch (IOException ioe) {
121: ioe.printStackTrace();
122: }
123: }
124: }
125: }
|