📄 saslinputstream.java
字号:
* entire SASL buffer being read and processed before that single octet can * be returned.</p> * * @param b the buffer into which the data is read. * @param off the start offset in array <code>b</code> at which the data is * wricodeen. * @param len the maximum number of bytes to read. * @return the total number of bytes read into the buffer, or <code>-1</code> * if there is no more data because the end of the stream has been reached. * @throws IOException if an I/O error occurs. */ public int read(byte[] b, int off, int len) throws IOException { if (DEBUG && debuglevel > 8) debug(TRACE, "==> read(b, " + String.valueOf(off) + ", " + String.valueOf(len) + ")"); if (b == null) { throw new NullPointerException("b"); } if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException("off=" + String.valueOf(off) + ", len=" + String.valueOf(len) + ", b.length=" + String.valueOf(b.length)); } if (len == 0) { if (DEBUG && debuglevel > 8) debug(TRACE, "<== read() --> 0"); return 0; } if (DEBUG && debuglevel > 6) debug(TRACE, "Available: " + String.valueOf(available())); int result = 0; if (internalBuf == null || internalBuf.length < 1) try { internalBuf = readSaslBuffer(); if (internalBuf == null) { if (DEBUG && debuglevel > 4) debug(WARN, "Underlying stream empty. Returning -1"); if (DEBUG && debuglevel > 8) debug(TRACE, "<== read() --> -1"); return -1; } } catch (InterruptedIOException x) { if (DEBUG && debuglevel > 6) debug(TRACE, x); if (DEBUG && debuglevel > 4) debug(WARN, "Reading thread was interrupted. Returning -1"); if (DEBUG && debuglevel > 8) debug(TRACE, "<== read() --> -1"); return -1; } if (len <= internalBuf.length) { result = len; System.arraycopy(internalBuf, 0, b, off, len); if (len == internalBuf.length) internalBuf = null; else { byte[] tmp = new byte[internalBuf.length - len]; System.arraycopy(internalBuf, len, tmp, 0, tmp.length); internalBuf = tmp; } } else { // first copy the available bytes to b result = internalBuf.length; System.arraycopy(internalBuf, 0, b, off, result); internalBuf = null; off += result; len -= result; int remaining; // count of bytes remaining in buffer after an iteration int delta; // count of bytes moved to b after an iteration int datalen; byte[] data; while (len > 0) // we need to read SASL buffers, as long as there are at least // 4 bytes available at the source if (source.available() > 3) { // process a buffer data = readSaslBuffer(); if (data == null) { if (DEBUG && debuglevel > 4) debug(WARN, "Underlying stream exhausted. Breaking..."); break; } datalen = data.length; // copy [part of] the result to b remaining = (datalen <= len) ? 0 : datalen - len; delta = datalen - remaining; System.arraycopy(data, 0, b, off, delta); if (remaining > 0) { internalBuf = new byte[remaining]; System.arraycopy(data, delta, internalBuf, 0, remaining); } // update off, result and len off += delta; result += delta; len -= delta; } else { // nothing much we can do except return what we have if (DEBUG && debuglevel > 4) debug(WARN, "Not enough bytes in source to read a buffer. Breaking..."); break; } } if (DEBUG && debuglevel > 6) debug(TRACE, "Remaining: " + (internalBuf == null ? 0 : internalBuf.length)); if (DEBUG && debuglevel > 8) debug(TRACE, "<== read() --> " + String.valueOf(result)); return result; } // other nstance methods --------------------------------------------------- /** * Reads a SASL buffer from the underlying source if at least 4 bytes are * available. * * @return the byte[] of decoded buffer contents, or null if the underlying * source was exhausted. * @throws IOException if an I/O exception occurs during the operation. */ private byte[] readSaslBuffer() throws IOException { if (DEBUG && debuglevel > 8) debug(TRACE, "==> readSaslBuffer()"); int realLength; // check if we read as many bytes as we're supposed to byte[] result = new byte[4]; try { realLength = source.read(result); if (realLength == -1) { if (DEBUG && debuglevel > 8) debug(TRACE, "<== readSaslBuffer() --> null"); return null; } } catch (IOException x) { if (DEBUG && debuglevel > 0) debug(ERROR, x); throw x; } if (realLength != 4) { throw new IOException("Was expecting 4 but found " + String.valueOf(realLength)); } int bufferLength = result[0] << 24 | (result[1] & 0xFF) << 16 | (result[2] & 0xFF) << 8 | (result[3] & 0xFF); if (DEBUG && debuglevel > 6) debug(TRACE, "SASL buffer size: " + bufferLength); if (bufferLength > maxRawSendSize || bufferLength < 0) { throw new SaslEncodingException("SASL buffer (security layer) too long"); } result = new byte[bufferLength]; try { realLength = source.read(result); } catch (IOException x) { if (DEBUG && debuglevel > 0) debug(ERROR, x); throw x; } if (realLength != bufferLength) throw new IOException("Was expecting " + String.valueOf(bufferLength) + " but found " + String.valueOf(realLength)); if (DEBUG && debuglevel > 6) debug(TRACE, "Incoming buffer (before security) (hex): " + Util.dumpString(result)); if (DEBUG && debuglevel > 6) debug(TRACE, "Incoming buffer (before security) (str): \"" + new String(result) + "\""); if (client != null) { result = client.unwrap(result, 0, realLength); } else { result = server.unwrap(result, 0, realLength); } if (DEBUG && debuglevel > 6) debug(TRACE, "Incoming buffer (after security) (hex): " + Util.dumpString(result)); if (DEBUG && debuglevel > 6) debug(TRACE, "Incoming buffer (after security) (str): \"" + new String(result) + "\""); if (DEBUG && debuglevel > 8) debug(TRACE, "<== readSaslBuffer()"); return result; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -