Search in sources :

Example 1 with BlobParameterBuffer

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));
}
Also used : BlobParameterBuffer(org.firebirdsql.gds.BlobParameterBuffer) Test(org.junit.Test)

Example 2 with BlobParameterBuffer

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();
        }
    }
}
Also used : SimpleStatementListener(org.firebirdsql.gds.ng.wire.SimpleStatementListener) RowValue(org.firebirdsql.gds.ng.fields.RowValue) BlobParameterBuffer(org.firebirdsql.gds.BlobParameterBuffer)

Example 3 with BlobParameterBuffer

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;
    }
}
Also used : SQLException(java.sql.SQLException) FbExceptionBuilder(org.firebirdsql.gds.ng.FbExceptionBuilder) BlobParameterBuffer(org.firebirdsql.gds.BlobParameterBuffer)

Example 4 with BlobParameterBuffer

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));
}
Also used : FbWireDatabase(org.firebirdsql.gds.ng.wire.FbWireDatabase) BlobParameterBuffer(org.firebirdsql.gds.BlobParameterBuffer) Test(org.junit.Test)

Example 5 with BlobParameterBuffer

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;
    }
}
Also used : SQLException(java.sql.SQLException) FbExceptionBuilder(org.firebirdsql.gds.ng.FbExceptionBuilder) XdrOutputStream(org.firebirdsql.gds.impl.wire.XdrOutputStream) IOException(java.io.IOException) BlobParameterBuffer(org.firebirdsql.gds.BlobParameterBuffer)

Aggregations

BlobParameterBuffer (org.firebirdsql.gds.BlobParameterBuffer)6 SQLException (java.sql.SQLException)3 FbExceptionBuilder (org.firebirdsql.gds.ng.FbExceptionBuilder)3 IOException (java.io.IOException)2 XdrOutputStream (org.firebirdsql.gds.impl.wire.XdrOutputStream)2 Test (org.junit.Test)2 RowValue (org.firebirdsql.gds.ng.fields.RowValue)1 FbWireDatabase (org.firebirdsql.gds.ng.wire.FbWireDatabase)1 SimpleStatementListener (org.firebirdsql.gds.ng.wire.SimpleStatementListener)1