I have a rest API that writes the InputStream of the body of a post directly to the database using a PreparedStatement like so:
public void store(String id, InputStream body) throws SQLException, SPSException {
if(id == null || id.length() == 0)
throw new SPSException("get: id is missing: " + id);
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getConnection();
Parameters parameters = Parameters.parse("insert into properties (id, body) values (?,?)");
ps = conn.prepareStatement( parameters.getSQL() );
ps.setString(1, id );
ps.setBlob(2, body);
ps.executeUpdate();
conn.commit();
} finally {
close( ps );
close( conn );
}
}
The setBlob call takes the InputStream. Is there a way I can wrap this inputStream with a jsonParser InputStream? So that if the json doesn't parse, an exception is thrown?
I don't want to have to write my own or have to rebuild the stream if I don't have to, but I can't find anything available that would do this with the need for a lot of extra code.
Note - I cannot read the object into memory in its entirety. It has to be a streaming solution.