001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
003: *
004: * This code is free software; you can redistribute it and/or modify it
005: * under the terms of the GNU General Public License version 2 only, as
006: * published by the Free Software Foundation. Sun designates this
007: * particular file as subject to the "Classpath" exception as provided
008: * by Sun in the LICENSE file that accompanied this code.
009: *
010: * This code is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
013: * version 2 for more details (a copy is included in the LICENSE file that
014: * accompanied this code).
015: *
016: * You should have received a copy of the GNU General Public License version
017: * 2 along with this work; if not, write to the Free Software Foundation,
018: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
019: *
020: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
021: * CA 95054 USA or visit www.sun.com if you need additional information or
022: * have any questions.
023: */
024:
025: /*
026: * @(#)KerberosFlags.java 1.16 07/04/27
027: *
028: * (C) Copyright IBM Corp. 1999 All Rights Reserved.
029: * Copyright 1997 The Open Group Research Institute. All rights reserved.
030: */
031:
032: package sun.security.krb5.internal.util;
033:
034: import java.io.IOException;
035: import java.util.Arrays;
036: import sun.security.krb5.internal.Krb5;
037: import sun.security.util.BitArray;
038: import sun.security.util.DerOutputStream;
039:
040: /**
041: * A wrapper class around sun.security.util.BitArray, so that KDCOptions,
042: * TicketFlags and ApOptions in krb5 classes can utilize some functions
043: * in BitArray classes.
044: *
045: * The data type is defined in RFC 4120 as:
046: *
047: * 5.2.8. KerberosFlags
048: *
049: * For several message types, a specific constrained bit string type,
050: * KerberosFlags, is used.
051: *
052: * KerberosFlags ::= BIT STRING (SIZE (32..MAX))
053: * -- minimum number of bits shall be sent,
054: * -- but no fewer than 32
055: *
056: * @author Yanni Zhang
057: * @version 1.0 22 June 2000
058: */
059: public class KerberosFlags {
060: BitArray bits;
061:
062: // This constant is used by child classes.
063: protected static final int BITS_PER_UNIT = 8;
064:
065: public KerberosFlags(int length) throws IllegalArgumentException {
066: bits = new BitArray(length);
067: }
068:
069: public KerberosFlags(int length, byte[] a)
070: throws IllegalArgumentException {
071: bits = new BitArray(length, a);
072: if (length != Krb5.KRB_FLAGS_MAX + 1) {
073: bits = new BitArray(Arrays.copyOf(bits.toBooleanArray(),
074: Krb5.KRB_FLAGS_MAX + 1));
075: }
076: }
077:
078: public KerberosFlags(boolean[] bools) {
079: bits = new BitArray(
080: (bools.length == Krb5.KRB_FLAGS_MAX + 1) ? bools
081: : Arrays.copyOf(bools, Krb5.KRB_FLAGS_MAX + 1));
082: }
083:
084: public void set(int index, boolean value) {
085: bits.set(index, value);
086: }
087:
088: public boolean get(int index) {
089: return bits.get(index);
090: }
091:
092: public boolean[] toBooleanArray() {
093: return bits.toBooleanArray();
094: }
095:
096: /**
097: * Writes the encoded data.
098: *
099: * @exception IOException if an I/O error occurs while reading encoded data.
100: * @return an byte array of encoded KDCOptions.
101: */
102: public byte[] asn1Encode() throws IOException {
103: DerOutputStream out = new DerOutputStream();
104: out.putUnalignedBitString(bits);
105: return out.toByteArray();
106: }
107:
108: public String toString() {
109: return bits.toString();
110: }
111: }
|