java - Arraylist of Arrays, How to make last array size somehow different from other arrays in the list -
explanation of trying acheive:
i given file , have read data file , create blocks of sizes 1 kb. example : if file size 5.8 kb have 5 blocks of 1 kb each , 1 last block of 0.8 kb. after having these in block have sha 256 encoding last block , append second last block, after have apply encoding second last block , append third last , on.
problem
if given multiple of 1024 byte file size code works well. if last block size not 1024 code doesnot work intended.
the way doing :
bufferedinputstream bis = new bufferedinputstream(new fileinputstream(f)); int sizeofblock = 1024; int sizeofhash = 256; messagedigest md; md = messagedigest.getinstance("sha-256"); byte[] block = new byte[sizeofblock]; list <byte []> blocklist = new arraylist <byte []>(); int tmp = 0; while ((tmp = bis.read(block)) > 0) { system.out.println(tmp); blocklist.add(block); } (int j = blocklist.size()-1; j > 0;){ system.out.println(blocklist.get(j).length); // first iteration shouldnt 1024 if file size not multiple of 1024 md.update(blocklist.get(j--)); byte[] hash = md.digest(); byte[] appendblock = new byte[blocklist.get(j).length + hash.length]; system.arraycopy(blocklist.get(j), 0, appendblock, 0, blocklist.get(j).length); system.arraycopy(md.digest(), 0, appendblock, blocklist.get(j).length, hash.length); blocklist.set(j, appendblock); } system.out.println(blocklist.get(0).length); md.update(blocklist.get(0)); byte[] hash = md.digest(); string result = bytestohex(hash); // converting function byte hex system.out.println(result);
it seems adding same 1024 byte array array list again , again. of course of arrays 1024 bytes. same array. in addition, overwriting block
array every time call read, contain last block read file. should store copy of array in array list.
you should :
while ((tmp = bis.read(block)) > 0) { byte[] currentblock = new byte[tmp]; system.arraycopy (block, 0, currentblock, 0, tmp); system.out.println(tmp); blocklist.add(currentblock); }
Comments
Post a Comment