1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43:
44: import ;
45: import ;
46: import ;
47:
48: import ;
49: import ;
50: import ;
51:
52:
56: public class SaslOutputStream
57: extends OutputStream
58: {
59: private static final Logger log = Configuration.DEBUG ?
60: Logger.getLogger(SaslOutputStream.class.getName()) : null;
61: private SaslClient client;
62: private SaslServer server;
63: private int maxRawSendSize;
64: private OutputStream dest;
65:
66: public SaslOutputStream(SaslClient client, OutputStream dest)
67: throws IOException
68: {
69: super();
70:
71: this.client = client;
72: String size = (String) client.getNegotiatedProperty(Sasl.RAW_SEND_SIZE);
73: maxRawSendSize = Integer.parseInt(size);
74: server = null;
75: this.dest = dest;
76: }
77:
78: public SaslOutputStream(SaslServer server, OutputStream dest)
79: throws IOException
80: {
81: super();
82:
83: this.server = server;
84: String size = (String) server.getNegotiatedProperty(Sasl.RAW_SEND_SIZE);
85: maxRawSendSize = Integer.parseInt(size);
86: client = null;
87: this.dest = dest;
88: }
89:
90: public void close() throws IOException
91: {
92: dest.flush();
93: dest.close();
94: }
95:
96: public void flush() throws IOException
97: {
98: dest.flush();
99: }
100:
101:
107: public void write(int b) throws IOException
108: {
109: write(new byte[] { (byte) b });
110: }
111:
112:
118: public void write(byte[] b, int off, int len) throws IOException
119: {
120: if (Configuration.DEBUG)
121: log.entering(this.getClass().getName(), "write");
122: if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length)
123: || ((off + len) < 0))
124: throw new IndexOutOfBoundsException("off=" + off + ", len=" + len
125: + ", b.length=" + b.length);
126: if (len == 0)
127: {
128: if (Configuration.DEBUG)
129: log.exiting(this.getClass().getName(), "write");
130: return;
131: }
132: int chunckSize, length, chunck = 1;
133: byte[] output = null, result;
134: if (Configuration.DEBUG)
135: log.finer("About to wrap " + len + " byte(s)...");
136: while (len > 0)
137: {
138: chunckSize = (len > maxRawSendSize ? maxRawSendSize : len);
139: if (Configuration.DEBUG)
140: {
141: log.finer("Outgoing buffer (before security) (hex): "
142: + Util.dumpString(b, off, chunckSize));
143: log.finer("Outgoing buffer (before security) (str): \""
144: + new String(b, off, chunckSize) + "\"");
145: }
146: if (client != null)
147: output = client.wrap(b, off, chunckSize);
148: else
149: output = server.wrap(b, off, chunckSize);
150:
151: if (Configuration.DEBUG)
152: {
153: log.finer("Outgoing buffer (after security) (hex): "
154: + Util.dumpString(output));
155: log.finer("Outgoing buffer (after security) (str): \""
156: + new String(output) + "\"");
157: }
158: length = output.length;
159: result = new byte[length + 4];
160: result[0] = (byte)(length >>> 24);
161: result[1] = (byte)(length >>> 16);
162: result[2] = (byte)(length >>> 8);
163: result[3] = (byte) length;
164: System.arraycopy(output, 0, result, 4, length);
165: dest.write(result);
166: off += chunckSize;
167: len -= chunckSize;
168: if (Configuration.DEBUG)
169: log.finer("Wrapped chunck #" + chunck);
170: chunck++;
171: }
172: dest.flush();
173: if (Configuration.DEBUG)
174: log.exiting(this.getClass().getName(), "write");
175: }
176: }