Search in sources :

Example 6 with FbBlob

use of org.firebirdsql.gds.ng.FbBlob in project jaybird by FirebirdSQL.

the class TestV10InputBlob method testBlobSeek_streamed.

/**
 * Tests absolute seek on a stream blob.
 */
@Test
public void testBlobSeek_streamed() throws Exception {
    final int testId = 1;
    final byte[] baseContent = generateBaseContent();
    // Use sufficiently large value so that multiple segments are used
    final int requiredSize = 200;
    populateStreamBlob(testId, baseContent, requiredSize);
    try (FbWireDatabase db = createDatabaseConnection()) {
        try {
            long blobId = getBlobId(testId, db);
            // NOTE: What matters is if the blob on the server is stream or segment
            final FbBlob blob = db.createBlobForInput(transaction, null, blobId);
            blob.open();
            final int offset = requiredSize / 2;
            blob.seek(offset, FbBlob.SeekMode.ABSOLUTE);
            byte[] segment = blob.getSegment(100);
            byte[] expected = Arrays.copyOfRange(baseContent, offset, offset + 100);
            blob.close();
            statement.close();
            assertEquals("Unexpected length read from blob", 100, segment.length);
            assertArrayEquals("Unexpected segment content", expected, segment);
        } finally {
            if (transaction != null)
                transaction.commit();
        }
    }
}
Also used : FbWireDatabase(org.firebirdsql.gds.ng.wire.FbWireDatabase) FbBlob(org.firebirdsql.gds.ng.FbBlob) Test(org.junit.Test)

Example 7 with FbBlob

use of org.firebirdsql.gds.ng.FbBlob in project jaybird by FirebirdSQL.

the class TestV10InputBlob method testReopen.

/**
 * Tests reopen is allowed.
 */
@Test
public void testReopen() throws Exception {
    final int testId = 1;
    final byte[] baseContent = generateBaseContent();
    final int requiredSize = 256;
    populateBlob(testId, baseContent, requiredSize);
    try (FbWireDatabase db = createDatabaseConnection()) {
        try {
            long blobId = getBlobId(testId, db);
            final FbBlob blob = db.createBlobForInput(transaction, null, blobId);
            blob.open();
            ByteArrayOutputStream bos = new ByteArrayOutputStream(requiredSize);
            while (!blob.isEof()) {
                bos.write(blob.getSegment(blob.getMaximumSegmentSize()));
            }
            blob.close();
            // Reopen
            blob.open();
            bos = new ByteArrayOutputStream(requiredSize);
            while (!blob.isEof()) {
                bos.write(blob.getSegment(blob.getMaximumSegmentSize()));
            }
            blob.close();
            statement.close();
            byte[] result = bos.toByteArray();
            assertEquals("Unexpected length read from blob", requiredSize, result.length);
            assertTrue("Unexpected blob content", validateBlobContent(result, baseContent, requiredSize));
        } finally {
            if (transaction != null)
                transaction.commit();
        }
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) FbWireDatabase(org.firebirdsql.gds.ng.wire.FbWireDatabase) FbBlob(org.firebirdsql.gds.ng.FbBlob) Test(org.junit.Test)

Example 8 with FbBlob

use of org.firebirdsql.gds.ng.FbBlob in project jaybird by FirebirdSQL.

the class TestV10InputBlob method testBlobRetrieval.

/**
 * Tests retrieval of a blob (what goes in is what comes out).
 */
@Test
public void testBlobRetrieval() 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;
    populateBlob(testId, baseContent, requiredSize);
    try (FbWireDatabase db = createDatabaseConnection()) {
        try {
            long blobId = getBlobId(testId, db);
            final FbBlob blob = db.createBlobForInput(transaction, null, blobId);
            blob.open();
            ByteArrayOutputStream bos = new ByteArrayOutputStream(requiredSize);
            while (!blob.isEof()) {
                bos.write(blob.getSegment(blob.getMaximumSegmentSize()));
            }
            blob.close();
            statement.close();
            byte[] result = bos.toByteArray();
            assertEquals("Unexpected length read from blob", requiredSize, result.length);
            assertTrue("Unexpected blob content", validateBlobContent(result, baseContent, requiredSize));
        } finally {
            if (transaction != null)
                transaction.commit();
        }
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) FbWireDatabase(org.firebirdsql.gds.ng.wire.FbWireDatabase) FbBlob(org.firebirdsql.gds.ng.FbBlob) Test(org.junit.Test)

Example 9 with FbBlob

use of org.firebirdsql.gds.ng.FbBlob in project jaybird by FirebirdSQL.

the class TestV10OutputBlob method testReopen.

/**
 * Test reopen is not allowed.
 */
@Test
public void testReopen() throws Exception {
    expectedException.expect(SQLNonTransientException.class);
    expectedException.expect(allOf(errorCodeEquals(ISCConstants.isc_segstr_no_op), fbMessageStartsWith(ISCConstants.isc_segstr_no_op)));
    final byte[] baseContent = generateBaseContent();
    final int requiredSize = 256;
    final byte[] testBytes = generateBlobContent(baseContent, requiredSize);
    try (FbWireDatabase db = createDatabaseConnection()) {
        final FbTransaction transaction = getTransaction(db);
        try {
            final FbBlob blob = db.createBlobForOutput(transaction, null);
            blob.open();
            int bytesWritten = 0;
            while (bytesWritten < testBytes.length) {
                // TODO the interface for writing blobs should be simpler
                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();
            // Reopen
            blob.open();
        } finally {
            transaction.commit();
        }
    }
}
Also used : FbWireDatabase(org.firebirdsql.gds.ng.wire.FbWireDatabase) FbTransaction(org.firebirdsql.gds.ng.FbTransaction) FbBlob(org.firebirdsql.gds.ng.FbBlob) Test(org.junit.Test)

Example 10 with FbBlob

use of org.firebirdsql.gds.ng.FbBlob in project jaybird by FirebirdSQL.

the class TestV10OutputBlob method testIsEof_afterClose.

/**
 * Test if blob is eof after close.
 */
@Test
public void testIsEof_afterClose() throws Exception {
    try (FbWireDatabase db = createDatabaseConnection()) {
        final FbTransaction transaction = getTransaction(db);
        try {
            FbBlob blob = db.createBlobForOutput(transaction, null);
            assumeTrue("Output blob before open should be eof", blob.isEof());
            blob.open();
            blob.close();
            assertTrue("Output blob after close should be eof", blob.isEof());
        } finally {
            transaction.commit();
        }
    }
}
Also used : FbWireDatabase(org.firebirdsql.gds.ng.wire.FbWireDatabase) FbTransaction(org.firebirdsql.gds.ng.FbTransaction) FbBlob(org.firebirdsql.gds.ng.FbBlob) Test(org.junit.Test)

Aggregations

FbBlob (org.firebirdsql.gds.ng.FbBlob)12 FbWireDatabase (org.firebirdsql.gds.ng.wire.FbWireDatabase)11 Test (org.junit.Test)11 FbTransaction (org.firebirdsql.gds.ng.FbTransaction)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DatatypeCoder (org.firebirdsql.gds.ng.DatatypeCoder)1 FbStatement (org.firebirdsql.gds.ng.FbStatement)1 FieldValue (org.firebirdsql.gds.ng.fields.FieldValue)1 SimpleStatementListener (org.firebirdsql.gds.ng.wire.SimpleStatementListener)1 Synchronizable (org.firebirdsql.jdbc.Synchronizable)1