use of org.firebirdsql.gds.BlobParameterBuffer in project jaybird by FirebirdSQL.
the class TestJnaBlob method testOutputBlobStorage_Stream.
/**
* Tests storage of a stream blob (what goes in is what comes out).
*/
@Test
public void testOutputBlobStorage_Stream() throws Exception {
final int testId = 1;
final byte[] baseContent = generateBaseContent();
// Use sufficiently large value so that multiple segments are used
final int requiredSize = 4 * Short.MAX_VALUE;
final byte[] testBytes = generateBlobContent(baseContent, requiredSize);
try (JnaDatabase db = createDatabaseConnection()) {
final BlobParameterBuffer blobParameterBuffer = db.createBlobParameterBuffer();
blobParameterBuffer.addArgument(BlobParameterBuffer.TYPE, BlobParameterBuffer.TYPE_STREAM);
writeBlob(testId, testBytes, db, blobParameterBuffer);
}
assertTrue("Unexpected blob content", validateBlob(testId, baseContent, requiredSize));
}
use of org.firebirdsql.gds.BlobParameterBuffer in project jaybird by FirebirdSQL.
the class BaseTestBlob method populateStreamBlob.
/**
* Populates a stream blob for testing.
*
* @param testId Id of the record to be inserted.
* @throws SQLException
*/
protected void populateStreamBlob(int testId, byte[] baseContent, int requiredSize) throws SQLException {
final byte[] testBytes = generateBlobContent(baseContent, requiredSize);
try (FbDatabase db = createDatabaseConnection()) {
listener = new SimpleStatementListener();
transaction = getTransaction(db);
try {
statement = db.createStatement(transaction);
statement.addStatementListener(listener);
final BlobParameterBuffer blobParameterBuffer = db.createBlobParameterBuffer();
blobParameterBuffer.addArgument(BlobParameterBuffer.TYPE, BlobParameterBuffer.TYPE_STREAM);
final FbBlob blob = db.createBlobForOutput(transaction, blobParameterBuffer);
blob.open();
int bytesWritten = 0;
while (bytesWritten < testBytes.length) {
byte[] buffer = new byte[Math.min(blob.getMaximumSegmentSize(), testBytes.length - bytesWritten)];
System.arraycopy(testBytes, bytesWritten, buffer, 0, buffer.length);
blob.putSegment(buffer);
bytesWritten += buffer.length;
}
blob.close();
statement.prepare(INSERT_BLOB_TABLE);
final DatatypeCoder datatypeCoder = db.getDatatypeCoder();
RowValue rowValue = RowValue.of(datatypeCoder.encodeInt(testId), datatypeCoder.encodeLong(blob.getBlobId()));
statement.execute(rowValue);
statement.close();
} finally {
transaction.commit();
}
}
}
use of org.firebirdsql.gds.BlobParameterBuffer in project jaybird by FirebirdSQL.
the class JnaBlob method open.
@Override
public void open() throws SQLException {
try {
if (isOutput() && getBlobId() != NO_BLOB_ID) {
throw new FbExceptionBuilder().nonTransientException(ISCConstants.isc_segstr_no_op).toSQLException();
}
final BlobParameterBuffer blobParameterBuffer = getBlobParameterBuffer();
final byte[] bpb;
if (blobParameterBuffer != null) {
bpb = blobParameterBuffer.toBytesWithType();
} else {
bpb = new byte[0];
}
synchronized (getSynchronizationObject()) {
checkDatabaseAttached();
checkTransactionActive();
checkBlobClosed();
final JnaDatabase db = getDatabase();
if (isOutput()) {
clientLibrary.isc_create_blob2(statusVector, db.getJnaHandle(), getTransaction().getJnaHandle(), getJnaHandle(), blobId, (short) bpb.length, bpb);
} else {
clientLibrary.isc_open_blob2(statusVector, db.getJnaHandle(), getTransaction().getJnaHandle(), getJnaHandle(), blobId, (short) bpb.length, bpb);
}
processStatusVector();
setOpen(true);
resetEof();
}
} catch (SQLException e) {
exceptionListenerDispatcher.errorOccurred(e);
throw e;
}
}
use of org.firebirdsql.gds.BlobParameterBuffer in project jaybird by FirebirdSQL.
the class TestV10OutputBlob method testBlobStorage_Stream.
/**
* Tests storage of a stream blob (what goes in is what comes out).
*/
@Test
public void testBlobStorage_Stream() throws Exception {
final int testId = 1;
final byte[] baseContent = generateBaseContent();
// Use sufficiently large value so that multiple segments are used
final int requiredSize = 4 * Short.MAX_VALUE;
final byte[] testBytes = generateBlobContent(baseContent, requiredSize);
try (FbWireDatabase db = createDatabaseConnection()) {
final BlobParameterBuffer blobParameterBuffer = db.createBlobParameterBuffer();
blobParameterBuffer.addArgument(BlobParameterBuffer.TYPE, BlobParameterBuffer.TYPE_STREAM);
writeBlob(testId, testBytes, db, blobParameterBuffer);
}
assertTrue("Unexpected blob content", validateBlob(testId, baseContent, requiredSize));
}
use of org.firebirdsql.gds.BlobParameterBuffer in project jaybird by FirebirdSQL.
the class V10InputBlob method open.
// TODO Need blob specific warning callback?
@Override
public void open() throws SQLException {
try {
synchronized (getSynchronizationObject()) {
checkDatabaseAttached();
checkTransactionActive();
checkBlobClosed();
final FbWireDatabase database = getDatabase();
try {
final XdrOutputStream xdrOut = database.getXdrStreamAccess().getXdrOut();
final BlobParameterBuffer blobParameterBuffer = getBlobParameterBuffer();
if (blobParameterBuffer == null) {
xdrOut.writeInt(op_open_blob);
} else {
xdrOut.writeInt(op_open_blob2);
xdrOut.writeTyped(blobParameterBuffer);
}
xdrOut.writeInt(getTransaction().getHandle());
xdrOut.writeLong(getBlobId());
xdrOut.flush();
} catch (IOException e) {
throw new FbExceptionBuilder().exception(ISCConstants.isc_net_write_err).cause(e).toSQLException();
}
try {
final GenericResponse genericResponse = database.readGenericResponse(null);
setHandle(genericResponse.getObjectHandle());
setOpen(true);
resetEof();
} catch (IOException e) {
throw new FbExceptionBuilder().exception(ISCConstants.isc_net_read_err).cause(e).toSQLException();
}
// TODO Request information on the blob?
}
} catch (SQLException e) {
exceptionListenerDispatcher.errorOccurred(e);
throw e;
}
}
Aggregations