use of org.firebirdsql.gds.ng.wire.FbWireDatabase 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();
}
}
}
use of org.firebirdsql.gds.ng.wire.FbWireDatabase 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();
}
}
}
use of org.firebirdsql.gds.ng.wire.FbWireDatabase 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();
}
}
}
use of org.firebirdsql.gds.ng.wire.FbWireDatabase 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.ng.wire.FbWireDatabase 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();
}
}
}
Aggregations