1 24 package javax.jcr; 25 26 import javax.jcr.util.ISO8601; 27 import java.io.*; 28 import java.util.Calendar ; 29 30 36 public class BinaryValue extends BaseValue { 37 38 public static final int TYPE = PropertyType.BINARY; 39 40 private byte[] streamData = null; 42 private String text = null; 43 44 49 public BinaryValue(String text) { 50 super(TYPE); 51 this.text = text; 52 } 53 54 59 public BinaryValue(InputStream stream) { 60 super(TYPE); 61 this.stream = stream; 62 } 63 64 69 public BinaryValue(byte[] data) { 70 super(TYPE); 71 streamData = data; 72 } 73 74 85 public boolean equals(Object 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 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 112 public Calendar getDate() throws ValueFormatException, IllegalStateException , RepositoryException { 113 setValueConsumed(); 114 115 Calendar 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 126 public long getLong() throws ValueFormatException, IllegalStateException , RepositoryException { 127 setValueConsumed(); 128 129 try { 130 return Long.parseLong(getString()); 131 } catch (NumberFormatException e) { 132 throw new ValueFormatException("conversion to long failed", e); 133 } 134 } 135 136 139 public boolean getBoolean() throws ValueFormatException, IllegalStateException , RepositoryException { 140 setValueConsumed(); 141 142 return Boolean.valueOf(getString()).booleanValue(); 143 } 144 145 148 public double getDouble() throws ValueFormatException, IllegalStateException , RepositoryException { 149 setValueConsumed(); 150 151 try { 152 return Double.parseDouble(getString()); 153 } catch (NumberFormatException e) { 154 throw new ValueFormatException("conversion to double failed", e); 155 } 156 } 157 158 161 public InputStream getStream() throws ValueFormatException, IllegalStateException , RepositoryException { 162 setStreamConsumed(); 163 164 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 183 public String getString() throws ValueFormatException, IllegalStateException , RepositoryException { 184 setValueConsumed(); 185 186 if (streamData != null) { 188 try { 189 text = new String (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 (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 } 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 |