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.server.kerberos.shared.messages.value; 021 022 023 import java.util.BitSet; 024 025 026 /** 027 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 028 * @version $Rev: 540371 $, $Date: 2007-05-22 02:00:43 +0200 (Tue, 22 May 2007) $ 029 */ 030 public abstract class Options 031 { 032 private BitSet options; 033 private int maxSize; 034 035 036 protected Options( int maxSize ) 037 { 038 this.maxSize = maxSize; 039 options = new BitSet( maxSize ); 040 } 041 042 043 /** 044 * Returns whether the option at a given index matches the option in this {@link Options}. 045 * 046 * @param options 047 * @param option 048 * @return true if two options are the same. 049 */ 050 public boolean match( Options options, int option ) 051 { 052 return options.get( option ) == this.get( option ); 053 } 054 055 056 /** 057 * Returns the value of the option at the given index. 058 * 059 * @param index 060 * @return true if the option at the given index is set. 061 */ 062 public boolean get( int index ) 063 { 064 return options.get( index ); 065 } 066 067 068 /** 069 * Sets the option at a given index. 070 * 071 * @param index 072 */ 073 public void set( int index ) 074 { 075 options.set( index ); 076 } 077 078 079 /** 080 * Clears (sets false) the option at a given index. 081 * 082 * @param index 083 */ 084 public void clear( int index ) 085 { 086 options.clear( index ); 087 } 088 089 090 /** 091 * Byte-reversing methods are an anomaly of the BouncyCastle 092 * DERBitString endianness. Thes methods can be removed if the 093 * Apache Directory Snickers codecs operate differently. 094 * 095 * @return The raw {@link Options} bytes. 096 */ 097 public byte[] getBytes() 098 { 099 byte[] bytes = new byte[maxSize / 8]; 100 101 for ( int ii = 0; ii < maxSize; ii++ ) 102 { 103 if ( options.get( reversePosition( ii ) ) ) 104 { 105 bytes[bytes.length - ii / 8 - 1] |= 1 << ( ii % 8 ); 106 } 107 } 108 return bytes; 109 } 110 111 112 protected void setBytes( byte[] bytes ) 113 { 114 for ( int ii = 0; ii < bytes.length * 8; ii++ ) 115 { 116 if ( ( bytes[bytes.length - ii / 8 - 1] & ( 1 << ( ii % 8 ) ) ) > 0 ) 117 { 118 options.set( reversePosition( ii ) ); 119 } 120 } 121 } 122 123 124 private int reversePosition( int position ) 125 { 126 return maxSize - 1 - position; 127 } 128 }