I am trying to pull binary data back out of a DB for output to a stream.
I've tried Blob, InputStream, and byte[] for the method return type.
I can't seem to get any of them to work.
I am using JDBI SqlObject.
Code snippet:
default void audioBytesStream(long aid, OutputStream os) throws IOException {
try (InputStream bs = new ByteArrayInputStream(audioBytes(aid))) {
IOUtils.copy(bs, os);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
@SqlQuery("select data from aqv_audio where aid=:aid")
byte[] audioBytes(@Bind("aid")long aid);
I've also tried with Blob (binaryStream()), and InputStream.
The
jdbi.org site shows usage of InputStream but it seems iffy to me. If you provide a FileInputStream to a method with a parameter of InputStream it fails because the type FileInputStream is not an exact match to the interface of InputStream.
To get the data into the DB when using FileInputStream I had to do:
default void setAudioBytesData(long aid, InputStream data) {
try {
useHandle(h->{
h.createUpdate("update aqv_audio set data=:data where aid=:aid") //
.bind("data", new InputStreamArgument(data, data.available(), false)) //
.bind("aid", aid) //
.execute();
});
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
The DB is MariaDB.
Any help would be appreciated.