1 11 package org.eclipse.core.runtime.content; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.util.*; 16 import org.eclipse.core.internal.content.ContentMessages; 17 import org.eclipse.core.runtime.*; 18 import org.eclipse.osgi.util.NLS; 19 20 53 public final class BinarySignatureDescriber implements IContentDescriber, IExecutableExtension { 54 private final static String SIGNATURE = "signature"; private final static String OFFSET = "offset"; private static final Object REQUIRED = "required"; private byte[] signature; 58 private int offset; 59 private boolean required = true; 60 61 64 public int describe(InputStream contents, IContentDescription description) throws IOException { 65 byte[] buffer = new byte[signature.length]; 66 int notValid = required ? INVALID : INDETERMINATE; 67 if (contents.skip(offset) < offset) 68 return notValid; 69 if (contents.read(buffer) != buffer.length) 70 return notValid; 71 for (int i = 0; i < signature.length; i++) 72 if (signature[i] != buffer[i]) 73 return notValid; 74 return VALID; 75 } 76 77 80 public QualifiedName[] getSupportedOptions() { 81 return new QualifiedName[0]; 82 } 83 84 87 public void setInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException { 88 try { 89 if (data instanceof String ) 90 signature = parseSignature((String ) data); 91 else if (data instanceof Hashtable) { 92 Hashtable parameters = (Hashtable) data; 93 if (!parameters.containsKey(SIGNATURE)) { 94 String message = NLS.bind(ContentMessages.content_badInitializationData, XMLRootElementContentDescriber.class.getName()); 95 throw new CoreException(new Status(IStatus.ERROR, ContentMessages.OWNER_NAME, 0, message, null)); 96 } 97 signature = parseSignature((String ) parameters.get(SIGNATURE)); 98 if (parameters.containsKey(OFFSET)) 99 offset = Integer.parseInt((String ) parameters.get(OFFSET)); 100 if (parameters.containsKey(REQUIRED)) 101 required = Boolean.valueOf((String ) parameters.get(REQUIRED)).booleanValue(); 102 } 103 } catch (NumberFormatException nfe) { 104 String message = NLS.bind(ContentMessages.content_badInitializationData, BinarySignatureDescriber.class.getName()); 105 throw new CoreException(new Status(IStatus.ERROR, ContentMessages.OWNER_NAME, 0, message, nfe)); 106 } 107 } 108 109 private static byte[] parseSignature(String data) { 110 List bytes = new ArrayList(); 111 StringTokenizer tokenizer = new StringTokenizer(data, " \t\n\r\f,"); while (tokenizer.hasMoreTokens()) 113 bytes.add(new Byte ((byte) Integer.parseInt(tokenizer.nextToken().trim(), 16))); 114 byte[] signature = new byte[bytes.size()]; 115 for (int i = 0; i < signature.length; i++) 116 signature[i] = ((Byte ) bytes.get(i)).byteValue(); 117 return signature; 118 } 119 } 120 | Popular Tags |