From: <bra...@us...> - 2010-05-18 22:56:29
|
Revision: 3106 http://archive-access.svn.sourceforge.net/archive-access/?rev=3106&view=rev Author: bradtofel Date: 2010-05-18 22:56:23 +0000 (Tue, 18 May 2010) Log Message: ----------- INITIAL REV: simple class for manipulating bits within a byte array. Added Paths: ----------- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/BitArray.java trunk/archive-access/projects/wayback/wayback-core/src/test/java/org/archive/wayback/util/BitArrayTest.java Added: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/BitArray.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/BitArray.java (rev 0) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/BitArray.java 2010-05-18 22:56:23 UTC (rev 3106) @@ -0,0 +1,109 @@ +/* BitArray + * + * $Id$: + * + * Created on Apr 27, 2010. + * + * Copyright (C) 2006 Internet Archive. + * + * This file is part of Wayback. + * + * Wayback is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * any later version. + * + * Wayback is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser Public License for more details. + * + * You should have received a copy of the GNU Lesser Public License + * along with Wayback; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +package org.archive.wayback.util; + +/** + * @author brad + * + */ +public class BitArray { + private static final int MASKS[] = { + 0x01, + 0x02, + 0x04, + 0x08, + 0x10, + 0x20, + 0x40, + 0x80 + }; + private static final int MASKSR[] = { + ~0x01, + ~0x02, + ~0x04, + ~0x08, + ~0x10, + ~0x20, + ~0x40, + ~0x80 + }; + private byte array[] = null; + + /** + * Construct a new BitArray holding at least n bits + * @param n number of bits to hold + */ + public BitArray(int n) { + int bytes = n / 8; + int bits = n % 8; + if(bits > 0) { + bytes++; + } + this.array = new byte[bytes]; + } + /** + * Construct a new BitArray using argument as initial values. + * @param array byte array of initial values + */ + public BitArray(byte array[]) { + this.array = array; + } + /** + * @return the byte array backing this bit array. + */ + public byte[] getBytes() { + return array; + } + /** + * @param i index of bit to test + * @return true if the i'th bit is set, false otherwise + */ + public boolean get(int i) { + int idx = i / 8; + if(idx >= array.length) { + throw new IndexOutOfBoundsException(); + } + int bit = 7 - (i % 8); + return ((array[idx] & MASKS[bit]) == MASKS[bit]); + } + /** + * set the i'th bit to 1 or 0 + * @param i bit number to set + * @param value if true, the bit is set to 1, otherwise it is set to 0 + */ + public void set(int i, boolean value) { + int idx = i / 8; + if(idx >= array.length) { + throw new IndexOutOfBoundsException(); + } + int bit = 7 - (i % 8); + if(value) { + array[idx] = (byte) (array[idx] | MASKS[bit]); + } else { + array[idx] = (byte) (array[idx] & MASKSR[bit]); + } + } +} Property changes on: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/BitArray.java ___________________________________________________________________ Added: svn:keywords + Author Date Revision Id Added: trunk/archive-access/projects/wayback/wayback-core/src/test/java/org/archive/wayback/util/BitArrayTest.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/test/java/org/archive/wayback/util/BitArrayTest.java (rev 0) +++ trunk/archive-access/projects/wayback/wayback-core/src/test/java/org/archive/wayback/util/BitArrayTest.java 2010-05-18 22:56:23 UTC (rev 3106) @@ -0,0 +1,84 @@ +/* BitArrayTest + * + * $Id$: + * + * Created on May 14, 2010. + * + * Copyright (C) 2006 Internet Archive. + * + * This file is part of Wayback. + * + * Wayback is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * any later version. + * + * Wayback is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser Public License for more details. + * + * You should have received a copy of the GNU Lesser Public License + * along with Wayback; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +package org.archive.wayback.util; + +import junit.framework.TestCase; + +/** + * @author brad + * + */ +public class BitArrayTest extends TestCase { + + /** + * Test method for {@link org.archive.wayback.util.BitArray#get(int)}. + */ + public void testGet() { + byte bytes[] = "Here is some data!".getBytes(); + byte bytes2[] = "Here is some data!".getBytes(); + int bits = bytes.length * 8; + sun.security.util.BitArray sba = + new sun.security.util.BitArray(bits, bytes); + org.archive.wayback.util.BitArray wba = + new org.archive.wayback.util.BitArray(bytes2); + for(int i = 0; i < bits; i++) { + boolean want = sba.get(i); + boolean got = wba.get(i); + if(want != got) { + got = wba.get(i); + } + assertEquals(want,got); + } + } + + /** + * Test method for {@link org.archive.wayback.util.BitArray#set(int, boolean)}. + */ + public void testSet() { + byte bytes[] = "Here is some data!".getBytes(); + byte bytes2[] = "Here is some data!".getBytes(); + int bits = bytes.length * 8; + sun.security.util.BitArray sba = + new sun.security.util.BitArray(bits, bytes); + org.archive.wayback.util.BitArray wba = + new org.archive.wayback.util.BitArray(bytes2); + for(int i = 0; i < bits; i++) { + boolean want = sba.get(i); + boolean got = wba.get(i); + boolean not = !want; + assertTrue(ByteOp.cmp(sba.toByteArray(), wba.getBytes())); + sba.set(i,not); + wba.set(i,not); + assertTrue(ByteOp.cmp(sba.toByteArray(), wba.getBytes())); + assertEquals(not,wba.get(i)); + sba.set(i,got); + wba.set(i,got); + assertEquals(sba.get(i),wba.get(i)); + assertTrue(ByteOp.cmp(sba.toByteArray(), wba.getBytes())); + } + + } +} Property changes on: trunk/archive-access/projects/wayback/wayback-core/src/test/java/org/archive/wayback/util/BitArrayTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Revision Id This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |