1 11 12 package org.eclipse.osgi.internal.verifier; 13 14 import java.io.*; 15 import java.security.MessageDigest ; 16 17 22 class DigestedInputStream extends FilterInputStream { 23 MessageDigest digest; 24 byte result[]; 25 long remaining; 26 27 36 DigestedInputStream(InputStream in, String digestAlgorithm, byte result[], long size) { 37 super(in); 38 this.remaining = size; 39 this.digest = SignedBundleFile.getMessageDigest(digestAlgorithm); 40 this.result = result; 41 } 42 43 46 public synchronized void mark(int readlimit) { 47 } 49 50 53 public boolean markSupported() { 54 return false; 55 } 56 57 67 public int read() throws IOException { 68 if (remaining <= 0) 69 return -1; 70 int c = super.read(); 71 if (c != -1) { 72 digest.update((byte) c); 73 remaining--; 74 } else { 75 remaining = 0; 77 } 78 if (remaining == 0) 79 verifyDigests(); 80 return c; 81 } 82 83 private void verifyDigests() throws IOException { 84 byte rc[] = digest.digest(); 86 if (!MessageDigest.isEqual(result, rc)) 87 throw new IOException("Corrupted file: the digest is valid for " + digest.getAlgorithm()); } 89 90 100 public int read(byte[] b, int off, int len) throws IOException { 101 if (remaining <= 0) 102 return -1; 103 int rc = super.read(b, off, len); 104 if (rc != -1) { 105 digest.update(b, off, rc); 106 remaining -= rc; 107 } else { 108 remaining = 0; 110 } 111 if (remaining <= 0) 112 verifyDigests(); 113 return rc; 114 } 115 116 122 public synchronized void reset() throws IOException { 123 throw new IOException("Reset not supported"); } 126 127 130 public long skip(long n) throws IOException { 131 byte buffer[] = new byte[4096]; 132 long count = 0; 133 while (n - count > 0) { 134 int rc = (n - count) > buffer.length ? buffer.length : (int) (n - count); 135 rc = read(buffer, 0, rc); 136 if (rc == -1) 137 break; 138 count += rc; 139 n -= rc; 140 } 141 return count; 142 } 143 } | Popular Tags |