1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
32
33
34
35
36 public class Blob extends Element {
37
38
39
40 private static final long serialVersionUID = 1L;
41
42
43
44 protected String format;
45
46
47
48 protected long size;
49
50
51
52
53
54
55
56
57
58 public Blob(String name, String label) {
59 super(name, label, null);
60 }
61
62
63
64
65
66
67
68
69
70
71
72
73
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
83
84
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
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
114
115
116
117
118
119
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
132
133
134
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
176
177
178
179
180
181 @Override
182 public void setValue(String data) {
183 setValue(data, "");
184 }
185
186 @Override
187
188
189
190 protected String getType() {
191
192 return "BLOB";
193 }
194 }