1 24 package javax.jcr; 25 26 import java.io.ByteArrayInputStream ; 27 import java.io.InputStream ; 28 import java.io.UnsupportedEncodingException ; 29 30 45 public abstract class BaseValue implements Value { 46 47 protected static final String DEFAULT_ENCODING = "UTF-8"; 48 49 private static final short STATE_UNDEFINED = 0; 50 private static final short STATE_VALUE_CONSUMED = 1; 51 private static final short STATE_STREAM_CONSUMED = 2; 52 53 private short state = STATE_UNDEFINED; 54 55 protected final int type; 56 57 protected InputStream stream = null; 58 59 64 BaseValue(int type) { 65 this.type = type; 66 } 67 68 78 protected void setStreamConsumed() throws IllegalStateException { 79 if (state == STATE_VALUE_CONSUMED) { 80 throw new IllegalStateException ("non-stream value has already been consumed"); 81 } 82 state = STATE_STREAM_CONSUMED; 83 } 84 85 93 protected void setValueConsumed() throws IllegalStateException { 94 if (state == STATE_STREAM_CONSUMED) { 95 throw new IllegalStateException ("stream value has already been consumed"); 96 } 97 state = STATE_VALUE_CONSUMED; 98 } 99 100 104 public int getType() { 105 return type; 106 } 107 108 111 public InputStream getStream() throws ValueFormatException, RepositoryException { 112 setStreamConsumed(); 113 114 if (stream != null) { 115 return stream; 116 } 117 118 try { 119 stream = new ByteArrayInputStream (getString().getBytes(DEFAULT_ENCODING)); 121 return stream; 122 } catch (UnsupportedEncodingException e) { 123 throw new RepositoryException(DEFAULT_ENCODING + " not supported on this platform", e); 124 } 125 } 126 } 127 | Popular Tags |