Thread: My refactored Archive class

Results 1 to 6 of 6
  1. #1 My refactored Archive class 
    Renown Programmer
    veer's Avatar
    Join Date
    Nov 2007
    Posts
    3,747
    Thanks given
    354
    Thanks received
    1,368
    Rep Power
    3032
    Code:
    final class Archive {
    
        public Archive(byte abyte0[]) {
            Stream stream = new Stream(abyte0);
            int decompressedSize = stream.get24BitInt();
            int compressedSize = stream.get24BitInt();
            if (compressedSize != decompressedSize) {
                byte abyte1[] = new byte[decompressedSize];
                BZip2Decompressor.decompressBuffer(abyte1, decompressedSize, abyte0, compressedSize, 6);
                finalBuffer = abyte1;
                stream = new Stream(finalBuffer);
                compressedAsWhole = true;
            } else {
                finalBuffer = abyte0;
                compressedAsWhole = false;
            }
            totalFiles = stream.getUShort();
            identifiers = new int[totalFiles];
            decompressedSizes = new int[totalFiles];
            compressedSizes = new int[totalFiles];
            startOffsets = new int[totalFiles];
            int offset = stream.currentOffset + totalFiles * 10; //file info at beginning is 10 bytes per file, and we want to start file data immediately after
            for (int l = 0; l < totalFiles; l++) {
                identifiers[l] = stream.getInt();           //  4
                decompressedSizes[l] = stream.get24BitInt();//  3
                compressedSizes[l] = stream.get24BitInt();  //+ 3
                startOffsets[l] = offset;                   //____
                offset += compressedSizes[l];               // 10
            }
        }
    
        public byte[] getFile(int identifier) {
            byte dataBuffer[] = null; //was a parameter
            for (int k = 0; k < totalFiles; k++)
                if (identifiers[k] == identifier) {
                    if (dataBuffer == null)
                        dataBuffer = new byte[decompressedSizes[k]];
                    if (!compressedAsWhole) {
                        BZip2Decompressor.decompressBuffer(dataBuffer, decompressedSizes[k], finalBuffer, compressedSizes[k], startOffsets[k]);
                    } else {
                        System.arraycopy(finalBuffer, startOffsets[k], dataBuffer, 0, decompressedSizes[k]);
    
                    }
                    return dataBuffer;
                }
    
            return null;
        }
    
        public byte[] getFile(String identStr) {
            byte dataBuffer[] = null; //was a parameter
            int identifier = 0;
            identStr = identStr.toUpperCase();
            for (int j = 0; j < identStr.length(); j++)
                identifier = (identifier * 61 + identStr.charAt(j)) - 32;
            for (int k = 0; k < totalFiles; k++)
                if (identifiers[k] == identifier) {
                    if (dataBuffer == null)
                        dataBuffer = new byte[decompressedSizes[k]];
                    if (!compressedAsWhole) {
                        BZip2Decompressor.decompressBuffer(dataBuffer, decompressedSizes[k], finalBuffer, compressedSizes[k], startOffsets[k]);
                    } else {
                        System.arraycopy(finalBuffer, startOffsets[k], dataBuffer, 0, decompressedSizes[k]);
    
                    }
                    return dataBuffer;
                }
    
            return null;
        }
    
        final byte[] finalBuffer;
        final int totalFiles;
        final int[] identifiers;
        final int[] decompressedSizes;
        final int[] compressedSizes;
        final int[] startOffsets;
        final boolean compressedAsWhole;
    }
    feel like pasting alot of code
     

  2. #2  
    Registered Member

    Join Date
    Dec 2009
    Posts
    1,142
    Thanks given
    275
    Thanks received
    169
    Rep Power
    164
    Can you post your refactored Stream class as well, or at least what these methods are unrefactored: get24BitInt, getUShort, getInt.

    Sorry for bump lol.

    EDIT: nevermind I got them. still would like to see your stream class please.
     

  3. #3  
    Registered Member

    Join Date
    Sep 2007
    Age
    29
    Posts
    2,396
    Thanks given
    5
    Thanks received
    436
    Rep Power
    902
    thanks may help alot in future if i need it
    Hyperion V2 Martin's Updates.
    [Only registered and activated users can see links. ]

    Scar says:
    i hate it when it hits your face
     

  4. #4  
    Community Veteran

    WH:II:DOW's Avatar
    Join Date
    Dec 2007
    Age
    32
    Posts
    2,022
    Thanks given
    145
    Thanks received
    872
    Rep Power
    4275
    Quote Originally Posted by Sadistic View Post
    Can you post your refactored Stream class as well, or at least what these methods are unrefactored: get24BitInt, getUShort, getInt.

    Sorry for bump lol.

    EDIT: nevermind I got them. still would like to see your stream class please.
    "readTriByte", "readUnsignedWord" and "readDWord".

    Edit: Damn you Martin and your immense bump.
     

  5. #5  
    Registered Member

    Join Date
    Oct 2009
    Posts
    1,595
    Thanks given
    100
    Thanks received
    50
    Rep Power
    232
    nice i guess.


    OFFTOPIC: Grats for me 500 post
    [Only registered and activated users can see links. ]
     

  6. #6  
    fumant viriditas quotidiana

    saifix's Avatar
    Join Date
    Feb 2009
    Age
    27
    Posts
    1,239
    Thanks given
    277
    Thanks received
    955
    Discord
    View profile
    Rep Power
    3304
    Code:
        public byte[] getFile(String identStr) {
            int identifier = 0;
            identStr = identStr.toUpperCase();
            for (int j = 0; j < identStr.length(); j++)
                identifier = (identifier * 61 + identStr.charAt(j)) - 32;
            return getFile(identifier);
        }
    "Im so bluezd out of my box.
    Im so fkd i canr even sens makeas Smoke blunt 420hash e tizday" - A legendary guy (1993 - 2015)
    Quote Originally Posted by nmopal View Post
    I will be creating a grimy dubstep song using these lyrics and vocaloid please prepare your bodies
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •