Debian Codegraph @

gss/src/main/java/org/globus/gsi/gssapi/net/GssOutputStream.java

raw
1 /*
2 * Copyright 1999-2010 University of Chicago
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11 * express or implied.
12 *
13 * See the License for the specific language governing permissions and limitations under the License.
14 */
15 package org.globus.gsi.gssapi.net;
16
17 import java.io.OutputStream;
18 import java.io.IOException;
19
20 import org.globus.common.ChainedIOException;
21
22 import org.ietf.jgss.GSSContext;
23 import org.ietf.jgss.GSSException;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 public abstract class GssOutputStream extends OutputStream {
29
30 private static Log logger =
31 LogFactory.getLog(GssOutputStream.class.getName());
32
33 protected OutputStream out;
34 protected GSSContext context;
35
36 protected boolean autoFlush = false;
37
38 protected byte [] buff;
39 protected int index;
40
41 public GssOutputStream(OutputStream out, GSSContext context) {
42 this(out, context, 16384);
43 }
44
45 public GssOutputStream(OutputStream out, GSSContext context, int size) {
46 this.out = out;
47 this.context = context;
48 this.buff = new byte[size];
49 this.index = 0;
50 }
51
52 public void setAutoFlush(boolean autoFlush) {
53 this.autoFlush = autoFlush;
54 }
55
56 public boolean getAutoFlush() {
57 return this.autoFlush;
58 }
59
60 public void write(int b)
61 throws IOException {
62 if (this.index == this.buff.length) {
63 flushData();
64 }
65
66 buff[index++] = (byte)b;
67
68 if (this.autoFlush) {
69 flushData();
70 }
71 }
72
73 public void write(byte[] data)
74 throws IOException {
75 write(data, 0, data.length);
76 }
77
78 public void write(byte [] data, int off, int len)
79 throws IOException {
80 int max;
81 while (len > 0) {
82 if (this.index + len > this.buff.length) {
83 max = (this.buff.length - this.index);
84 System.arraycopy(data, off, this.buff, this.index, max);
85 this.index += max;
86 flushData();
87 len -= max;
88 off += max;
89 } else {
90 System.arraycopy(data, off, this.buff, this.index, len);
91 this.index += len;
92 if (this.autoFlush) {
93 flushData();
94 }
95 break;
96 }
97 }
98 }
99
100 protected byte[] wrap()
101 throws IOException {
102 try {
103 return context.wrap(this.buff, 0, this.index, null);
104 } catch (GSSException e) {
105 throw new ChainedIOException("wrap failed", e);
106 }
107 }
108
109 public abstract void flush()
110 throws IOException;
111
112 private void flushData()
113 throws IOException {
114 flush();
115 this.index = 0;
116 }
117
118 public void close()
119 throws IOException {
120 logger.debug("close");
121 flushData();
122 this.out.close();
123 }
124
125 }