I khave some doubt about how convert a Blob object (taken from a database and representing a PDF) into a ByteArrayInputStream object.
So I know that I can do something like this:
ByteArrayOutputStream docPDF = null;
InputStream blobinstream = null;
Blob blob = rset.getBlob("PDF"); // I obtain it from a result set DB query
if(blob!=null) {
blobinstream = blob.getBinaryStream();
int chunk = 1024;
byte[] buffer = new byte[chunk];
int length = -1;
docPDF = new ByteArrayOutputStream();
while ((length = blobinstream.read(buffer)) != -1) {
docPDF.write(buffer, 0, length);
}
docPDF.flush();
So in the previous code snippet I obtained the Blob object from a DB query and I read it obtaining the ByteArrayOutputStream docPDF.
Then I do something like this to convert my ByteArrayOutputStream docPDF into a ByteArrayInputStream object:
ByteArrayInputStream currentPdfBAIS = new ByteArrayInputStream(docPDF.toByteArray());
So I have obtained my ByteArrayInputStream object.
It works fine but it is the best way to do it? Can I obtain a ByteArrayInputStream object starting from a Blob object without passing through the ByteArrayOutputStream docPdf object ? Or the previous presented solution is the right one?
Tnx