01: /*
02:
03: This software is OSI Certified Open Source Software.
04: OSI Certified is a certification mark of the Open Source Initiative.
05:
06: The license (Mozilla version 1.0) can be read at the MMBase site.
07: See http://www.MMBase.org/license
08:
09: */
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: /**
17: * The datatype associated with byte arrays ('blobs').
18: *
19: * @author Pierre van Rooden
20: * @version $Id: BinaryDataType.java,v 1.14 2007/09/16 17:55:28 michiel Exp $
21: * @since MMBase-1.8
22: */
23: public class BinaryDataType extends AbstractLengthDataType<byte[]> {
24:
25: private static final Logger log = Logging
26: .getLoggerInstance(BinaryDataType.class);
27:
28: private static final long serialVersionUID = 1L; // increase this if object serialization changes (which we shouldn't do!)
29:
30: protected Pattern validMimeTypes = Pattern.compile(".*");
31:
32: /**
33: * Constructor for binary field.
34: * @param name the name of the data type
35: */
36: public BinaryDataType(String name) {
37: super (name, byte[].class);
38: }
39:
40: protected void inheritProperties(BasicDataType origin) {
41: super .inheritProperties(origin);
42: if (origin instanceof BinaryDataType) {
43: validMimeTypes = ((BinaryDataType) origin).validMimeTypes;
44: }
45: }
46:
47: //
48: public long getLength(Object value) {
49: if (value == null)
50: return 0;
51: if (value instanceof byte[]) {
52: byte[] bytes = (byte[]) value;
53: if (log.isDebugEnabled()) {
54: StringBuilder buf = new StringBuilder("[");
55: for (int i = 0; i < bytes.length; i++) {
56: buf.append((char) bytes[i]);
57: if (i + 1 < bytes.length) {
58: buf.append(", ");
59: }
60: }
61: buf.append("]");
62: log.debug("Getting length of " + buf);
63: }
64: return bytes.length;
65: } else if (value instanceof FileItem) {
66: FileItem fi = (FileItem) value;
67: return fi.getSize();
68: } else {
69: throw new RuntimeException("Value "
70: + value
71: + " of "
72: + getName()
73: + " is not a byte array but"
74: + (value == null ? "null" : value.getClass()
75: .getName()));
76: }
77: }
78:
79: /**
80: * Returns a regular expression which describes wich mime-types are valid for blobs with this
81: * DataType. This is not yet available as a Restriction, only as a property.
82: */
83: public Pattern getValidMimeTypes() {
84: return validMimeTypes;
85: }
86:
87: public void setValidMimeTypes(Pattern pattern) {
88: validMimeTypes = pattern;
89: }
90:
91: }
|