Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.bouncycastle.crypto.tls;
  
  
 
 public class HeartbeatMessage
 {
     protected short type;
     protected byte[] payload;
     protected int paddingLength;
 
     public HeartbeatMessage(short typebyte[] payloadint paddingLength)
     {
         if (!HeartbeatMessageType.isValid(type))
         {
             throw new IllegalArgumentException("'type' is not a valid HeartbeatMessageType value");
         }
         if (payload == null || payload.length >= (1 << 16))
         {
             throw new IllegalArgumentException("'payload' must have length < 2^16");
         }
         if (paddingLength < 16)
         {
             throw new IllegalArgumentException("'paddingLength' must be at least 16");
         }
 
         this. = type;
         this. = payload;
         this. = paddingLength;
     }

    
Encode this HeartbeatMessage to an java.io.OutputStream.

Parameters:
output the java.io.OutputStream to encode to.
Throws:
java.io.IOException
 
     public void encode(TlsContext contextOutputStream outputthrows IOException
     {
         TlsUtils.writeUint8(output);
 
         TlsUtils.checkUint16(.);
         TlsUtils.writeUint16(.output);
         output.write();
 
         byte[] padding = new byte[];
         context.getNonceRandomGenerator().nextBytes(padding);
         output.write(padding);
     }

    
Parse a HeartbeatMessage from an java.io.InputStream.

Parameters:
input the java.io.InputStream to parse from.
Returns:
a HeartbeatMessage object.
Throws:
java.io.IOException
 
     public static HeartbeatMessage parse(InputStream inputthrows IOException
     {
         short type = TlsUtils.readUint8(input);
         if (!HeartbeatMessageType.isValid(type))
         {
             throw new TlsFatalAlert(.);
         }
 
         int payload_length = TlsUtils.readUint16(input);
 
         PayloadBuffer buf = new PayloadBuffer();
         Streams.pipeAll(inputbuf);
 
         byte[] payload = buf.toTruncatedByteArray(payload_length);
         if (payload == null)
         {
             /*
              * RFC 6520 4. If the payload_length of a received HeartbeatMessage is too large, the
              * received HeartbeatMessage MUST be discarded silently.
              */
             return null;
         }
 
         int padding_length = buf.size() - payload.length;
 
         /*
          * RFC 6520 4. The padding of a received HeartbeatMessage message MUST be ignored
          */
         return new HeartbeatMessage(typepayloadpadding_length);
     }
 
     static class PayloadBuffer extends ByteArrayOutputStream
     {
         byte[] toTruncatedByteArray(int payloadLength)
         {
            /*
             * RFC 6520 4. The padding_length MUST be at least 16.
             */
            int minimumCount = payloadLength + 16;
            if ( < minimumCount)
            {
                return null;
            }
            return Arrays.copyOf(payloadLength);
        }
    }
New to GrepCode? Check out our FAQ X