Class HandshakeIODataStream
- All Implemented Interfaces:
Closeable,AutoCloseable,Appendable,DataStream
public class HandshakeIODataStream extends SSLInputStream implements Appendable, DataStream
-
Constructor Summary
Constructors Constructor Description HandshakeIODataStream() -
Method Summary
Modifier and Type Method Description voidappend(byte[] src)Appends the income data to be read by handshake protocol.intavailable()Returns an estimated number of bytes that can be read or skipped without blocking for more input.protected voidclearBuffer()byte[]getData(int length)returns the chunk of stored data with the length no more than specified.protected byte[]getDigestMD5()Returns the MD5 digest of the data passed throught the streamprotected byte[]getDigestMD5withoutLast()Returns the MD5 digest of the data passed throught the stream except last messageprotected byte[]getDigestSHA()Returns the SHA-1 digest of the data passed throught the streamprotected byte[]getDigestSHAwithoutLast()Returns the SHA-1 digest of the data passed throught the stream except last messageprotected byte[]getMessages()Returns all the data passed throught the streambooleanhasData()Checks if there is data to be read.voidmark()voidmark(int limit)Sets a mark position in this InputStream.booleanmarkSupported()Indicates whether this stream supports themark()andreset()methods.intread()read an opaque value;intread(byte[] dst, int offset, int length)Reads up tobyteCountbytes from this stream and stores them in the byte arraybufferstarting atbyteOffset.byte[]read(int length)reads vector of opaque valuesprotected voidremoveFromMarkedPosition()Removes the data from the marked position to the current read position.voidreset()Resets this stream to the last marked location.voidwrite(byte b)Writes an opaque valuevoidwrite(byte[] vector)writes vector of opaque valuesvoidwriteUint16(long n)Writes Uint16 valuevoidwriteUint24(long n)Writes Uint24 valuevoidwriteUint32(long n)Writes Uint32 valuevoidwriteUint64(long n)Writes Uint64 valuevoidwriteUint8(long n)Writes Uint8 valueMethods inherited from class org.conscrypt.SSLInputStream
readUint16, readUint24, readUint32, readUint64, readUint8Methods inherited from class java.io.InputStream
close, read, skip
-
Constructor Details
-
HandshakeIODataStream
public HandshakeIODataStream()
-
-
Method Details
-
available
public int available()Description copied from class:InputStreamReturns an estimated number of bytes that can be read or skipped without blocking for more input.Note that this method provides such a weak guarantee that it is not very useful in practice.
Firstly, the guarantee is "without blocking for more input" rather than "without blocking": a read may still block waiting for I/O to complete — the guarantee is merely that it won't have to wait indefinitely for data to be written. The result of this method should not be used as a license to do I/O on a thread that shouldn't be blocked.
Secondly, the result is a conservative estimate and may be significantly smaller than the actual number of bytes available. In particular, an implementation that always returns 0 would be correct. In general, callers should only use this method if they'd be satisfied with treating the result as a boolean yes or no answer to the question "is there definitely data ready?".
Thirdly, the fact that a given number of bytes is "available" does not guarantee that a read or skip will actually read or skip that many bytes: they may read or skip fewer.
It is particularly important to realize that you must not use this method to size a container and assume that you can read the entirety of the stream without needing to resize the container. Such callers should probably write everything they read to a
ByteArrayOutputStreamand convert that to a byte array. Alternatively, if you're reading from a file,File.length()returns the current length of the file (though assuming the file's length can't change may be incorrect, reading a file is inherently racy).The default implementation of this method in
InputStreamalways returns 0. Subclasses should override this method if they are able to indicate the number of bytes available.- Specified by:
availablein classSSLInputStream- Returns:
- the estimated number of bytes available
-
markSupported
public boolean markSupported()Description copied from class:InputStreamIndicates whether this stream supports themark()andreset()methods. The default implementation returnsfalse.- Overrides:
markSupportedin classInputStream- Returns:
- always
false. - See Also:
InputStream.mark(int),InputStream.reset()
-
mark
public void mark(int limit)Description copied from class:InputStreamSets a mark position in this InputStream. The parameterreadlimitindicates how many bytes can be read before the mark is invalidated. Sendingreset()will reposition the stream back to the marked position providedreadLimithas not been surpassed.This default implementation does nothing and concrete subclasses must provide their own implementation.
- Overrides:
markin classInputStream- Parameters:
limit- the number of bytes that can be read from this stream before the mark is invalidated.- See Also:
InputStream.markSupported(),InputStream.reset()
-
mark
public void mark() -
reset
public void reset()Description copied from class:InputStreamResets this stream to the last marked location. Throws anIOExceptionif the number of bytes read since the mark has been set is greater than the limit provided tomark, or if no mark has been set.This implementation always throws an
IOExceptionand concrete subclasses should provide the proper implementation.- Overrides:
resetin classInputStream
-
removeFromMarkedPosition
protected void removeFromMarkedPosition()Removes the data from the marked position to the current read position. The method is usefull when it is needed to delete one message from the internal buffer. -
read
read an opaque value;- Specified by:
readin classSSLInputStream- Parameters:
byte- : byte- Returns:
- Throws:
IOException- if the stream is closed or another IOException occurs.- See Also:
SSLStreamedInput.read(),SSLBufferedInput.read(),read()
-
read
reads vector of opaque values- Overrides:
readin classSSLInputStream- Parameters:
new- : long- Returns:
- Throws:
IOException- if read operation could not be finished.
-
read
Description copied from class:InputStreamReads up tobyteCountbytes from this stream and stores them in the byte arraybufferstarting atbyteOffset. Returns the number of bytes actually read or -1 if the end of the stream has been reached.- Overrides:
readin classSSLInputStream- Throws:
IOException- if the stream is closed or another IOException occurs.
-
append
public void append(byte[] src)Appends the income data to be read by handshake protocol. The attempts to overflow the buffer by means of this methods seem to be futile because of: 1. The SSL protocol specifies the maximum size of the record and record protocol does not pass huge messages. (see TLS v1 specification http://www.ietf.org/rfc/rfc2246.txt , p 6.2) 2. After each call of this method, handshake protocol should start (and starts) the operations on received data and recognize the fake data if such was provided (to check the size of certificate for example).- Specified by:
appendin interfaceAppendable- Parameters:
src- the source data to be appended.
-
clearBuffer
protected void clearBuffer() -
write
public void write(byte b)Writes an opaque value- Parameters:
byte- : byte
-
writeUint8
public void writeUint8(long n)Writes Uint8 value- Parameters:
long- : the value to be written (last byte)
-
writeUint16
public void writeUint16(long n)Writes Uint16 value- Parameters:
long- : the value to be written (last 2 bytes)
-
writeUint24
public void writeUint24(long n)Writes Uint24 value- Parameters:
long- : the value to be written (last 3 bytes)
-
writeUint32
public void writeUint32(long n)Writes Uint32 value- Parameters:
long- : the value to be written (last 4 bytes)
-
writeUint64
public void writeUint64(long n)Writes Uint64 value- Parameters:
long- : the value to be written
-
write
public void write(byte[] vector)writes vector of opaque values- Parameters:
vector- the vector to be written
-
hasData
public boolean hasData()Description copied from interface:DataStreamChecks if there is data to be read.- Specified by:
hasDatain interfaceDataStream- Returns:
- true if there is the input data in the stream, false otherwise
-
getData
public byte[] getData(int length)returns the chunk of stored data with the length no more than specified.- Specified by:
getDatain interfaceDataStream- Parameters:
length- : int- Returns:
-
getDigestMD5
protected byte[] getDigestMD5()Returns the MD5 digest of the data passed throught the stream- Returns:
- MD5 digest
-
getDigestSHA
protected byte[] getDigestSHA()Returns the SHA-1 digest of the data passed throught the stream- Returns:
- SHA-1 digest
-
getDigestMD5withoutLast
protected byte[] getDigestMD5withoutLast()Returns the MD5 digest of the data passed throught the stream except last message- Returns:
- MD5 digest
-
getDigestSHAwithoutLast
protected byte[] getDigestSHAwithoutLast()Returns the SHA-1 digest of the data passed throught the stream except last message- Returns:
- SHA-1 digest
-
getMessages
protected byte[] getMessages()Returns all the data passed throught the stream- Returns:
- all the data passed throught the stream at the moment
-