1 5 package org.h2.jdbc; 6 7 import java.io.ByteArrayOutputStream ; 8 import java.io.InputStream ; 9 import java.io.OutputStream ; 10 import java.sql.Blob ; 11 import java.sql.SQLException ; 12 13 import org.h2.engine.Constants; 14 import org.h2.engine.SessionInterface; 15 import org.h2.message.Message; 16 import org.h2.message.TraceObject; 17 import org.h2.util.IOUtils; 18 import org.h2.value.Value; 19 20 23 public class JdbcBlob extends TraceObject implements Blob { 24 25 private Value value; 26 private JdbcConnection conn; 27 28 31 public JdbcBlob(SessionInterface session, JdbcConnection conn, Value value, int id) { 32 setTrace(session.getTrace(), TraceObject.BLOB, id); 33 this.conn = conn; 34 this.value = value; 35 } 36 37 42 public long length() throws SQLException { 43 try { 44 debugCodeCall("length"); 45 checkClosed(); 46 if(value.getType() == Value.BLOB) { 47 long precision = value.getPrecision(); 48 if(precision > 0) { 49 return precision; 50 } 51 } 52 long size = 0; 53 InputStream in = value.getInputStream(); 54 try { 55 byte[] buff = new byte[Constants.FILE_BLOCK_SIZE]; 56 while(true) { 57 int len = in.read(buff, 0, Constants.FILE_BLOCK_SIZE); 58 if(len <= 0) { 59 break; 60 } 61 size += len; 62 } 63 } finally { 64 in.close(); 65 } 66 return size; 67 } catch(Throwable e) { 68 throw Message.convert(e); 69 } 70 } 71 72 77 public void truncate(long len) throws SQLException { 78 debugCodeCall("truncate", len); 79 throw Message.getUnsupportedException(); 80 } 81 82 89 public byte[] getBytes(long pos, int length) throws SQLException { 90 try { 91 debugCode("getBytes("+pos+", "+length+");"); 92 checkClosed(); 93 ByteArrayOutputStream out = new ByteArrayOutputStream (); 94 InputStream in = value.getInputStream(); 95 try { 96 IOUtils.skipFully(in, pos - 1); 97 while(length > 0) { 98 int x = in.read(); 99 if(x<0) { 100 break; 101 } 102 out.write(x); 103 length--; 104 } 105 } finally { 106 in.close(); 107 } 108 return out.toByteArray(); 109 } catch(Throwable e) { 110 throw Message.convert(e); 111 } 112 } 113 114 119 public int setBytes(long pos, byte[] bytes) throws SQLException { 120 debugCode("setBytes("+pos+", bytes);"); 121 throw Message.getUnsupportedException(); 122 } 123 124 129 public int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException { 130 debugCode("setBytes("+pos+", bytes, "+offset+", "+len+");"); 131 throw Message.getUnsupportedException(); 132 } 133 134 139 public InputStream getBinaryStream() throws SQLException { 140 debugCodeCall("getBinaryStream"); 141 return value.getInputStream(); 142 } 143 144 149 public OutputStream setBinaryStream(long pos) throws SQLException { 150 debugCodeCall("setBinaryStream", pos); 151 throw Message.getUnsupportedException(); 152 } 153 154 159 public long position(byte[] pattern, long start) throws SQLException { 160 debugCode("position(pattern, "+start+");"); 161 throw Message.getUnsupportedException(); 162 } 206 207 212 public long position(Blob blobPattern, long start) throws SQLException { 213 debugCode("position(blobPattern, "+start+");"); 214 throw Message.getUnsupportedException(); 215 216 } 239 240 243 public void free() throws SQLException { 244 debugCodeCall("free"); 245 value = null; 246 } 247 248 253 public InputStream getBinaryStream(long pos, long length) throws SQLException { 254 debugCode("getBinaryStream("+pos+", "+length+");"); 255 throw Message.getUnsupportedException(); 256 } 257 258 private void checkClosed() throws SQLException { 259 conn.checkClosed(); 260 if (value == null) { 261 throw Message.getSQLException(Message.OBJECT_CLOSED); 262 } 263 } 264 265 } 266 | Popular Tags |