View Javadoc

1   /*
2   Copyright (C) 2007 Dirk Huenniger
3   
4   This library is free software; you can redistribute it and/or
5   modify it under the terms of the GNU Lesser General Public
6   License as published by the Free Software Foundation; either
7   version 2.1 of the License, or (at your option) any later version.
8   
9   This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  Lesser General Public License for more details.
13  
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17   */
18  
19  package org.indi.objects;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.RandomAccessFile;
26  import java.io.UnsupportedEncodingException;
27  
28  import org.indi.server.Base64;
29  
30  /**
31   * a BLOB (binary large object) as transferred by the indi protocol
32   * 
33   * @author Dirk Hünniger
34   * 
35   */
36  public class Blob extends Element {
37      /**
38       * 
39       */
40      private static final long serialVersionUID = 1L;
41      /**
42       * the format of the BLOB usually the extension of a file format
43       */
44      protected String format;
45      /**
46       * the length of the BLOB in bytes
47       */
48      protected long size;
49  
50      /**
51       * class constuctor
52       * 
53       * @param name
54       *                the name of the BLOB
55       * @param label
56       *                the label of the BLOB (to be used for display in the GUI)
57       */
58      public Blob(String name, String label) {
59  	super(name, label, null);
60      }
61  
62      /**
63       * class constructor
64       * 
65       * @param name
66       *                the name of the BLOB
67       * @param format
68       *                the format of the BLOB usually the extension of a file
69       *                format
70       * @param size
71       *                the length of the BLOB in bytes
72       * @param value
73       *                the data carryed by the BLOB
74       */
75      public Blob(String name, String format, long size, String value) {
76  	super(name, null, value);
77  	this.size = size;
78  	this.format = format;
79      }
80  
81      /**
82       * @param type
83       *                the way the blob has benn transferred.
84       * @return the data carryed by the BLOB
85       */
86      @Override
87      public String getValue(TransferType type) {
88  	switch (type) {
89  	case Def:
90  	    return "";
91  	default:
92  	    return this.value;
93  	}
94      }
95  
96      /**
97       * @see org.indi.objects.Element#getStartTag(org.indi.objects.TransferType)
98       */
99      @Override
100     public String getStartTag(TransferType type) {
101 	switch (type) {
102 	case Def:
103 	    return "<def" + getType() + " name=\"" + this.name + "\" label=\""
104 		    + this.label + "\"> ";
105 	default:
106 	    return "<one" + getType() + " name=\"" + this.name + "\" size=\""
107 		    + Long.toString(this.size) + "\" format=\"" + this.format
108 		    + "\"> ";
109 	}
110     }
111 
112     /**
113      * Set the data to be tranferred by the BLOB
114      * 
115      * @param data
116      *                the data to be transferred
117      * @param format
118      *                the format of the data, ususually the extension of a file
119      *                format
120      */
121     public void setValue(String data, String format) {
122 	try {
123 	    this.value = Base64.encodeBytes(data.getBytes("UTF-8"));
124 	} catch (UnsupportedEncodingException e) {
125 	    e.printStackTrace();
126 	}
127 	this.size = data.length();
128     }
129 
130     /**
131      * Set the data to be transferred using a given file as input
132      * 
133      * @param file
134      *                the file to be used as input
135      */
136     public void setValue(File file) {
137 	try {
138 	    RandomAccessFile rfile = new RandomAccessFile(file, "r");
139 	    byte[] data = new byte[(int) file.length()];
140 	    rfile.readFully(data);
141 	    this.value = Base64.encodeBytes(data);
142 	    String name = file.getName();
143 	    String[] spname = name.split("\\.");
144 	    if (spname.length > 1) {
145 		this.format = "." + spname[1];
146 	    } else {
147 		this.format = "";
148 	    }
149 	    this.size = file.length();
150 	} catch (IOException e) {
151 	    throw new RuntimeException(e);
152 	}
153     }
154 
155     public void setValue(InputStream inputStream, String format) {
156 	try {
157 	    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
158 	    byte[] buffer = new byte[256];
159 	    int count = inputStream.read(buffer);
160 	    while (count >= 0) {
161 		bytes.write(buffer, 0, count);
162 		count = inputStream.read(buffer);
163 	    }
164 	    byte[] data = bytes.toByteArray();
165 	    this.value = Base64.encodeBytes(data);
166 	    this.format = format;
167 	    this.size = data.length;
168 	    inputStream.close();
169 	} catch (IOException e) {
170 	    throw new RuntimeException(e);
171 	}
172     }
173 
174     /**
175      * Set the data to be tranferred by the BLOB (format is set to the empty
176      * string)
177      * 
178      * @param data
179      *                the data to be transferred
180      */
181     @Override
182     public void setValue(String data) {
183 	setValue(data, "");
184     }
185 
186     @Override
187     /**
188      * @return the type of the indiobjects.Object (BLOB in this case)
189      */
190     protected String getType() {
191 	// TODO Auto-generated method stub
192 	return "BLOB";
193     }
194 }