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