KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > datatypes > BinaryDataType


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.datatypes;
11
12 import org.mmbase.util.logging.*;
13 import java.util.regex.Pattern JavaDoc;
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.10 2006/07/18 12:58:40 michiel Exp $
21  * @since MMBase-1.8
22  */

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; // increase this if object serialization changes (which we shouldn't do!)
28

29     protected Pattern JavaDoc validMimeTypes = Pattern.compile(".*");
30     /**
31      * Constructor for binary field.
32      * @param name the name of the data type
33      */

34     public BinaryDataType(String JavaDoc 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     //
46
public long getLength(Object JavaDoc value) {
47         if (value instanceof byte[]) {
48             byte[] bytes = (byte[]) value;
49             if (log.isDebugEnabled()) {
50                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc("[");
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 JavaDoc("Value " + value + " of " + getName() + " is not a byte array but" + (value == null ? "null" : value.getClass().getName()));
66         }
67     }
68
69     /**
70      * Returns a regular expression which describes wich mime-types are valid for blobs with this
71      * DataType. This is not yet available as a Restriction, only as a property.
72      */

73     public Pattern JavaDoc getValidMimeTypes() {
74         return validMimeTypes;
75     }
76
77     public void setValidMimeTypes(Pattern JavaDoc pattern) {
78         validMimeTypes = pattern;
79     }
80
81 }
82
Popular Tags