1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.modeshape.graph.property.basic;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.Reader;
30 import java.io.UnsupportedEncodingException;
31 import java.math.BigDecimal;
32 import java.net.URI;
33 import java.util.Calendar;
34 import java.util.Date;
35 import java.util.UUID;
36 import net.jcip.annotations.Immutable;
37 import org.modeshape.common.text.TextDecoder;
38 import org.modeshape.common.util.IoUtil;
39 import org.modeshape.graph.GraphI18n;
40 import org.modeshape.graph.property.Binary;
41 import org.modeshape.graph.property.BinaryFactory;
42 import org.modeshape.graph.property.DateTime;
43 import org.modeshape.graph.property.IoException;
44 import org.modeshape.graph.property.Name;
45 import org.modeshape.graph.property.Path;
46 import org.modeshape.graph.property.PropertyType;
47 import org.modeshape.graph.property.Reference;
48 import org.modeshape.graph.property.ValueFactory;
49 import org.modeshape.graph.property.ValueFormatException;
50
51
52
53
54
55 @Immutable
56 public abstract class AbstractBinaryValueFactory extends AbstractValueFactory<Binary> implements BinaryFactory {
57
58 private static final String CHAR_SET_NAME = "UTF-8";
59
60 protected AbstractBinaryValueFactory( TextDecoder decoder,
61 ValueFactory<String> stringValueFactory ) {
62 super(PropertyType.BINARY, decoder, stringValueFactory);
63 }
64
65
66
67
68 public Binary create( String value ) {
69 if (value == null) return null;
70 try {
71 return create(value.getBytes(CHAR_SET_NAME));
72 } catch (UnsupportedEncodingException err) {
73 throw new ValueFormatException(value, getPropertyType(),
74 GraphI18n.errorConvertingType.text(String.class.getSimpleName(),
75 Binary.class.getSimpleName(),
76 value), err);
77 }
78 }
79
80
81
82
83 public Binary create( String value,
84 TextDecoder decoder ) {
85 if (value == null) return null;
86 return create(getDecoder(decoder).decode(value));
87 }
88
89
90
91
92 public Binary create( int value ) {
93
94 return create(this.getStringValueFactory().create(value));
95 }
96
97
98
99
100 public Binary create( long value ) {
101
102 return create(this.getStringValueFactory().create(value));
103 }
104
105
106
107
108 public Binary create( boolean value ) {
109
110 return create(this.getStringValueFactory().create(value));
111 }
112
113
114
115
116 public Binary create( float value ) {
117
118 return create(this.getStringValueFactory().create(value));
119 }
120
121
122
123
124 public Binary create( double value ) {
125
126 return create(this.getStringValueFactory().create(value));
127 }
128
129
130
131
132 public Binary create( BigDecimal value ) {
133
134 return create(this.getStringValueFactory().create(value));
135 }
136
137
138
139
140 public Binary create( Calendar value ) {
141
142 return create(this.getStringValueFactory().create(value));
143 }
144
145
146
147
148 public Binary create( Date value ) {
149
150 return create(this.getStringValueFactory().create(value));
151 }
152
153
154
155
156
157
158 public Binary create( DateTime value ) throws ValueFormatException {
159
160 return create(this.getStringValueFactory().create(value));
161 }
162
163
164
165
166 public Binary create( Name value ) {
167
168 return create(this.getStringValueFactory().create(value));
169 }
170
171
172
173
174 public Binary create( Path value ) {
175
176 return create(this.getStringValueFactory().create(value));
177 }
178
179
180
181
182 public Binary create( Reference value ) {
183
184 return create(this.getStringValueFactory().create(value));
185 }
186
187
188
189
190 public Binary create( URI value ) {
191
192 return create(this.getStringValueFactory().create(value));
193 }
194
195
196
197
198
199
200 public Binary create( UUID value ) {
201
202 return create(this.getStringValueFactory().create(value));
203 }
204
205
206
207
208
209
210 public Binary create( Binary value ) throws ValueFormatException, IoException {
211 return value;
212 }
213
214
215
216
217 public Binary create( InputStream stream,
218 long approximateLength ) throws IoException {
219 if (stream == null) return null;
220 try {
221 byte[] value = IoUtil.readBytes(stream);
222 return create(value);
223 } catch (IOException err) {
224 throw new IoException(GraphI18n.errorConvertingIo.text(InputStream.class.getSimpleName(),
225 Binary.class.getSimpleName()), err);
226 }
227 }
228
229
230
231
232 public Binary create( Reader reader,
233 long approximateLength ) throws IoException {
234 if (reader == null) return null;
235
236 try {
237 String value = IoUtil.read(reader);
238 return create(this.getStringValueFactory().create(value));
239 } catch (IOException err) {
240 throw new IoException(GraphI18n.errorConvertingIo.text(Reader.class.getSimpleName(), Binary.class.getSimpleName()),
241 err);
242 }
243 }
244
245
246
247
248
249
250 public Binary create( File file ) throws ValueFormatException, IoException {
251 if (file == null) return null;
252 if (!file.canRead()) return null;
253 if (!file.isFile()) return null;
254 try {
255 byte[] value = IoUtil.readBytes(file);
256 return create(value);
257 } catch (IOException err) {
258 throw new IoException(GraphI18n.errorConvertingIo.text(file, Binary.class.getSimpleName()), err);
259 }
260 }
261
262
263
264
265
266
267
268
269
270
271 public Binary create( Reader reader,
272 long approximateLength,
273 byte[] secureHash ) throws ValueFormatException, IoException {
274 return create(reader, approximateLength);
275 }
276
277
278
279
280
281
282
283
284
285
286 public Binary create( InputStream stream,
287 long approximateLength,
288 byte[] secureHash ) throws ValueFormatException, IoException {
289 return create(stream, approximateLength);
290 }
291
292
293
294
295
296
297
298
299
300 public Binary find( byte[] secureHash ) {
301 return null;
302 }
303
304
305
306
307 @Override
308 protected Binary[] createEmptyArray( int length ) {
309 return new Binary[length];
310 }
311 }