11package org .sqlite .jdbc4 ;
22
3+ import java .io .ByteArrayInputStream ;
4+ import java .io .ByteArrayOutputStream ;
5+ import java .io .IOException ;
36import java .io .InputStream ;
47import java .io .Reader ;
58import java .sql .NClob ;
@@ -48,14 +51,21 @@ public void setNClob(int parameterIndex, NClob value) throws SQLException {
4851 }
4952
5053 public void setClob (int parameterIndex , Reader reader , long length ) throws SQLException {
51- // TODO Support this
52- throw new SQLFeatureNotSupportedException ();
54+ requireLengthIsPositiveInt (length );
55+ setCharacterStream (parameterIndex , reader , (int ) length );
56+ }
57+
58+ private void requireLengthIsPositiveInt (long length ) throws SQLFeatureNotSupportedException {
59+ if (length > Integer .MAX_VALUE || length < 0 ) {
60+ throw new SQLFeatureNotSupportedException (
61+ "Data must have a length between 0 and Integer.MAX_VALUE" );
62+ }
5363 }
5464
5565 public void setBlob (int parameterIndex , InputStream inputStream , long length )
5666 throws SQLException {
57- // TODO Support this
58- throw new SQLFeatureNotSupportedException ( );
67+ requireLengthIsPositiveInt ( length );
68+ setBinaryStream ( parameterIndex , inputStream , ( int ) length );
5969 }
6070
6171 public void setNClob (int parameterIndex , Reader reader , long length ) throws SQLException {
@@ -69,35 +79,59 @@ public void setSQLXML(int parameterIndex, SQLXML xmlObject) throws SQLException
6979 }
7080
7181 public void setAsciiStream (int parameterIndex , InputStream x , long length ) throws SQLException {
72- // TODO Support this
73- throw new SQLFeatureNotSupportedException ( );
82+ requireLengthIsPositiveInt ( length );
83+ setAsciiStream ( parameterIndex , x , ( int ) length );
7484 }
7585
7686 public void setBinaryStream (int parameterIndex , InputStream x , long length )
7787 throws SQLException {
78- // TODO Support this
79- throw new SQLFeatureNotSupportedException ( );
88+ requireLengthIsPositiveInt ( length );
89+ setBinaryStream ( parameterIndex , x , ( int ) length );
8090 }
8191
8292 public void setCharacterStream (int parameterIndex , Reader reader , long length )
8393 throws SQLException {
84- // TODO Support this
85- throw new SQLFeatureNotSupportedException ( );
94+ requireLengthIsPositiveInt ( length );
95+ setCharacterStream ( parameterIndex , reader , ( int ) length );
8696 }
8797
8898 public void setAsciiStream (int parameterIndex , InputStream x ) throws SQLException {
89- // TODO Support this
90- throw new SQLFeatureNotSupportedException ();
99+ byte [] bytes = readBytes (x );
100+ setAsciiStream (parameterIndex , new ByteArrayInputStream (bytes ), bytes .length );
101+ }
102+
103+ /**
104+ * Reads given number of bytes from an input stream.
105+ *
106+ * @param istream The input stream.
107+ * @param length The number of bytes to read.
108+ * @return byte array.
109+ * @throws SQLException
110+ */
111+ private byte [] readBytes (InputStream istream ) throws SQLException {
112+ ByteArrayOutputStream baos = new ByteArrayOutputStream ();
113+ byte [] bytes = new byte [8192 ];
114+
115+ try {
116+ int bytesRead ;
117+ while ((bytesRead = istream .read (bytes )) > 0 ) {
118+ baos .write (bytes , 0 , bytesRead );
119+ }
120+ return baos .toByteArray ();
121+ } catch (IOException cause ) {
122+ SQLException exception = new SQLException ("Error reading stream" );
123+
124+ exception .initCause (cause );
125+ throw exception ;
126+ }
91127 }
92128
93129 public void setBinaryStream (int parameterIndex , InputStream x ) throws SQLException {
94- // TODO Support this
95- throw new SQLFeatureNotSupportedException ();
130+ setBytes (parameterIndex , readBytes (x ));
96131 }
97132
98133 public void setCharacterStream (int parameterIndex , Reader reader ) throws SQLException {
99- // TODO Support this
100- throw new SQLFeatureNotSupportedException ();
134+ setCharacterStream (parameterIndex , reader , Integer .MAX_VALUE );
101135 }
102136
103137 public void setNCharacterStream (int parameterIndex , Reader value ) throws SQLException {
@@ -106,13 +140,11 @@ public void setNCharacterStream(int parameterIndex, Reader value) throws SQLExce
106140 }
107141
108142 public void setClob (int parameterIndex , Reader reader ) throws SQLException {
109- // TODO Support this
110- throw new SQLFeatureNotSupportedException ();
143+ setCharacterStream (parameterIndex , reader , Integer .MAX_VALUE );
111144 }
112145
113146 public void setBlob (int parameterIndex , InputStream inputStream ) throws SQLException {
114- // TODO Support this
115- throw new SQLFeatureNotSupportedException ();
147+ setBytes (parameterIndex , readBytes (inputStream ));
116148 }
117149
118150 public void setNClob (int parameterIndex , Reader reader ) throws SQLException {
0 commit comments