1
1
package org .sqlite .jdbc4 ;
2
2
3
+ import java .io .ByteArrayInputStream ;
4
+ import java .io .ByteArrayOutputStream ;
5
+ import java .io .IOException ;
3
6
import java .io .InputStream ;
4
7
import java .io .Reader ;
5
8
import java .sql .NClob ;
@@ -48,14 +51,21 @@ public void setNClob(int parameterIndex, NClob value) throws SQLException {
48
51
}
49
52
50
53
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
+ }
53
63
}
54
64
55
65
public void setBlob (int parameterIndex , InputStream inputStream , long length )
56
66
throws SQLException {
57
- // TODO Support this
58
- throw new SQLFeatureNotSupportedException ( );
67
+ requireLengthIsPositiveInt ( length );
68
+ setBinaryStream ( parameterIndex , inputStream , ( int ) length );
59
69
}
60
70
61
71
public void setNClob (int parameterIndex , Reader reader , long length ) throws SQLException {
@@ -69,35 +79,59 @@ public void setSQLXML(int parameterIndex, SQLXML xmlObject) throws SQLException
69
79
}
70
80
71
81
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 );
74
84
}
75
85
76
86
public void setBinaryStream (int parameterIndex , InputStream x , long length )
77
87
throws SQLException {
78
- // TODO Support this
79
- throw new SQLFeatureNotSupportedException ( );
88
+ requireLengthIsPositiveInt ( length );
89
+ setBinaryStream ( parameterIndex , x , ( int ) length );
80
90
}
81
91
82
92
public void setCharacterStream (int parameterIndex , Reader reader , long length )
83
93
throws SQLException {
84
- // TODO Support this
85
- throw new SQLFeatureNotSupportedException ( );
94
+ requireLengthIsPositiveInt ( length );
95
+ setCharacterStream ( parameterIndex , reader , ( int ) length );
86
96
}
87
97
88
98
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
+ }
91
127
}
92
128
93
129
public void setBinaryStream (int parameterIndex , InputStream x ) throws SQLException {
94
- // TODO Support this
95
- throw new SQLFeatureNotSupportedException ();
130
+ setBytes (parameterIndex , readBytes (x ));
96
131
}
97
132
98
133
public void setCharacterStream (int parameterIndex , Reader reader ) throws SQLException {
99
- // TODO Support this
100
- throw new SQLFeatureNotSupportedException ();
134
+ setCharacterStream (parameterIndex , reader , Integer .MAX_VALUE );
101
135
}
102
136
103
137
public void setNCharacterStream (int parameterIndex , Reader value ) throws SQLException {
@@ -106,13 +140,11 @@ public void setNCharacterStream(int parameterIndex, Reader value) throws SQLExce
106
140
}
107
141
108
142
public void setClob (int parameterIndex , Reader reader ) throws SQLException {
109
- // TODO Support this
110
- throw new SQLFeatureNotSupportedException ();
143
+ setCharacterStream (parameterIndex , reader , Integer .MAX_VALUE );
111
144
}
112
145
113
146
public void setBlob (int parameterIndex , InputStream inputStream ) throws SQLException {
114
- // TODO Support this
115
- throw new SQLFeatureNotSupportedException ();
147
+ setBytes (parameterIndex , readBytes (inputStream ));
116
148
}
117
149
118
150
public void setNClob (int parameterIndex , Reader reader ) throws SQLException {
0 commit comments