Last active
August 29, 2015 14:22
-
-
Save eurycea/130e76e421d58066eb64 to your computer and use it in GitHub Desktop.
Playing with FileInputStream and crc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.commons.codec.binary.Hex; | |
import sun.misc.CRC16; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.net.URL; | |
import java.util.zip.CRC32; | |
import java.util.zip.Checksum; | |
public class BlockBreaker { | |
public static int BLOCK_SIZE = 4; | |
FileInputStream fileInputStream; | |
URL resourceUrl; | |
Long fileSize; | |
public BlockBreaker(String resource) { | |
ClassLoader classLoader = this.getClass().getClassLoader(); | |
this.resourceUrl = classLoader.getResource(resource); | |
} | |
public void initialize() throws BlockBreakerException { | |
try { | |
File file = new File(resourceUrl.toURI()); | |
fileSize = file.length(); | |
this.fileInputStream = new FileInputStream(file); | |
} catch (Exception e) { | |
throw new BlockBreakerException(e); | |
} | |
} | |
public void breakIt() throws BlockBreakerException { | |
initialize(); | |
byte[] bytes = new byte[BLOCK_SIZE]; | |
System.out.println("Reading.. "); | |
try { | |
int printCount = 0; | |
while (fileInputStream.available() >= BLOCK_SIZE) { | |
fileInputStream.read(bytes); | |
System.out.print(Hex.encodeHex(bytes)); | |
System.out.print(" "); | |
if (++printCount > 10) { | |
System.out.println(); | |
printCount = 0; | |
} | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public Block getBlock(long blockNumber) throws BlockBreakerException { | |
initialize(); | |
byte[] block = new byte[BLOCK_SIZE]; | |
try { | |
fileInputStream.skip(BLOCK_SIZE * blockNumber); | |
fileInputStream.read(block); | |
return new Block(block); | |
} catch (IOException e) { | |
throw new BlockBreakerException(e); | |
} | |
} | |
public Block timedGetBlock(long blockNumber) throws BlockBreakerException { | |
long startTime = System.currentTimeMillis(); | |
Block block = getBlock(blockNumber); | |
long runTime = System.currentTimeMillis() - startTime; | |
System.out.println(); | |
System.out.println("getBlock " + blockNumber + " took " + runTime + "ms"); | |
return block; | |
} | |
public long getBlockCount() throws BlockBreakerException { | |
if (fileSize == null) { | |
throw new BlockBreakerException(new NullPointerException("File size not initialized")); | |
} | |
long blockCount = fileSize / BLOCK_SIZE; | |
System.out.println(); | |
System.out.println("blockCount: " + blockCount); | |
return blockCount; | |
} | |
public class BlockBreakerException extends Exception { | |
public BlockBreakerException(Exception e) { | |
super(e); | |
} | |
} | |
public class Block { | |
byte[] data; | |
long crc32; | |
int crc16; | |
public Block(byte[] data) { | |
this.data = data; | |
Checksum checksum = new CRC32(); | |
checksum.update(data, 0, data.length); | |
crc32 = checksum.getValue(); | |
CRC16 crc16 = new CRC16(); | |
for (byte d : data) { | |
crc16.update(d); | |
} | |
this.crc16 = crc16.value; | |
} | |
public byte[] getData() { | |
return data; | |
} | |
public long getCrc32() { | |
return crc32; | |
} | |
@Override | |
public String toString() { | |
return "Block{" + | |
"length=" + data.length + | |
", crc32=" + crc32 + | |
", crc16=" + crc16 + | |
'}'; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
dead_beef = [0xDE, 0xAD, 0xBE, 0xEF] | |
food_food = [0xF0, 0x0D, 0xF0, 0x0D] | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='deadbeef_gen') | |
parser.add_argument("size", help="size in kb of file to be generated") | |
args = parser.parse_args() | |
with open("dummy.hex", "wb") as f: | |
data = [] | |
print("Generating...") | |
for i in range(int(args.size)): | |
for x in range(1024 // 4): | |
if x % 2 == 0: | |
data = data + dead_beef | |
else: | |
data = data + food_food | |
f.write(bytearray(data)) | |
print("Complete") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment