package com.molina.cvmfs.directoryentry;

import com.molina.cvmfs.directoryentry.exception.ChunkFileDoesNotMatch;

/**
 * @author Jose Molina Colmenero
 *         <p/>
 *         Wrapper around file chunks in the CVMFS catalogs
 */
public class Chunk {

    protected int offset;
    protected int size;
    protected String contentHash;
    protected int contentHashType;


    public Chunk(Object[] chunkData, int contentHashType) throws ChunkFileDoesNotMatch {
        if (chunkData.length != 5)
            throw new ChunkFileDoesNotMatch();
        this.offset = (Integer) chunkData[2];
        this.size = (Integer) chunkData[3];
        this.contentHash = new String((byte[]) chunkData[4]);
        this.contentHashType = contentHashType;
    }

    public static String catalogDatabaseFields() {
        return "md5path_1, md5path_2, offset, size, hash";
    }

    public String contentHashString() {
        String suffix = ContentHashTypes.toSuffix(contentHashType);
        StringBuilder sb = new StringBuilder();
        for (byte b : contentHash.getBytes())
            sb.append(String.format("%02X ", b));
        return sb.toString().toLowerCase() + suffix;
    }
}

@@ -108,16 +108,82 @@
 Match;%0A%0A
+import org.sqldroid.SQLDroidBlob;%0A%0Aimport java.sql.SQLException;%0A%0A
 /**%0A * @
@@ -723,93 +723,466 @@
 Hash
- = new String((byte%5B%5D) chunkData%5B4%5D);%0A        this.contentHashType = contentHashType;
+Type = contentHashType;%0A        if (chunkData%5B4%5D instanceof byte%5B%5D) %7B%0A            this.contentHash = new String((byte%5B%5D) chunkData%5B4%5D);%0A        %7D else if (chunkData%5B4%5D instanceof SQLDroidBlob)%7B%0A            SQLDroidBlob blob = (SQLDroidBlob) chunkData%5B4%5D;%0A            try %7B%0A                this.contentHash = new String(blob.getBytes(0, (int) blob.length()));%0A            %7D catch (SQLException e) %7B%0A                e.printStackTrace();%0A            %7D%0A        %7D
 %0A   

Fix chunks when using the SQLDroid library