01: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
02:
03: This file is part of the db4o open source object database.
04:
05: db4o is free software; you can redistribute it and/or modify it under
06: the terms of version 2 of the GNU General Public License as published
07: by the Free Software Foundation and as clarified by db4objects' GPL
08: interpretation policy, available at
09: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11: Suite 350, San Mateo, CA 94403, USA.
12:
13: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14: WARRANTY; without even the implied warranty of MERCHANTABILITY or
15: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16: for more details.
17:
18: You should have received a copy of the GNU General Public License along
19: with this program; if not, write to the Free Software Foundation, Inc.,
20: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21: package com.db4o.foundation;
22:
23: /**
24: * @exclude
25: */
26: public final class BitMap4 {
27:
28: private final byte[] _bits;
29:
30: public BitMap4(int numBits) {
31: _bits = new byte[byteCount(numBits)];
32: }
33:
34: /** "readFrom buffer" constructor **/
35: public BitMap4(byte[] buffer, int pos, int numBits) {
36: this (numBits);
37: System.arraycopy(buffer, pos, _bits, 0, _bits.length);
38: }
39:
40: public BitMap4(byte singleByte) {
41: _bits = new byte[] { singleByte };
42: }
43:
44: public boolean isTrue(int bit) {
45: return ((_bits[arrayOffset(bit)] >>> byteOffset(bit)) & 1) != 0;
46: }
47:
48: public int marshalledLength() {
49: return _bits.length;
50: }
51:
52: public void setFalse(int bit) {
53: _bits[arrayOffset(bit)] &= (byte) ~bitMask(bit);
54: }
55:
56: public void set(int bit, boolean val) {
57: if (val) {
58: setTrue(bit);
59: } else {
60: setFalse(bit);
61: }
62: }
63:
64: public void setTrue(int bit) {
65: _bits[arrayOffset(bit)] |= bitMask(bit);
66: }
67:
68: public void writeTo(byte[] bytes, int pos) {
69: System.arraycopy(_bits, 0, bytes, pos, _bits.length);
70: }
71:
72: private byte byteOffset(int bit) {
73: return (byte) (bit % 8);
74: }
75:
76: private int arrayOffset(int bit) {
77: return bit / 8;
78: }
79:
80: private byte bitMask(int bit) {
81: return (byte) (1 << byteOffset(bit));
82: }
83:
84: private int byteCount(int numBits) {
85: return (numBits + 7) / 8;
86: }
87:
88: public byte getByte(int index) {
89: return _bits[index];
90: }
91: }
|