001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.shared.ldap.codec.controls;
021
022
023 import java.nio.ByteBuffer;
024
025 import org.apache.directory.shared.asn1.ber.tlv.Value;
026 import org.apache.directory.shared.asn1.codec.EncoderException;
027 import org.apache.directory.shared.i18n.I18n;
028 import org.apache.directory.shared.ldap.util.StringTools;
029
030
031 /**
032 * A genericcodec Control.
033 *
034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035 * @version $Rev: 764131 $, $Date: 2009-04-11 03:03:00 +0200 (Sat, 11 Apr 2009) $,
036 */
037 public class ControlImpl extends AbstractControl
038 {
039 /**
040 * Default constructor.
041 */
042 public ControlImpl( String oid )
043 {
044 super( oid );
045
046 decoder = null;
047 }
048
049
050 /**
051 * Set the encoded control value
052 *
053 * @param encodedValue The encoded control value to store
054 */
055 public void setValue( byte[] value )
056 {
057 if ( value != null )
058 {
059 this.value = new byte[ value.length ];
060 System.arraycopy( value, 0, this.value, 0, value.length );
061 }
062 else
063 {
064 this.value = null;
065 }
066 }
067
068
069 /**
070 * Get the raw control encoded bytes
071 *
072 * @return the encoded bytes for the control
073 */
074 public byte[] getValue()
075 {
076 if ( value == null )
077 {
078 return StringTools.EMPTY_BYTES;
079 }
080
081 final byte[] copy = new byte[ value.length ];
082 System.arraycopy( value, 0, copy, 0, value.length );
083 return copy;
084 }
085
086
087 /**
088 * {@inheritDoc}
089 */
090 @Override
091 public int computeLength()
092 {
093 if ( value != null )
094 {
095 return super.computeLength( value.length );
096 }
097 else
098 {
099 return super.computeLength( 0 );
100 }
101 }
102
103
104 /**
105 * {@inheritDoc}
106 */
107 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
108 {
109 if ( buffer == null )
110 {
111 throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
112 }
113
114 // Encode the Control envelop
115 super.encode( buffer );
116
117 // If we have a value, encode it
118 if ( value != null )
119 {
120 Value.encode( buffer, value );
121 }
122
123 return buffer;
124 }
125 }