*/
public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
{
if ( buffer == null )
{
throw new EncoderException( I18n.err( I18n.ERR_148 ) );
}
try
{
// The PrincipalName SEQ Tag
buffer.put( UniversalTag.SEQUENCE_TAG );
buffer.put( TLV.getBytes( principalNameSeqLength ) );
// The name-type, first the tag, then the value
buffer.put( ( byte ) 0xA0 );
buffer.put( TLV.getBytes( principalTypeTagLength ) );
Value.encode( buffer, nameType.getOrdinal() );
// The name-string tag
buffer.put( ( byte ) 0xA1 );
buffer.put( TLV.getBytes( principalStringsTagLength ) );
// The name-string sequence
buffer.put( UniversalTag.SEQUENCE_TAG );
if ( ( nameString == null ) || ( nameString.size() == 0 ) )
{
buffer.put( ( byte ) 0x00 );
}
else
{
buffer.put( TLV.getBytes( principalStringsSeqLength ) );
// The kerberosStrings
for ( byte[] name : nameBytes )
{
buffer.put( UniversalTag.GENERALIZED_STRING_TAG );
if ( ( name == null ) || ( name.length == 0 ) )
{
buffer.put( ( byte ) 0x00 );
}
else
{
buffer.put( TLV.getBytes( name.length ) );
buffer.put( name );
}
}
}
}
catch ( BufferOverflowException boe )
{
LOG.error( I18n.err( I18n.ERR_146, 1 + TLV.getNbBytes( principalNameSeqLength )
+ principalNameSeqLength, buffer.capacity() ) );
throw new EncoderException( I18n.err( I18n.ERR_138 ) );
}
if ( IS_DEBUG )
{
LOG.debug( "PrinipalName encoding : {}", StringTools.dumpBytes( buffer.array() ) );