1 19 package com.mysql.jdbc; 20 21 import java.io.ByteArrayInputStream ; 22 import java.io.InputStream ; 23 import java.io.OutputStream ; 24 import java.io.Reader ; 25 import java.io.StringReader ; 26 import java.io.Writer ; 27 28 import java.sql.SQLException ; 29 30 31 37 public class Clob implements java.sql.Clob , OutputStreamWatcher, WriterWatcher { 38 private String charData; 39 40 Clob(String charData) { 41 this.charData = charData; 42 } 43 44 47 public OutputStream setAsciiStream(long indexToWriteAt) 48 throws SQLException { 49 if (indexToWriteAt < 1) { 50 throw new SQLException ("indexToWriteAt must be >= 1", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 51 } 52 53 WatchableOutputStream bytesOut = new WatchableOutputStream(); 54 bytesOut.setWatcher(this); 55 56 if (indexToWriteAt > 0) { 57 bytesOut.write(this.charData.getBytes(), 0, 58 (int) (indexToWriteAt - 1)); 59 } 60 61 return bytesOut; 62 } 63 64 67 public InputStream getAsciiStream() throws SQLException { 68 if (this.charData != null) { 69 return new ByteArrayInputStream (this.charData.getBytes()); 70 } else { 71 return null; 72 } 73 } 74 75 78 public Writer setCharacterStream(long indexToWriteAt) 79 throws SQLException { 80 if (indexToWriteAt < 1) { 81 throw new SQLException ("indexToWriteAt must be >= 1", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 82 } 83 84 WatchableWriter writer = new WatchableWriter(); 85 writer.setWatcher(this); 86 87 91 if (indexToWriteAt > 1) { 92 writer.write(this.charData, 0, (int) (indexToWriteAt - 1)); 93 } 94 95 return writer; 96 } 97 98 101 public Reader getCharacterStream() throws SQLException { 102 if (this.charData != null) { 103 return new StringReader (this.charData); 104 } else { 105 return null; 106 } 107 } 108 109 112 public int setString(long pos, String str) throws SQLException { 113 if (pos < 1) { 114 throw new SQLException ("Starting position can not be < 1", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 115 } 116 117 if (str == null) { 118 throw new SQLException ("String to set can not be NULL", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 119 } 120 121 StringBuffer charBuf = new StringBuffer (this.charData); 122 123 pos--; 124 125 int strLength = str.length(); 126 127 charBuf.replace((int) pos, (int) (pos + strLength), str); 128 129 this.charData = charBuf.toString(); 130 131 return strLength; 132 } 133 134 137 public int setString(long pos, String str, int offset, int len) 138 throws SQLException { 139 if (pos < 1) { 140 throw new SQLException ("Starting position can not be < 1", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 141 } 142 143 if (str == null) { 144 throw new SQLException ("String to set can not be NULL", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 145 } 146 147 StringBuffer charBuf = new StringBuffer (this.charData); 148 149 pos--; 150 151 String replaceString = str.substring(offset, len); 152 153 charBuf.replace((int) pos, (int) (pos + replaceString.length()), 154 replaceString); 155 156 this.charData = charBuf.toString(); 157 158 return len; 159 } 160 161 164 public String getSubString(long startPos, int length) 165 throws SQLException { 166 if (startPos < 1) { 167 throw new SQLException ("CLOB start position can not be < 1", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 168 } 169 170 if (this.charData != null) { 171 if (((startPos - 1) + length) > charData.length()) { 172 throw new SQLException ("CLOB start position + length can not be > length of CLOB", 173 SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 174 } 175 176 return this.charData.substring((int) (startPos - 1), length); 177 } else { 178 return null; 179 } 180 } 181 182 185 public long length() throws SQLException { 186 if (this.charData != null) { 187 return this.charData.length(); 188 } else { 189 return 0; 190 } 191 } 192 193 196 public long position(String stringToFind, long startPos) 197 throws SQLException { 198 if (startPos < 1) { 199 throw new SQLException ("Illegal starting position for search, '" 200 + startPos + "'", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 201 } 202 203 if (this.charData != null) { 204 if ((startPos - 1) > this.charData.length()) { 205 throw new SQLException ("Starting position for search is past end of CLOB", 206 SQLError.SQL_STATE_ILLEGAL_ARGUMENT); 207 } 208 209 int pos = this.charData.indexOf(stringToFind, (int) (startPos - 1)); 210 211 return (pos == -1) ? (-1) : (pos + 1); 212 } else { 213 return -1; 214 } 215 } 216 217 220 public long position(java.sql.Clob arg0, long arg1) 221 throws SQLException { 222 return position(arg0.getSubString(0L, (int) arg0.length()), arg1); 223 } 224 225 228 public void streamClosed(byte[] byteData) { 229 this.charData = StringUtils.toAsciiString(byteData); 230 } 231 232 235 public void truncate(long length) throws SQLException { 236 if (length > this.charData.length()) { 237 throw new SQLException ("Cannot truncate CLOB of length " 238 + this.charData.length() 239 + " to length of " 240 + length 241 + "."); 242 } 243 244 this.charData = this.charData.substring(0, (int) length); 245 } 246 247 250 public void writerClosed(char[] charData) { 251 this.charData = new String (charData); 252 } 253 } 254 | Popular Tags |