KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > jcr > BinaryValue


1 /*
2  * $Id: BinaryValue.java,v 1.2 2004/07/24 00:16:21 benjmestrallet Exp $
3  *
4  * Copyright 2002-2004 Day Management AG, Switzerland.
5  *
6  * Licensed under the Day RI License, Version 2.0 (the "License"),
7  * as a reference implementation of the following specification:
8  *
9  * Content Repository API for Java Technology, revision 0.12
10  * <http://www.jcp.org/en/jsr/detail?id=170>
11  *
12  * You may not use this file except in compliance with the License.
13  * You may obtain a copy of the License files at
14  *
15  * http://www.day.com/content/en/licenses/day-ri-license-2.0
16  * http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  */

24 package javax.jcr;
25
26 import javax.jcr.util.ISO8601;
27 import java.io.*;
28 import java.util.Calendar JavaDoc;
29
30 /**
31  * A <code>BinaryValue</code> provides an implementation
32  * of the <code>Value</code> interface representing a binary value.
33  *
34  * @author Stefan Guggisberg
35  */

36 public class BinaryValue extends BaseValue {
37
38   public static final int TYPE = PropertyType.BINARY;
39
40   // those fields are mutually exclusive, i.e. only one can be non-null
41
private byte[] streamData = null;
42   private String JavaDoc text = null;
43
44   /**
45    * Constructs a <code>BinaryValue</code> object based on a string.
46    *
47    * @param text the string this <code>BinaryValue</code> should represent
48    */

49   public BinaryValue(String JavaDoc text) {
50     super(TYPE);
51     this.text = text;
52   }
53
54   /**
55    * Constructs a <code>BinaryValue</code> object based on a stream.
56    *
57    * @param stream the stream this <code>BinaryValue</code> should represent
58    */

59   public BinaryValue(InputStream stream) {
60     super(TYPE);
61     this.stream = stream;
62   }
63
64   /**
65    * Constructs a <code>BinaryValue</code> object based on a stream.
66    *
67    * @param data the stream this <code>BinaryValue</code> should represent
68    */

69   public BinaryValue(byte[] data) {
70     super(TYPE);
71     streamData = data;
72   }
73
74   /**
75    * Indicates whether some other object is "equal to" this one.
76    * <p/>
77    * The result is <code>true</code> if and only if the argument is not
78    * <code>null</code> and is a <code>BinaryValue</code> object that
79    * represents the same value as this object.
80    *
81    * @param obj the reference object with which to compare.
82    * @return <code>true</code> if this object is the same as the obj
83    * argument; <code>false</code> otherwise.
84    */

85   public boolean equals(Object JavaDoc obj) {
86     if (this == obj) {
87       return true;
88     }
89     if (obj instanceof BinaryValue) {
90       BinaryValue other = (BinaryValue) obj;
91       if (text == other.text && stream == other.stream &&
92           streamData == other.streamData) {
93         return true;
94       }
95       // stream, streamData and text are mutually exclusive,
96
// i.e. only one of them can be non-null
97
if (stream != null) {
98         return stream.equals(other.stream);
99       } else if (streamData != null) {
100         return streamData.equals(other.streamData);
101       } else {
102         return text.equals(other.text);
103       }
104     }
105     return false;
106   }
107
108   //----------------------------------------------------------------< Value >
109
/**
110    * @see Value#getDate
111    */

112   public Calendar JavaDoc getDate() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException {
113     setValueConsumed();
114
115     Calendar JavaDoc cal = ISO8601.parse(getString());
116     if (cal != null) {
117       return cal;
118     } else {
119       throw new ValueFormatException("not a valid date format");
120     }
121   }
122
123   /**
124    * @see Value#getLong
125    */

126   public long getLong() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException {
127     setValueConsumed();
128
129     try {
130       return Long.parseLong(getString());
131     } catch (NumberFormatException JavaDoc e) {
132       throw new ValueFormatException("conversion to long failed", e);
133     }
134   }
135
136   /**
137    * @see Value#getBoolean
138    */

139   public boolean getBoolean() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException {
140     setValueConsumed();
141
142     return Boolean.valueOf(getString()).booleanValue();
143   }
144
145   /**
146    * @see Value#getDouble
147    */

148   public double getDouble() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException {
149     setValueConsumed();
150
151     try {
152       return Double.parseDouble(getString());
153     } catch (NumberFormatException JavaDoc e) {
154       throw new ValueFormatException("conversion to double failed", e);
155     }
156   }
157
158   /**
159    * @see Value#getStream
160    */

161   public InputStream getStream() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException {
162     setStreamConsumed();
163
164     // build stream value if necessary
165
if (streamData != null) {
166       stream = new ByteArrayInputStream(streamData);
167       streamData = null;
168     } else if (text != null) {
169       try {
170         stream = new ByteArrayInputStream(text.getBytes(DEFAULT_ENCODING));
171       } catch (UnsupportedEncodingException e) {
172         throw new RepositoryException(DEFAULT_ENCODING + " not supported on this platform", e);
173       }
174       text = null;
175     }
176
177     return super.getStream();
178   }
179
180   /**
181    * @see Value#getString
182    */

183   public String JavaDoc getString() throws ValueFormatException, IllegalStateException JavaDoc, RepositoryException {
184     setValueConsumed();
185
186     // build text value if necessary
187
if (streamData != null) {
188       try {
189         text = new String JavaDoc(streamData, DEFAULT_ENCODING);
190       } catch (UnsupportedEncodingException e) {
191         throw new RepositoryException(DEFAULT_ENCODING + " not supported on this platform", e);
192       }
193       streamData = null;
194     } else if (stream != null) {
195       try {
196         ByteArrayOutputStream out = new ByteArrayOutputStream();
197         byte[] buffer = new byte[8192];
198         int read;
199         while ((read = stream.read(buffer)) > 0) {
200           out.write(buffer, 0, read);
201         }
202         byte[] data = out.toByteArray();
203         text = new String JavaDoc(data, DEFAULT_ENCODING);
204       } catch (UnsupportedEncodingException e) {
205         throw new RepositoryException(DEFAULT_ENCODING + " not supported on this platform", e);
206       } catch (IOException e) {
207         throw new ValueFormatException("conversion from stream to string failed", e);
208       } finally {
209         try {
210           if (stream != null) {
211             stream.close();
212           }
213         } catch (IOException e) {
214           // ignore
215
}
216       }
217       stream = null;
218     }
219
220     if (text != null) {
221       return text;
222     } else {
223       throw new ValueFormatException("empty value");
224     }
225   }
226 }
227
Popular Tags