Source code: com/k_int/codec/runtime/AsnBitString.java
1 /**
2 *
3 * AsnBitString : A utility class representing an ASN bit string
4 *
5 * @author Ian Ibbotson ( ibbo@k-int.com )
6 * @version $Id: AsnBitString.java,v 1.2 2000/12/23 10:44:33 ianibbo Exp $
7 *
8 * Copyright: Copyright (C) 2000, Knowledge Integration Ltd.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1 of
13 * the license, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite
23 * 330, Boston, MA 02111-1307, USA.
24 *
25 */
26
27 package com.k_int.codec.runtime;
28
29 public class AsnBitString
30 {
31 public int numbytes=0;
32 public int unused_bits = 0;
33 public byte[] value = null;
34
35 public AsnBitString()
36 {
37 // Default to 8 bits of data, we can always grow later...
38 numbytes=1;
39 value = new byte[1];
40 }
41
42 public AsnBitString(byte[] bits, int unused)
43 {
44 value = bits;
45 unused_bits = unused;
46 }
47
48 public AsnBitString(int top_bit_pos)
49 {
50 // Allocate the value buffer based on the top bit position...
51 numbytes = top_bit_pos / 8;
52 numbytes += ( top_bit_pos % 8 > 0 ? 1 : 0 );
53 value = new byte[numbytes];
54 }
55
56 byte[] getValue()
57 {
58 return value;
59 }
60
61 // Set a specific bit position
62 public void setBit(int bitpos)
63 {
64 int num_bytes_needed = (bitpos+1) / 8;
65 num_bytes_needed += ( (bitpos+1) % 8 > 0 ? 1 : 0 );
66
67 // System.err.println("setBit request to set bit at pos "+bitpos);
68 // System.err.println("setBit num_bytes_needed = "+num_bytes_needed+" current = "+numbytes);
69
70 if ( num_bytes_needed > numbytes )
71 {
72 // System.err.println("re-allocating bitstring");
73
74 // We need to re-allocate the buffer
75 byte [] newvalue = new byte[num_bytes_needed];
76 if ( null != value )
77 System.arraycopy(value, 0, newvalue, 0, numbytes);
78
79 numbytes = num_bytes_needed;
80 value = newvalue;
81 }
82
83 // Figure out which octet contains the bit we need to set, and what
84 // position in that octet
85
86 int octet_to_set = bitpos / 8;
87 int bit_to_set = 7 - ( bitpos % 8 );
88 // int bit_to_set = 8 - ( bitpos % 8 );
89
90 value[octet_to_set] |= ( 1 << bit_to_set );
91
92 // System.err.println("After anding bit "+bit_to_set+" of octet "+octet_to_set+" with "+(1 << bit_to_set)+"value = "+value[octet_to_set]);
93 }
94
95 // Check a specific bit position
96 public void isBitSet(int bitpos)
97 {
98
99 }
100 }