/** * Method uncompress given input ByteList to the output ByteList. * * @param readByteList Input ByteList * @param writeByteList Output ByteList */ public void uncompress(ByteList readByteList, ByteList writeByteList) // throws Exception { ByteData currentByteData = byteDataBinaryTree.getRoot(); long binaryIterator = 1L; for (int i = this.headerLength; i < readByteList.size(); i++) { if (binaryIterator > this.binaryCounter) { break; } byte currentByte = readByteList.get(i); for (int k = 7; k >= 0; k--) { if (binaryIterator > this.binaryCounter) { break; } if ((currentByte & (1 << k)) == 0) { currentByteData = currentByteData.getLeftChild(); } else { currentByteData = currentByteData.getRightChild(); } if (currentByteData.getLeftChild() == null) { writeByteList.add(currentByteData.getNormalChar()); currentByteData = byteDataBinaryTree.getRoot(); } binaryIterator++; } } }
public void binaryTreeGenerationFromTheBinaryCodedCodesWorks() { Assert.Equal(null, this.byteDataBinaryTree.getRoot()); byteData2.setCompressedChar(7L); byteData2.setCompressedLength(3); byteData4.setCompressedChar(0L); byteData4.setCompressedLength(1); byteData3.setCompressedChar(2L); byteData3.setCompressedLength(2); byteData1.setCompressedChar(6L); byteData1.setCompressedLength(3); ByteData[] byteDatas256 = new ByteData[256]; for (int i = 0; i < 256; i++) { byteDatas256[i] = new ByteData((byte)(i - 128)); } byteDatas256[1] = byteData2; byteDatas256[3] = byteData4; byteDatas256[2] = byteData3; byteDatas256[0] = byteData1; this.byteDataBinaryTree.createBinaryTreeFromBinaryCodedCodes(byteDatas256); ByteData root = this.byteDataBinaryTree.getRoot(); Assert.True(root != null); Assert.Equal(byteData4, root.getLeftChild()); Assert.Equal(root, root.getLeftChild().getParent()); Assert.Equal(root, root.getRightChild().getParent()); Assert.Equal(byteData3, root.getRightChild().getLeftChild()); Assert.Equal(byteData1, root.getRightChild().getRightChild().getLeftChild()); Assert.Equal(byteData2, root.getRightChild().getRightChild().getRightChild()); Assert.Equal(root, root.getRightChild().getRightChild().getRightChild().getParent().getParent().getParent()); }
private ByteData createLeafForTheCharacter(long compressed, int k, ByteData current) { if ((compressed & (1L << k)) == 0) { if (current.getLeftChild() == null) { ByteData newChild = new ByteData((byte)0); newChild.setParent(current); current.setLeftChild(newChild); current = newChild; } else { current = current.getLeftChild(); } } else { if (current.getRightChild() == null) { ByteData newChild = new ByteData((byte)0); newChild.setParent(current); current.setRightChild(newChild); current = newChild; } else { current = current.getRightChild(); } } return(current); }
private void saveCode(ByteData current, int level, long code) { current.setCompressedChar(code); current.setCompressedLength(level); level += 1; if (current.getLeftChild() != null) { saveCode(current.getLeftChild(), level, code * 2); } if (current.getRightChild() != null) { saveCode(current.getRightChild(), level, code * 2 + 1); } }
public void compressionCodeGenerationWorks() { this.byteDataBinaryTree.createBinaryTreeFromLinkedList(this.byteDataLinkedList); this.byteDataBinaryTree.saveCodesForTree(); ByteData root = this.byteDataBinaryTree.getRoot(); Assert.Equal(0L, root.getLeftChild().getCompressedChar()); Assert.Equal(1, (int)root.getLeftChild().getCompressedLength()); Assert.Equal(2L, root.getRightChild().getLeftChild().getCompressedChar()); Assert.Equal(2, (int)root.getRightChild().getLeftChild().getCompressedLength()); Assert.Equal(6L, root.getRightChild().getRightChild().getLeftChild().getCompressedChar()); Assert.Equal(3, (int)root.getRightChild().getRightChild().getLeftChild().getCompressedLength()); Assert.Equal(7L, root.getRightChild().getRightChild().getRightChild().getCompressedChar()); Assert.Equal(3, (int)root.getRightChild().getRightChild().getRightChild().getCompressedLength()); }
public void binaryTreeGenerationFromTheLinkedListWorks() { Assert.Equal(null, this.byteDataBinaryTree.getRoot()); this.byteDataBinaryTree.createBinaryTreeFromLinkedList(this.byteDataLinkedList); ByteData root = this.byteDataBinaryTree.getRoot(); Assert.True(root != null); Assert.Equal(10, root.getCount()); Assert.Equal(byteData4, root.getLeftChild()); Assert.Equal(root, root.getLeftChild().getParent()); Assert.Equal(6, root.getRightChild().getCount()); Assert.Equal(root, root.getRightChild().getParent()); Assert.Equal(byteData3, root.getRightChild().getLeftChild()); Assert.Equal(3, root.getRightChild().getRightChild().getCount()); Assert.Equal(byteData1, root.getRightChild().getRightChild().getLeftChild()); Assert.Equal(byteData2, root.getRightChild().getRightChild().getRightChild()); Assert.Equal(root, root.getRightChild().getRightChild().getRightChild().getParent().getParent().getParent()); }