1 21 22 package org.apache.derby.client.am; 23 24 import java.io.InputStream ; 25 import java.io.IOException ; 26 import java.util.ArrayList ; 27 28 import org.apache.derby.shared.common.reference.SQLState; 29 30 public abstract class Lob implements UnitOfWorkListener { 31 public static final int STRING = 2; 33 public static final int ASCII_STREAM = 4; 34 public static final int UNICODE_STREAM = 8; 35 public static final int CHARACTER_STREAM = 16; 36 public static final int BINARY_STREAM = 32; 37 public static final int BINARY_STRING = 64; 38 39 protected Agent agent_; 41 42 protected int dataType_ = 0; 45 protected long sqlLength_; protected boolean lengthObtained_; 47 48 protected Lob(Agent agent) { 50 agent_ = agent; 51 lengthObtained_ = false; 52 } 53 54 protected void finalize() throws java.lang.Throwable { 55 super.finalize(); 56 } 57 58 60 62 63 public long sqlLength() throws SqlException { 65 checkForClosedConnection(); 66 67 return sqlLength_; 68 } 69 70 71 73 public void listenToUnitOfWork() { 74 agent_.connection_.CommitAndRollbackListeners_.put(this,null); 75 } 76 77 public void completeLocalCommit(java.util.Iterator listenerIterator) { 78 listenerIterator.remove(); 79 } 80 81 public void completeLocalRollback(java.util.Iterator listenerIterator) { 82 listenerIterator.remove(); 83 } 84 85 87 public Agent getAgent() { 88 return agent_; 89 } 90 91 void checkForClosedConnection() throws SqlException { 92 if (agent_.connection_.isClosedX()) { 93 agent_.checkForDeferredExceptions(); 94 throw new SqlException(agent_.logWriter_, 95 new ClientMessageId(SQLState.LOB_METHOD_ON_CLOSED_CONNECTION)); 96 } else { 97 agent_.checkForDeferredExceptions(); 98 } 99 } 100 101 void completeLocalRollback() { 102 ; 103 } 104 105 void completeLocalCommit() { 106 ; 107 } 108 109 120 protected InputStream materializeStream(InputStream is, String typeDesc) 121 throws SqlException { 122 final int GROWBY = 32 * 1024; ArrayList byteArrays = new ArrayList (); 124 byte[] curBytes = new byte[GROWBY]; 125 int totalLength = 0; 126 int partLength = 0; 127 try { 129 do { 130 partLength = is.read(curBytes, 0, curBytes.length); 131 if (partLength == curBytes.length) { 132 byteArrays.add(curBytes); 133 int newLength = totalLength + GROWBY; 135 if (newLength < 0 || newLength == Integer.MAX_VALUE) { 136 curBytes = new byte[Integer.MAX_VALUE - totalLength]; 137 } else { 138 curBytes = new byte[GROWBY]; 139 } 140 } 141 if (partLength > 0) { 142 totalLength += partLength; 143 } 144 } while (partLength == GROWBY); 145 if (is.read() != -1) { 147 throw new SqlException( 149 null, 150 new ClientMessageId( 151 SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE), 152 typeDesc 153 ); 154 } 155 if (partLength > 0) { 156 byteArrays.add(curBytes); 157 } 158 159 curBytes = null; 161 sqlLength_ = totalLength; 162 lengthObtained_ = true; 163 return new ByteArrayCombinerStream(byteArrays, totalLength); 166 } catch (IOException ioe) { 167 throw new SqlException(null, 168 new ClientMessageId( 169 SQLState.LANG_STREAMING_COLUMN_I_O_EXCEPTION), 170 typeDesc, 171 ioe 172 ); 173 } 174 } 175 } 176 | Popular Tags |