| CODENOTIFIER | HelpYou are not signed inSign in |
Project: Flanderra
Revision: 20
Author: pcherkas
Date: 09 Aug 2008 18:59:27
Changes:bit logic refactoring and optimization. Initial commit.
Files:| ... | ...@@ -12,6 +12,7 @@ | |
| 12 | 12 | import org.junit.Test; |
| 13 | 13 | |
| 14 | 14 | import com.flanderra.commons.utils.BitUtils; |
| 15 | import com.flanderra.commons.utils.Bits; | |
| 15 | 16 | |
| 16 | 17 | import junit.framework.TestCase; |
| 17 | 18 | |
| ... | ...@@ -30,9 +31,20 @@ | |
| 30 | 31 | String reverse2 = BitUtils._hex(res2); |
| 31 | 32 | |
| 32 | 33 | Assert.assertArrayEquals(res1, res2); |
| 33 | Assert.assertEquals(reverse1, reverse2); | |
| 34 | Assert.assertEquals(reverse1.trim(), reverse2.trim()); | |
| 34 | 35 | |
| 36 | reverse1 = BitUtils.bin(res1); | |
| 37 | reverse2 = BitUtils._bin(res2); | |
| 38 | Assert.assertEquals(reverse1.trim(), reverse2.trim()); | |
| 35 | 39 | |
| 40 | BitSet bits1 = BitUtils.bytesToBits(res1); | |
| 41 | int offset = 36; | |
| 42 | Bits bits2 = new Bits(); | |
| 43 | bits2.setData(res2); | |
| 44 | bits2.setOffset(offset); | |
| 45 | reverse1 = BitUtils.bin(bits1, offset); | |
| 46 | reverse2 = BitUtils._bin(bits2); | |
| 47 | Assert.assertEquals(reverse1.trim(), reverse2.trim()); | |
| 36 | 48 | } |
| 37 | 49 | |
| 38 | 50 | public String bin(byte[] bytes) { |
| ... | ...@@ -589,11 +589,85 @@ | |
| 589 | 589 | } |
| 590 | 590 | return sb.toString(); |
| 591 | 591 | } |
| 592 | public static String _bin(Bits data) { | |
| 593 | if (data == null || data.getData() == null || data.getData().length == 0 || data.getOffset() == 0) { | |
| 594 | return ""; | |
| 595 | } else if(data.getOffset()%8 == 0) { | |
| 596 | return _bin(data.getData()); | |
| 597 | } else { | |
| 598 | int len = data.getOffset() + (data.getOffset()/8); | |
| 599 | char[] result = new char[len]; | |
| 600 | int ind = 0; | |
| 601 | for (int i = 0; i < data.getData().length; i++) { | |
| 602 | if (ind <= (len-1)) { | |
| 603 | result[ind++] = (data.getData()[i] & 0x80) == 0 ? '0' : '1'; | |
| 604 | } else { | |
| 605 | break; | |
| 606 | } | |
| 607 | if (ind <= (len-1)) { | |
| 608 | result[ind++] = (data.getData()[i] & 0x40) == 0 ? '0' : '1'; | |
| 609 | } else { | |
| 610 | break; | |
| 611 | } | |
| 612 | if (ind <= (len-1)) { | |
| 613 | result[ind++] = (data.getData()[i] & 0x20) == 0 ? '0' : '1'; | |
| 614 | } else { | |
| 615 | break; | |
| 616 | } | |
| 617 | if (ind <= (len-1)) { | |
| 618 | result[ind++] = (data.getData()[i] & 0x10) == 0 ? '0' : '1'; | |
| 619 | } else { | |
| 620 | break; | |
| 621 | } | |
| 622 | if (ind <= (len-1)) { | |
| 623 | result[ind++] = (data.getData()[i] & 0x08) == 0 ? '0' : '1'; | |
| 624 | } else { | |
| 625 | break; | |
| 626 | } | |
| 627 | if (ind <= (len-1)) { | |
| 628 | result[ind++] = (data.getData()[i] & 0x04) == 0 ? '0' : '1'; | |
| 629 | } else { | |
| 630 | break; | |
| 631 | } | |
| 632 | if (ind <= (len-1)) { | |
| 633 | result[ind++] = (data.getData()[i] & 0x02) == 0 ? '0' : '1'; | |
| 634 | } else { | |
| 635 | break; | |
| 636 | } | |
| 637 | if (ind <= (len-1)) { | |
| 638 | result[ind++] = (data.getData()[i] & 0x01) == 0 ? '0' : '1'; | |
| 639 | result[ind++] = ' '; | |
| 640 | } | |
| 641 | } | |
| 642 | return new String(result); | |
| 643 | } | |
| 644 | } | |
| 592 | 645 | |
| 593 | 646 | public static String bin(byte[] data) { |
| 594 | 647 | return bin(bytesToBits(data), 8 * data.length); |
| 595 | 648 | } |
| 596 | 649 | |
| 650 | public static String _bin(byte[] data) { | |
| 651 | if (data == null || data.length == 0) { | |
| 652 | return ""; | |
| 653 | } else { | |
| 654 | char[] result = new char[data.length * 9]; | |
| 655 | int ind = 0; | |
| 656 | for (int i = 0; i < data.length; i++) { | |
| 657 | result[ind++] = (data[i] & 0x80) == 0 ? '0' : '1'; | |
| 658 | result[ind++] = (data[i] & 0x40) == 0 ? '0' : '1'; | |
| 659 | result[ind++] = (data[i] & 0x20) == 0 ? '0' : '1'; | |
| 660 | result[ind++] = (data[i] & 0x10) == 0 ? '0' : '1'; | |
| 661 | result[ind++] = (data[i] & 0x08) == 0 ? '0' : '1'; | |
| 662 | result[ind++] = (data[i] & 0x04) == 0 ? '0' : '1'; | |
| 663 | result[ind++] = (data[i] & 0x02) == 0 ? '0' : '1'; | |
| 664 | result[ind++] = (data[i] & 0x01) == 0 ? '0' : '1'; | |
| 665 | result[ind++] = ' '; | |
| 666 | } | |
| 667 | return new String(result); | |
| 668 | } | |
| 669 | } | |
| 670 | ||
| 597 | 671 | public static String hex(BitSet bitSet, int bitNumber) { |
| 598 | 672 | StringBuffer sb = new StringBuffer(""); |
| 599 | 673 | int byteNumber = ((bitNumber % 8) == 0) ? (bitNumber / 8) : (bitNumber / 8 + 1); |
| ... | ...@@ -614,6 +688,11 @@ | |
| 614 | 688 | return sb.toString(); |
| 615 | 689 | } |
| 616 | 690 | |
| 691 | public static String _hex(BitSet bitSet, int bitNumber) { | |
| 692 | StringBuffer sb = new StringBuffer(""); | |
| 693 | return sb.toString(); | |
| 694 | } | |
| 695 | ||
| 617 | 696 | public static String hex(byte[] data) { |
| 618 | 697 | int byteNumber = data.length; |
| 619 | 698 | StringBuffer sb = new StringBuffer(""); |
| ... | ...@@ -634,9 +713,9 @@ | |
| 634 | 713 | } |
| 635 | 714 | return sb.toString(); |
| 636 | 715 | } |
| 637 | ||
| 716 | ||
| 638 | 717 | public static String _hex(byte[] data) { |
| 639 | if(data == null || data.length == 0){ | |
| 718 | if (data == null || data.length == 0) { | |
| 640 | 719 | return ""; |
| 641 | 720 | } else { |
| 642 | 721 | char[] result = new char[data.length * 3]; |
| ... | ...@@ -678,12 +757,12 @@ | |
| 678 | 757 | if (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9' |
| 679 | 758 | || c == 'a' || c == 'b' || c == 'c' || c == 'd' || c == 'e' || c == 'f' || c == 'A' || c == 'B' || c == 'C' || c == 'D' |
| 680 | 759 | || c == 'E' || c == 'F') { |
| 681 | ||
| 760 | ||
| 682 | 761 | if (isWord) { |
| 683 | b = (byte) (((c == '0') ? 0 : (c == '1') ? 1 : (c == '2') ? 2 : (c == '3') ? 3 : (c == '4') ? 4 : (c == '5') ? 5 | |
| 762 | b = (byte) (((c == '0') ? 0 : (c == '1') ? 1 : (c == '2') ? 2 : (c == '3') ? 3 : (c == '4') ? 4 : (c == '5') ? 5 | |
| 684 | 763 | : (c == '6') ? 6 : (c == '7') ? 7 : (c == '8') ? 8 : (c == '9') ? 9 : (c == 'a' || c == 'A') ? 10 |
| 685 | 764 | : (c == 'b' || c == 'B') ? 11 : (c == 'c' || c == 'C') ? 12 : (c == 'd' || c == 'D') ? 13 |
| 686 | : (c == 'e' || c == 'E') ? 14 : 15) << 4) ; | |
| 765 | : (c == 'e' || c == 'E') ? 14 : 15) << 4); | |
| 687 | 766 | } else { |
| 688 | 767 | b += (byte) ((c == '0') ? 0 : (c == '1') ? 1 : (c == '2') ? 2 : (c == '3') ? 3 : (c == '4') ? 4 : (c == '5') ? 5 |
| 689 | 768 | : (c == '6') ? 6 : (c == '7') ? 7 : (c == '8') ? 8 : (c == '9') ? 9 : (c == 'a' || c == 'A') ? 10 |