/** Caclulates an MD5 hash for each file in the archive. */ public String[] getMD5s() throws FileNotFoundException, NoSuchAlgorithmException, IOException { /** * This could be more efficiently handled during the output phase using a filtering channel, but * would require placeholder values in the archive and some state. This is left for a later * refactoring. */ final ByteBuffer buffer = ByteBuffer.allocate(4096); String[] array = new String[headers.size()]; int x = 0; for (CpioHeader header : headers) { Object object = sources.get(header); String value = ""; if (object instanceof File) { final ReadableChannelWrapper input = new ReadableChannelWrapper(new FileInputStream((File) object).getChannel()); final Key<byte[]> key = input.start("MD5"); while (input.read(buffer) != -1) buffer.rewind(); value = new String(Util.hex(input.finish(key))); input.close(); } else if (object instanceof URL) { final ReadableChannelWrapper input = new ReadableChannelWrapper( Channels.newChannel(((URL) object).openConnection().getInputStream())); final Key<byte[]> key = input.start("MD5"); while (input.read(buffer) != -1) buffer.rewind(); value = new String(Util.hex(input.finish(key))); input.close(); } array[x++] = value; } return array; }
public static Format readFormat(ReadableChannelWrapper in) throws Exception { Format format = new Format(); Key<Integer> lead = in.start(); format.getLead().read(in); LOG.trace("Lead ended at '" + in.finish(lead) + "'."); Key<Integer> signature = in.start(); int count = format.getSignature().read(in); int expected = ByteBuffer.wrap((byte[]) format.getSignature().getEntry(SIGNATURES).getValues(), 8, 4) .getInt() / -16; LOG.trace( "Signature ended at '" + in.finish(signature) + "' and contained '" + count + "' headers (expected '" + expected + "')."); Key<Integer> header = in.start(); count = format.getHeader().read(in); expected = ByteBuffer.wrap((byte[]) format.getHeader().getEntry(HEADERIMMUTABLE).getValues(), 8, 4) .getInt() / -16; LOG.trace( "Header ended at '" + in.finish(header) + " and contained '" + count + "' headers (expected '" + expected + "')."); return format; }