Java 8 SSLEngineResult - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 SSLEngineResult class.

This code example shows how to use the following methods:bytesConsumed, bytesProduced, getHandshakeStatus, getStatus
            resultOnce = false;
            Log.info("The format of the SSLEngineResult is: \n" 
                    + "\t\"getStatus() / getHandshakeStatus()\" +\n" 
                    + "\t\"bytesConsumed() / bytesProduced()\"\n"); 
        } 
        HandshakeStatus hsStatus = result.getHandshakeStatus();
        Log.info(str + result.getStatus() + "/" + hsStatus + ", " + result.bytesConsumed() + "/"
                + result.bytesProduced() + " bytes");
        if (hsStatus == HandshakeStatus.FINISHED) {
            Log.info("\t...ready for application data"); 
        } 
    } 
 
    protected SSLEngine getTlsEngine() {
        return tlsEngine;
    } 
} 
5
CodeRank
This code example shows how to use the following methods:bytesConsumed, bytesProduced, getHandshakeStatus, getStatus
        } 
    } 
 
    private String resultToString(SSLEngineResult result)
    { 
        return new StringBuilder("[SSLEngineResult status = ").append(result.getStatus())
                .append(" handshakeStatus = ").append(result.getHandshakeStatus())
                .append(" bytesConsumed = ").append(result.bytesConsumed())
                .append(" bytesProduced = ").append(result.bytesProduced())
                .append("]").toString();
    } 
 
    @Override 
    public int capacity() 
    { 
        if (_tail_closed) return Transport.END_OF_STREAM;
        return _inputBuffer.remaining();
    } 
 
    @Override 
5
CodeRank
This code example shows how to use the following methods:bytesProduced, getHandshakeStatus, getStatus
 
    private static boolean needsUnwrap(boolean handshake, SSLEngineResult result, int totalBytesConsumed, int totalBytesToBeConsumed)
    { 
        if (!handshake)
        { 
            return result.getStatus() == SSLEngineResult.Status.OK && result.getHandshakeStatus().equals(SSLEngineResult.HandshakeStatus.NEED_UNWRAP) && result.bytesProduced() == 0;
        } 
        else 
        { 
            return result.getStatus() == SSLEngineResult.Status.OK && (result.bytesProduced() != 0 || totalBytesConsumed < totalBytesToBeConsumed);
        } 
    } 
 
} 
5
CodeRank
This code example shows how to use the following methods:bytesConsumed, bytesProduced
            e.wrap(bbs, bbd);
            fail("SSLException must be thrown");
        } catch (SSLException ex) {
        } 
        SSLEngineResult res = e.wrap(bbd, bbs);
        assertEquals(10, res.bytesConsumed());
        assertEquals(20, res.bytesProduced());
 
        try { 
            e.wrap(new ByteBuffer[] { bbs }, bbd);
            fail("SSLException must be thrown");
        } catch (SSLException ex) {
        } 
        res = e.wrap(new ByteBuffer[] { bbd }, bbs);
        assertEquals(10, res.bytesConsumed());
        assertEquals(20, res.bytesProduced());
    } 
 
    /** 
     * Test for <code>wrap(ByteBuffer src, ByteBuffer dst)</code> method 
5
CodeRank
This code example shows how to use the following methods:bytesConsumed, bytesProduced, getHandshakeStatus, getStatus
            System.out.println("\nClient sends responding close notify");
        } 
        print(result = client.wrap(ByteBuffer.wrap(new byte[] {0}), buffer));
        assertEquals("Unexpected status of operation:",
                SSLEngineResult.Status.CLOSED,
                result.getStatus());
        assertEquals("Unexpected status of operation:",
                SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING,
                result.getHandshakeStatus());
        assertEquals(
            "The length of the consumed data differs from expected", 
            0, result.bytesConsumed());
        assertTrue(
            "The length of the produced data differs from expected", 
            result.bytesProduced() > 0);
        if (doLog) {
            System.out.println(
                    "\nClient tries to send data after closure alert"); 
        } 
        // this data will not be sent (should do nothing - 0 cons, 0 prod) 
5
CodeRank
This code example shows how to use the following methods:bytesProduced, getHandshakeStatus
		int n = 0;
		SSLEngineResult result = encode(EMPTY_BUFFER);
		if (log.isLoggable(Level.FINE)) {
			log.fine("wrapped " + result);
		} 
		if (result.bytesProduced() > 0) {
			wrapData.flip();
			n = writeToChannel(wrapData);
 
			if (result.getHandshakeStatus().equals(HandshakeStatus.NEED_WRAP)) {
				n += wrapAndSend();
			} 
			return n;
		} else { 
			log.warning("wrap produced no data " + getRemoteHost());
		} 
		return n;
	} 
	/** 
	 * encrypt application data, does not write data to socket. After this method call the data to be 
4
CodeRank
This code example shows how to use the following methods:getHandshakeStatus, getStatus
                    // "SSL-handshake doing: unwrap"); 
                    inNetBuffer.flip(); // ready for being read
                    result = sslEngine.unwrap(inNetBuffer.getBuffer(), dummyBuffer.getBuffer());
                    inNetBuffer.compact(); // ready for reading from channel
                     
                    handshakeStatus = result.getHandshakeStatus();
                    switch (result.getStatus()) {
                    case OK: {
                        analyseHandshakeStatus(key, handshakeStatus);
                        break; 
                    } 
                    case BUFFER_UNDERFLOW: {
                        // needed more data in inNetBuffer, maybe nexttime 
                        underflow = true;
                        key.interestOps(key.interestOps() | SelectionKey.OP_READ);
                        break; 
                    } 
                    case CLOSED: {
                        throw new IOException("The SSLEngine is already closed.");
                    } 
4
CodeRank
This code example shows how to use the following methods:bytesConsumed, bytesProduced
            e.wrap(new ByteBuffer[] { bbs }, bbd);
            fail("SSLException must be thrown");
        } catch (SSLException ex) {
        } 
        res = e.wrap(new ByteBuffer[] { bbd }, bbs);
        assertEquals(10, res.bytesConsumed());
        assertEquals(20, res.bytesProduced());
    } 
 
    /** 
     * Test for <code>wrap(ByteBuffer src, ByteBuffer dst)</code> method 
     * 
     * Assertion: encodes a buffer data into network data. 
     * 
     */ 
    public void testWrap04() throws SSLException { 
        String host = "new host";
        int port = 8080;
        ByteBuffer bb = ByteBuffer.allocate(10);
        SSLEngine e = new mySSLEngine(host, port);
4
CodeRank
This code example shows how to use the following methods:getHandshakeStatus, getStatus
        //prep the buffer for the close message 
        netOutBuffer.clear();
        //perform the close, since we called sslEngine.closeOutbound 
        SSLEngineResult handshake = sslEngine.wrap(getEmptyBuf(), netOutBuffer);
        //we should be in a close state 
        if (handshake.getStatus() != SSLEngineResult.Status.CLOSED) {
            throw new IOException("Invalid close state, will not send network data.");
        } 
        //prepare the buffer for writing 
        netOutBuffer.flip();
        //if there is data to be written 
        flush(netOutBuffer);
 
        //is the channel closed? 
        closed = (!netOutBuffer.hasRemaining() && (handshake.getHandshakeStatus() != HandshakeStatus.NEED_WRAP));
    } 
 
    /** 
     * Force a close, can throw an IOException 
     * @param force boolean 
4
CodeRank
This code example shows how to use the following methods:
    } 
 
    @Override 
    public SSLEngineResult unwrap(ByteBuffer src, ByteBuffer[] dsts,
            int offset, int length) throws SSLException {
        return new SSLEngineResult(SSLEngineResult.Status.OK,
                SSLEngineResult.HandshakeStatus.FINISHED, 1, 2);
    } 
 
    @Override 
    public SSLEngineResult wrap(ByteBuffer[] srcs, int offset, int length,
            ByteBuffer dst) throws SSLException {
        return new SSLEngineResult(SSLEngineResult.Status.OK,
                SSLEngineResult.HandshakeStatus.FINISHED, 10, 20);
    } 
 
    @Override 
    public SSLParameters getSSLParameters() {
        // TODO Auto-generated method stub 
        return null; 
3
CodeRank
See Code Examples for Java 8 SSLEngineResult Methods: