1 10 package org.mmbase.datatypes; 11 12 import org.mmbase.util.logging.*; 13 import java.util.regex.Pattern ; 14 import org.apache.commons.fileupload.FileItem; 15 16 23 public class BinaryDataType extends AbstractLengthDataType { 24 25 private static final Logger log = Logging.getLoggerInstance(BinaryDataType.class); 26 27 private static final long serialVersionUID = 1L; 29 protected Pattern validMimeTypes = Pattern.compile(".*"); 30 34 public BinaryDataType(String name) { 35 super(name, byte[].class); 36 } 37 38 protected void inheritProperties(BasicDataType origin) { 39 super.inheritProperties(origin); 40 if (origin instanceof BinaryDataType) { 41 validMimeTypes = ((BinaryDataType) origin).validMimeTypes; 42 } 43 } 44 45 public long getLength(Object value) { 47 if (value instanceof byte[]) { 48 byte[] bytes = (byte[]) value; 49 if (log.isDebugEnabled()) { 50 StringBuffer buf = new StringBuffer ("["); 51 for (int i = 0 ; i < bytes.length; i++) { 52 buf.append((char) bytes[i]); 53 if (i + 1 < bytes.length) { 54 buf.append(", "); 55 } 56 } 57 buf.append("]"); 58 log.debug("Getting length of " + buf); 59 } 60 return bytes.length; 61 } else if (value instanceof FileItem) { 62 FileItem fi = (FileItem) value; 63 return fi.getSize(); 64 } else { 65 throw new RuntimeException ("Value " + value + " of " + getName() + " is not a byte array but" + (value == null ? "null" : value.getClass().getName())); 66 } 67 } 68 69 73 public Pattern getValidMimeTypes() { 74 return validMimeTypes; 75 } 76 77 public void setValidMimeTypes(Pattern pattern) { 78 validMimeTypes = pattern; 79 } 80 81 } 82 | Popular Tags |