| 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; |
| 16 |
|
| 17 |
import java.io.InputStream; |
| 18 |
import java.io.IOException; |
| 19 |
import java.util.LinkedList; |
| 20 |
|
| 21 |
import org.apache.commons.logging.Log; |
| 22 |
import org.apache.commons.logging.LogFactory; |
| 23 |
|
| 24 |
/** |
| 25 |
* Used as token-oriented input stream needed for SSL library I/O abstraction. |
| 26 |
*/ |
| 27 |
public class TokenInputStream extends InputStream { |
| 28 |
|
| 29 |
private static Log logger = |
| 30 |
LogFactory.getLog(TokenInputStream.class.getName()); |
| 31 |
|
| 32 |
private LinkedList tokens; // list of buffers |
| 33 |
private byte [] buff; // current buffer |
| 34 |
private int index; // position within current buffer |
| 35 |
|
| 36 |
private boolean closed; |
| 37 |
|
| 38 |
public TokenInputStream() { |
| 39 |
this.tokens = new LinkedList(); |
| 40 |
this.index = 0; |
| 41 |
this.closed = false; |
| 42 |
} |
| 43 |
|
| 44 |
// main function |
| 45 |
public void putToken(byte [] buf, int off, int len) { |
| 46 |
if (buf == null || len <=0) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
if (logger.isDebugEnabled()) { |
| 51 |
logger.debug("put token: " + len); |
| 52 |
} |
| 53 |
|
| 54 |
byte[] localBuf = buf; |
| 55 |
if (off != 0) { |
| 56 |
localBuf = new byte[len]; |
| 57 |
System.arraycopy(buf, off, localBuf, 0, len); |
| 58 |
} |
| 59 |
|
| 60 |
synchronized(this) { |
| 61 |
if (this.buff == null || |
| 62 |
this.buff != null && this.buff.length == this.index) { |
| 63 |
this.buff = localBuf; |
| 64 |
this.index = 0; |
| 65 |
} else { |
| 66 |
this.tokens.add(localBuf); |
| 67 |
} |
| 68 |
notify(); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
public int read(byte [] data) |
| 73 |
throws IOException { |
| 74 |
return read(data, 0, data.length); |
| 75 |
} |
| 76 |
|
| 77 |
public int read(byte [] data, int off, int len) |
| 78 |
throws IOException { |
| 79 |
if (logger.isDebugEnabled()) { |
| 80 |
logger.debug("read byte array: " + len); |
| 81 |
} |
| 82 |
|
| 83 |
if (!checkData()) { |
| 84 |
return -1; |
| 85 |
} |
| 86 |
|
| 87 |
int size = Math.min(len, buff.length-index); |
| 88 |
System.arraycopy(buff, index, data, off, size); |
| 89 |
index += size; |
| 90 |
|
| 91 |
return size; |
| 92 |
} |
| 93 |
|
| 94 |
public int read() |
| 95 |
throws IOException { |
| 96 |
logger.debug("read byte"); |
| 97 |
|
| 98 |
if (!checkData()) { |
| 99 |
return -1; |
| 100 |
} |
| 101 |
|
| 102 |
return buff[index++] & 0xff; |
| 103 |
} |
| 104 |
|
| 105 |
protected synchronized boolean checkData() { |
| 106 |
try { |
| 107 |
while(!hasData()) { |
| 108 |
wait(); |
| 109 |
if (closed) { |
| 110 |
return false; |
| 111 |
} |
| 112 |
} |
| 113 |
} catch(Exception e) { |
| 114 |
return false; |
| 115 |
} |
| 116 |
return true; |
| 117 |
} |
| 118 |
|
| 119 |
protected boolean hasData() { |
| 120 |
if (this.buff == null) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
if (this.buff.length == this.index) { |
| 125 |
if (tokens.isEmpty()) { |
| 126 |
return false; |
| 127 |
} else { |
| 128 |
this.buff = (byte[])tokens.removeFirst(); |
| 129 |
this.index = 0; |
| 130 |
return true; |
| 131 |
} |
| 132 |
} |
| 133 |
return true; |
| 134 |
} |
| 135 |
|
| 136 |
public int available() |
| 137 |
throws IOException { |
| 138 |
if (!hasData()) { |
| 139 |
return 0; |
| 140 |
} else { |
| 141 |
return buff.length-index; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
public void close() |
| 146 |
throws IOException { |
| 147 |
logger.debug("close() called"); |
| 148 |
synchronized(this) { |
| 149 |
this.closed = true; |
| 150 |
notify(); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
public String toString() { |
| 155 |
return tokens.toString() + " " + index + " " + buff.length; |
| 156 |
} |
| 157 |
|
| 158 |
} |