📄 saslinputstream.java
字号:
/* SaslInputStream.java -- Copyright (C) 2003, 2006 Free Software Foundation, Inc.This file is a part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or (atyour option) any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; if not, write to the Free SoftwareFoundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301USALinking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package gnu.javax.crypto.sasl;import gnu.java.security.util.Util;import java.io.InputStream;import java.io.InterruptedIOException;import java.io.IOException;import java.io.PrintWriter;import javax.security.sasl.Sasl;import javax.security.sasl.SaslClient;import javax.security.sasl.SaslServer;/** * An input stream that uses either a {@link SaslClient} or a {@link SaslServer} * to process the data through these entities' security layer filter(s). */public class SaslInputStream extends InputStream{ // Debugging methods and variables // ------------------------------------------------------------------------- private static final String NAME = "SaslOutputStream"; private static final String ERROR = "ERROR"; private static final String WARN = " WARN"; // private static final String INFO = " INFO"; private static final String TRACE = "DEBUG"; private static final boolean DEBUG = true; private static final int debuglevel = 3; private static final PrintWriter err = new PrintWriter(System.out, true); private static void debug(String level, Object obj) { err.println("[" + level + "] " + NAME + ": " + String.valueOf(obj)); } // Constants and variables // ------------------------------------------------------------------------- private SaslClient client; private SaslServer server; private int maxRawSendSize; private InputStream source; private byte[] internalBuf; // Constructor(s) // ------------------------------------------------------------------------- public SaslInputStream(SaslClient client, InputStream source) throws IOException { super(); this.client = client; maxRawSendSize = Integer.parseInt((String) client.getNegotiatedProperty(Sasl.RAW_SEND_SIZE)); server = null; this.source = source; } public SaslInputStream(SaslServer server, InputStream source) throws IOException { super(); this.server = server; maxRawSendSize = Integer.parseInt((String) server.getNegotiatedProperty(Sasl.RAW_SEND_SIZE)); client = null; this.source = source; } // Class methods // ------------------------------------------------------------------------- // Instance methods // ------------------------------------------------------------------------- // Overloaded java.io.InputStream methods ---------------------------------- public int available() throws IOException { return (internalBuf == null) ? 0 : internalBuf.length; } public void close() throws IOException { source.close(); } /** * <p>Reads the next byte of data from the input stream. The value byte is * returned as an <code>int</code> in the range <code>0</code> to * <code>255</code>. If no byte is available because the end of the stream * has been reached, the value <code>-1</code> is returned. This method * blocks until input data is available, the end of the stream is detected, * or an exception is thrown.</p> * * <p>From a SASL mechanism provider's perspective, if a security layer has * been negotiated, the underlying <i>source</i> is expected to contain SASL * buffers, as defined in RFC 2222. Four octets in network byte order in the * front of each buffer identify the length of the buffer. The provider is * responsible for performing any integrity checking or other processing on * the buffer before returning the data as a stream of octets. For example, * the protocol driver's request for a single octet from the stream might; * i.e. an invocation of this method, may result in an entire SASL buffer * being read and processed before that single octet can be returned.</p> * * @return the next byte of data, or <code>-1</code> if the end of the stream * is reached. * @throws IOException if an I/O error occurs. */ public int read() throws IOException { int result = -1; if (internalBuf != null && internalBuf.length > 0) { result = internalBuf[0] & 0xFF; if (internalBuf.length == 1) internalBuf = new byte[0]; else { byte[] tmp = new byte[internalBuf.length - 1]; // System.arraycopy(internalBuf, 0, tmp, 0, tmp.length); System.arraycopy(internalBuf, 1, tmp, 0, tmp.length); internalBuf = tmp; } } else { byte[] buf = new byte[1]; int check = read(buf); result = (check > 0) ? (buf[0] & 0xFF) : -1; } return result; } /** * <p>Reads up to <code>len</code> bytes of data from the underlying * <i>source</i> input stream into an array of bytes. An attempt is made to * read as many as <code>len</code> bytes, but a smaller number may be read, * possibly zero. The number of bytes actually read is returned as an * integer.</p> * * <p>This method blocks until input data is available, end of file is * detected, or an exception is thrown.</p> * * <p>If <code>b</code> is <code>null</code>, a {@link NullPointerException} is * thrown.</p> * * <p>If <code>off</code> is negative, or <code>len</code> is negative, or * <code>off+len</code> is greater than the length of the array <code>b</code>, * then an {@link IndexOutOfBoundsException} is thrown.</p> * * <p>If <code>len</code> is zero, then no bytes are read and <code>0</code> * is returned; otherwise, there is an attempt to read at least one byte. If * no byte is available because the stream is at end of file, the value * <code>-1</code> is returned; otherwise, at least one byte is read and * stored into <code>b</code>.</p> * * <p>The first byte read is stored into element <code>b[off]</code>, the * next one into <code>b[off+1]</code>, and so on. The number of bytes read * is, at most, equal to <code>len</code>. Let <code>k</code> be the number * of bytes actually read; these bytes will be stored in elements * <code>b[off]</code> through <code>b[off+k-1]</code>, leaving elements * <code>b[off+k]</code> through <code>b[off+len-1]</code> unaffected.</p> * * <p>In every case, elements <code>b[0]</code> through <code>b[off]</code> * and elements <code>b[off+len]</code> through <code>b[b.length-1]</code> * are unaffected.</p> * * <p>If the first byte cannot be read for any reason other than end of file, * then an {@link IOException} is thrown. In particular, an {@link IOException} * is thrown if the input stream has been closed.</p> * * <p>From the SASL mechanism provider's perspective, if a security layer has * been negotiated, the underlying <i>source</i> is expected to contain SASL * buffers, as defined in RFC 2222. Four octets in network byte order in the * front of each buffer identify the length of the buffer. The provider is * responsible for performing any integrity checking or other processing on * the buffer before returning the data as a stream of octets. The protocol * driver's request for a single octet from the stream might result in an
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -