DB2 end of stream prematurely reached while reading inputstream

Following code:

InputStream imageData=image.getInputStream();
byte[] bytes = ByteStreams.toByteArray(imageData);
pst.setBinaryStream(3, imageData, (int)bytes.length);

gives the following error:

com.ibm.db2.jcc.am.SqlException: [jcc][t4][2039][11162][3.57.82] End of stream prematurely reached while reading InputStream, parameter #3. Remaining data has been padded with 0x0. ERRORCODE=-4225, SQLSTATE=null`

and the DDL which I used to create table is following:

CREATE TABLE ARCH_TRANCHE.UPLOAD_DETAIL (
    SEQ_ID VARCHAR(200) NOT NULL,
    FILE_NAME VARCHAR(140) NOT NULL,
    IMAGE_FILE BLOB(1048576),
    CREATED_DATE TIMESTAMP,
    LAST_MODIFIED_DATE TIMESTAMP,
    FILE_SIZE VARCHAR(20),
    PRIMARY KEY (SEQ_ID,FILE_NAME)
);
Jon Skeet
people
quotationmark

I suspect the problem is that you've already read the data by this point in order to obtain bytes... so when the driver tries to read from imageData, it immediately finds there's no more data to read.

I suggest you just set the stream without a length:

pst.setBinaryStream(3, image.getInputStream());

If you can't do that (as per comments) you could extract all the data into a byte array as you're already doing, then wrap that in a ByteArrayInputStream:

InputStream imageData = image.getInputStream();
byte[] bytes = ByteStreams.toByteArray(imageData);
pst.setBinaryStream(3, new ByteArrayInputStream(bytes), bytes.length);

people

See more on this question at Stackoverflow