*/
public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
{
if ( buffer == null )
{
throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
}
try
{
// The SubstringFilter Tag
buffer.put( ( byte ) LdapConstants.SUBSTRINGS_FILTER_TAG );
buffer.put( TLV.getBytes( substringsFilterLength ) );
// The type
Value.encode( buffer, type.getBytes() );
// The SubstringSequenceFilter Tag
buffer.put( UniversalTag.SEQUENCE.getValue() );
buffer.put( TLV.getBytes( substringsFilterSequenceLength ) );
if ( ( initialSubstrings == null ) && ( ( anySubstrings == null ) || ( anySubstrings.size() == 0 ) )
&& ( finalSubstrings == null ) )
{
throw new EncoderException( I18n.err( I18n.ERR_04058 ) );
}
// The initial substring
if ( initialSubstrings != null )
{
byte[] initialBytes = Strings.getBytesUtf8( initialSubstrings );
buffer.put( ( byte ) LdapConstants.SUBSTRINGS_FILTER_INITIAL_TAG );
buffer.put( TLV.getBytes( initialBytes.length ) );
buffer.put( initialBytes );
}
// The any substrings
if ( anySubstrings != null )
{
for ( String any : anySubstrings )
{
byte[] anyBytes = Strings.getBytesUtf8( any );
buffer.put( ( byte ) LdapConstants.SUBSTRINGS_FILTER_ANY_TAG );
buffer.put( TLV.getBytes( anyBytes.length ) );
buffer.put( anyBytes );
}
}
// The final substring
if ( finalSubstrings != null )
{
byte[] finalBytes = Strings.getBytesUtf8( finalSubstrings );
buffer.put( ( byte ) LdapConstants.SUBSTRINGS_FILTER_FINAL_TAG );
buffer.put( TLV.getBytes( finalBytes.length ) );
buffer.put( finalBytes );
}
}
catch ( BufferOverflowException boe )
{
throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
}
return buffer;
}