Java Code Examples for java.sql.Blob#setBinaryStream()

The following examples show how to use java.sql.Blob#setBinaryStream() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Dialect.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Blob mergeBlob(Blob original, Blob target, SharedSessionContractImplementor session) {
	if ( original != target ) {
		try {
			// the BLOB just read during the load phase of merge
			final OutputStream connectedStream = target.setBinaryStream( 1L );
			// the BLOB from the detached state
			final InputStream detachedStream = original.getBinaryStream();
			StreamCopier.copy( detachedStream, connectedStream );
			return target;
		}
		catch (SQLException e ) {
			throw session.getFactory().getSQLExceptionHelper().convert( e, "unable to merge BLOB data" );
		}
	}
	else {
		return NEW_LOCATOR_LOB_MERGE_STRATEGY.mergeBlob( original, target, session );
	}
}
 
Example 2
Source File: TestFBBlob.java    From jaybird with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testUseBlob() throws Exception {
    setupTable("T1");

    t.begin();
    PreparedStatement p = c.prepareStatement("insert into T1 values (?, ?)");
    Blob blob = c.createBlob();
    OutputStream os = blob.setBinaryStream(1);
    byte[] a = "a".getBytes();
    byte[] testbuf = new byte[bloblength];
    Arrays.fill(testbuf, a[0]);
    os.write(testbuf);
    os.close();

    p.setInt(1, 1);
    p.setBlob(2, blob);
    assertEquals("executeUpdate count != 1", 1, p.executeUpdate());

    p.close();
    checkReadBlob("T1");

    t.commit();
}
 
Example 3
Source File: AbstractJdbcStorageDao.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
private static void setReplicationDataBlob ( final PreparedStatement stmt, final Event event ) throws SQLException, IOException
{
    final Blob blob = stmt.getConnection ().createBlob ();

    final ObjectOutputStream oos = new ObjectOutputStream ( blob.setBinaryStream ( 1 ) );
    oos.writeObject ( event );
    oos.close ();

    stmt.setBlob ( 4, blob );
}
 
Example 4
Source File: SqlFileStore.java    From syndesis with Apache License 2.0 5 votes vote down vote up
private static void doWriteDerby(Handle h, String path, InputStream file) {
    doDelete(h, path);
    try {
        Blob blob = h.getConnection().createBlob();
        try (OutputStream out = blob.setBinaryStream(1)) {
            IOUtils.copy(file, out);
        }

        h.insert("INSERT INTO filestore(path, data) values (?,?)", path, blob);
    } catch (IOException | SQLException ex) {
        throw DaoException.launderThrowable(ex);
    }
}
 
Example 5
Source File: BlobGenerator.java    From aliyun-maxcompute-data-collectors with Apache License 2.0 5 votes vote down vote up
@Override
public Blob next() {
  try {
    Blob blob =
        (Blob) methCreateTemporary.invoke(null, conn, false, durationSession);

    int blobSize =
        (int) (rng.nextDouble() * (maxBytes - minBytes) + minBytes);
    byte[] blobData = new byte[blobSize];
    rng.nextBytes(blobData);

    // blob.setBytes(blobData);

    OutputStream os = blob.setBinaryStream(1);
    InputStream is = new ByteArrayInputStream(blobData);
    int bufferSize = (Integer) methGetBufferSize.invoke(blob);
    byte[] buffer = new byte[bufferSize];
    int bytesRead = 0;
    while ((bytesRead = is.read(buffer)) != -1) {
      os.write(buffer, 0, bytesRead);
    }
    os.close();
    is.close();

    return blob;
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
 
Example 6
Source File: LobStreamsTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Tests the BlobOutputStream.write(byte  b[], int off, int len) method
 **/
public void testBlobWrite3Param() throws Exception {
    InputStream streamIn = new LoopingAlphabetStream(streamSize[0]);
    assertTrue("FAIL -- file not found", streamIn != null);

    PreparedStatement stmt3 = prepareStatement(
        "SELECT b FROM testBlobX1 WHERE a = 1");
    ResultSet rs3 = stmt3.executeQuery();
    rs3.next();
    Blob blob = rs3.getBlob(1);

    assertTrue ("FAIL -- blob is NULL", (blob != null));

    int count = 0;
    byte[] buffer = new byte[1024];
    OutputStream outstream = blob.setBinaryStream(1L);
    while ((count = streamIn.read(buffer)) != -1) {
        outstream.write(buffer, 0, count);
    }
    outstream.close();
    streamIn.close();

    PreparedStatement stmt4 = prepareStatement(
        "UPDATE testBlobX1 SET b = ? WHERE a = 1");
    stmt4.setBlob(1,  blob);
    stmt4.executeUpdate();
    stmt4.close();
    rs3.close();
    // GemStone changes BEGIN
    getConnection().commit();
    // GemStone changes END

    rs3 = stmt3.executeQuery();
    assertTrue("FAIL -- blob not found", rs3.next());

    long new_length = rs3.getBlob(1).length();
    assertEquals("FAIL -- wrong blob length;",
            streamSize[0], new_length);

    // Check contents ...
    InputStream fStream = new LoopingAlphabetStream(streamSize[0]);
    InputStream lStream = rs3.getBlob(1).getBinaryStream();
    assertTrue("FAIL - Blob and file contents do not match",
            compareLob2File(fStream, lStream));

    fStream.close();
    lStream.close();
    rs3.close();
    stmt3.close();
}
 
Example 7
Source File: LobStreamsTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Tests the BlobOutputStream.write(int b) method
 **/
public void testBlobWrite1Param() throws Exception {
    InputStream streamIn = new LoopingAlphabetStream(streamSize[1]);

    PreparedStatement stmt3 = prepareStatement(
        "SELECT b FROM testBlobX1 WHERE a = 1");
    ResultSet rs3 = stmt3.executeQuery();
    rs3.next();
    Blob blob = rs3.getBlob(1);

    assertTrue("FAIL -- blob is NULL", blob != null);

    int buffer;
    OutputStream outstream = blob.setBinaryStream(1L);
    while ((buffer = streamIn.read()) != -1) {
        outstream.write(buffer);
    }
    outstream.close();
    streamIn.close();

    PreparedStatement stmt4 = prepareStatement(
        "UPDATE testBlobX1 SET b = ? WHERE a = 1");
    stmt4.setBlob(1,  blob);
    stmt4.executeUpdate();
    stmt4.close();

    // GemStone changes BEGIN
    getConnection().commit();
    // GemStone changes END

    rs3.close();
    rs3 = stmt3.executeQuery();

    assertTrue("FAIL -- blob not found", rs3.next());

    long new_length = rs3.getBlob(1).length();
    assertEquals("FAIL -- wrong blob length", streamSize[1], new_length);

    // Check contents ...
    InputStream fStream = new LoopingAlphabetStream(streamSize[1]);
    InputStream lStream = rs3.getBlob(1).getBinaryStream();
    assertTrue("FAIL - Blob and file contents do not match",
            compareLob2File(fStream, lStream));

    fStream.close();
    lStream.close();
    rs3.close();
    stmt3.close();
}
 
Example 8
Source File: ConnectionMethodsTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Test the createBlob method implementation in the Connection interface
 *
 * @exception  SQLException, FileNotFoundException, Exception if error occurs
 */
public void testCreateBlob() throws   SQLException,
        FileNotFoundException,
        IOException,
        Exception{

    Connection conn = getConnection();
    int b, c;
    Blob blob;

    Statement s = createStatement();
    PreparedStatement ps =
            prepareStatement("insert into blobtable2 (n, blobcol)" + " values(?,?)");
    ps.setInt(1,1000);
    blob = conn.createBlob();

    try {
        is = (FileInputStream) AccessController.doPrivileged(
                new PrivilegedExceptionAction() {
            public Object run() throws FileNotFoundException {
                return new FileInputStream("extin/short.txt");
            }
        });
    } catch (PrivilegedActionException e) {
        // e.getException() should be an instance of FileNotFoundException,
        // as only "checked" exceptions will be "wrapped" in a
        // PrivilegedActionException.
        throw (FileNotFoundException) e.getException();
    }

    OutputStream os = blob.setBinaryStream(1);
    ArrayList beforeUpdateList = new ArrayList();

    int actualLength = 0;
    c = is.read();
    while(c>0) {
        os.write(c);
        beforeUpdateList.add(c);
        c = is.read();
        actualLength ++;
    }
    ps.setBlob(2, blob);
    ps.executeUpdate();

    Statement stmt = createStatement();
    ResultSet rs =
            stmt.executeQuery("select blobcol from blobtable2 where n = 1000");
    assertTrue(rs.next());

    blob = rs.getBlob(1);
    assertEquals(beforeUpdateList.size(), blob.length());

    //Get the InputStream from this Blob.
    InputStream in = blob.getBinaryStream();
    ArrayList afterUpdateList = new ArrayList();

    b = in.read();

    while (b > -1) {
        afterUpdateList.add(b);
        b = in.read();
    }

    assertEquals(beforeUpdateList.size(), afterUpdateList.size());

    //Now check if the two InputStreams
    //match
    for (int i = 0; i < blob.length(); i++) {
        assertEquals(beforeUpdateList.get(i), afterUpdateList.get(i));
    }

    os.close();
    is.close();
}
 
Example 9
Source File: LobStreamsTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Tests the BlobOutputStream.write(byte  b[], int off, int len) method
 **/
public void testBlobWrite3Param() throws Exception {
    InputStream streamIn = new LoopingAlphabetStream(streamSize[0]);
    assertTrue("FAIL -- file not found", streamIn != null);

    PreparedStatement stmt3 = prepareStatement(
        "SELECT b FROM testBlobX1 WHERE a = 1");
    ResultSet rs3 = stmt3.executeQuery();
    rs3.next();
    Blob blob = rs3.getBlob(1);

    assertTrue ("FAIL -- blob is NULL", (blob != null));

    int count = 0;
    byte[] buffer = new byte[1024];
    OutputStream outstream = blob.setBinaryStream(1L);
    while ((count = streamIn.read(buffer)) != -1) {
        outstream.write(buffer, 0, count);
    }
    outstream.close();
    streamIn.close();

    PreparedStatement stmt4 = prepareStatement(
        "UPDATE testBlobX1 SET b = ? WHERE a = 1");
    stmt4.setBlob(1,  blob);
    stmt4.executeUpdate();
    stmt4.close();
    rs3.close();
    // GemStone changes BEGIN
    getConnection().commit();
    // GemStone changes END

    rs3 = stmt3.executeQuery();
    assertTrue("FAIL -- blob not found", rs3.next());

    long new_length = rs3.getBlob(1).length();
    assertEquals("FAIL -- wrong blob length;",
            streamSize[0], new_length);

    // Check contents ...
    InputStream fStream = new LoopingAlphabetStream(streamSize[0]);
    InputStream lStream = rs3.getBlob(1).getBinaryStream();
    assertTrue("FAIL - Blob and file contents do not match",
            compareLob2File(fStream, lStream));

    fStream.close();
    lStream.close();
    rs3.close();
    stmt3.close();
}
 
Example 10
Source File: LobStreamsTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Tests the BlobOutputStream.write(int b) method
 **/
public void testBlobWrite1Param() throws Exception {
    InputStream streamIn = new LoopingAlphabetStream(streamSize[1]);

    PreparedStatement stmt3 = prepareStatement(
        "SELECT b FROM testBlobX1 WHERE a = 1");
    ResultSet rs3 = stmt3.executeQuery();
    rs3.next();
    Blob blob = rs3.getBlob(1);

    assertTrue("FAIL -- blob is NULL", blob != null);

    int buffer;
    OutputStream outstream = blob.setBinaryStream(1L);
    while ((buffer = streamIn.read()) != -1) {
        outstream.write(buffer);
    }
    outstream.close();
    streamIn.close();

    PreparedStatement stmt4 = prepareStatement(
        "UPDATE testBlobX1 SET b = ? WHERE a = 1");
    stmt4.setBlob(1,  blob);
    stmt4.executeUpdate();
    stmt4.close();

    // GemStone changes BEGIN
    getConnection().commit();
    // GemStone changes END

    rs3.close();
    rs3 = stmt3.executeQuery();

    assertTrue("FAIL -- blob not found", rs3.next());

    long new_length = rs3.getBlob(1).length();
    assertEquals("FAIL -- wrong blob length", streamSize[1], new_length);

    // Check contents ...
    InputStream fStream = new LoopingAlphabetStream(streamSize[1]);
    InputStream lStream = rs3.getBlob(1).getBinaryStream();
    assertTrue("FAIL - Blob and file contents do not match",
            compareLob2File(fStream, lStream));

    fStream.close();
    lStream.close();
    rs3.close();
    stmt3.close();
}
 
Example 11
Source File: ConnectionMethodsTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Test the createBlob method implementation in the Connection interface
 *
 * @exception  SQLException, FileNotFoundException, Exception if error occurs
 */
public void testCreateBlob() throws   SQLException,
        FileNotFoundException,
        IOException,
        Exception{

    Connection conn = getConnection();
    int b, c;
    Blob blob;

    Statement s = createStatement();
    PreparedStatement ps =
            prepareStatement("insert into blobtable2 (n, blobcol)" + " values(?,?)");
    ps.setInt(1,1000);
    blob = conn.createBlob();

    try {
        is = (FileInputStream) AccessController.doPrivileged(
                new PrivilegedExceptionAction() {
            public Object run() throws FileNotFoundException {
                return new FileInputStream("extin/short.txt");
            }
        });
    } catch (PrivilegedActionException e) {
        // e.getException() should be an instance of FileNotFoundException,
        // as only "checked" exceptions will be "wrapped" in a
        // PrivilegedActionException.
        throw (FileNotFoundException) e.getException();
    }

    OutputStream os = blob.setBinaryStream(1);
    ArrayList beforeUpdateList = new ArrayList();

    int actualLength = 0;
    c = is.read();
    while(c>0) {
        os.write(c);
        beforeUpdateList.add(c);
        c = is.read();
        actualLength ++;
    }
    ps.setBlob(2, blob);
    ps.executeUpdate();

    Statement stmt = createStatement();
    ResultSet rs =
            stmt.executeQuery("select blobcol from blobtable2 where n = 1000");
    assertTrue(rs.next());

    blob = rs.getBlob(1);
    assertEquals(beforeUpdateList.size(), blob.length());

    //Get the InputStream from this Blob.
    InputStream in = blob.getBinaryStream();
    ArrayList afterUpdateList = new ArrayList();

    b = in.read();

    while (b > -1) {
        afterUpdateList.add(b);
        b = in.read();
    }

    assertEquals(beforeUpdateList.size(), afterUpdateList.size());

    //Now check if the two InputStreams
    //match
    for (int i = 0; i < blob.length(); i++) {
        assertEquals(beforeUpdateList.get(i), afterUpdateList.get(i));
    }

    os.close();
    is.close();
}
 
Example 12
Source File: LobStreamsTest.java    From spliceengine with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Tests the BlobOutputStream.write(byte  b[], int off, int len) method
 **/
public void testBlobWrite3Param() throws Exception {
    InputStream streamIn = new LoopingAlphabetStream(streamSize[0]);
    assertTrue("FAIL -- file not found", streamIn != null);

    PreparedStatement stmt3 = prepareStatement(
        "SELECT b FROM testBlobX1 WHERE a = 1");
    ResultSet rs3 = stmt3.executeQuery();
    rs3.next();
    Blob blob = rs3.getBlob(1);

    assertTrue ("FAIL -- blob is NULL", (blob != null));

    int count = 0;
    byte[] buffer = new byte[1024];
    OutputStream outstream = blob.setBinaryStream(1L);
    while ((count = streamIn.read(buffer)) != -1) {
        outstream.write(buffer, 0, count);
    }
    outstream.close();
    streamIn.close();

    PreparedStatement stmt4 = prepareStatement(
        "UPDATE testBlobX1 SET b = ? WHERE a = 1");
    stmt4.setBlob(1,  blob);
    stmt4.executeUpdate();
    stmt4.close();
    rs3.close();

    rs3 = stmt3.executeQuery();
    assertTrue("FAIL -- blob not found", rs3.next());

    blob = rs3.getBlob(1);
    long new_length = blob.length();
    assertEquals("FAIL -- wrong blob length;",
            streamSize[0], new_length);

    // Check contents ...
    InputStream fStream = new LoopingAlphabetStream(streamSize[0]);
    InputStream lStream = blob.getBinaryStream();
    assertTrue("FAIL - Blob and file contents do not match",
            compareLob2File(fStream, lStream));

    fStream.close();
    lStream.close();
    rs3.close();
    stmt3.close();
}
 
Example 13
Source File: LobStreamsTest.java    From spliceengine with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Tests the BlobOutputStream.write(int b) method
 **/
public void testBlobWrite1Param() throws Exception {
    InputStream streamIn = new LoopingAlphabetStream(streamSize[1]);

    PreparedStatement stmt3 = prepareStatement(
        "SELECT b FROM testBlobX1 WHERE a = 1");
    ResultSet rs3 = stmt3.executeQuery();
    rs3.next();
    Blob blob = rs3.getBlob(1);

    assertTrue("FAIL -- blob is NULL", blob != null);

    int buffer;
    OutputStream outstream = blob.setBinaryStream(1L);
    while ((buffer = streamIn.read()) != -1) {
        outstream.write(buffer);
    }
    outstream.close();
    streamIn.close();

    PreparedStatement stmt4 = prepareStatement(
        "UPDATE testBlobX1 SET b = ? WHERE a = 1");
    stmt4.setBlob(1,  blob);
    stmt4.executeUpdate();
    stmt4.close();

    rs3.close();
    rs3 = stmt3.executeQuery();

    assertTrue("FAIL -- blob not found", rs3.next());

    blob = rs3.getBlob(1);
    long new_length = blob.length();
    assertEquals("FAIL -- wrong blob length", streamSize[1], new_length);

    // Check contents ...
    InputStream fStream = new LoopingAlphabetStream(streamSize[1]);
    InputStream lStream = blob.getBinaryStream();
    assertTrue("FAIL - Blob and file contents do not match",
            compareLob2File(fStream, lStream));

    fStream.close();
    lStream.close();
    rs3.close();
    stmt3.close();
}
 
Example 14
Source File: ConnectionMethodsTest.java    From spliceengine with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Test the createBlob method implementation in the Connection interface
 *
 * @exception  SQLException, FileNotFoundException, Exception if error occurs
 */
public void testCreateBlob() throws   SQLException,
        FileNotFoundException,
        IOException,
        Exception{

    Connection conn = getConnection();
    int b, c;
    Blob blob;

    Statement s = createStatement();
    PreparedStatement ps =
            prepareStatement("insert into blobtable2 (n, blobcol)" + " values(?,?)");
    ps.setInt(1,1000);
    blob = conn.createBlob();

    try {
        is = AccessController.doPrivileged(
                new PrivilegedExceptionAction<FileInputStream>() {
            public FileInputStream run() throws FileNotFoundException {
                return new FileInputStream("extin/short.txt");
            }
        });
    } catch (PrivilegedActionException e) {
        // e.getException() should be an instance of FileNotFoundException,
        // as only "checked" exceptions will be "wrapped" in a
        // PrivilegedActionException.
        throw (FileNotFoundException) e.getException();
    }

    OutputStream os = blob.setBinaryStream(1);
    ArrayList<Integer> beforeUpdateList = new ArrayList<Integer>();

    int actualLength = 0;
    c = is.read();
    while(c>0) {
        os.write(c);
        beforeUpdateList.add(c);
        c = is.read();
        actualLength ++;
    }
    ps.setBlob(2, blob);
    ps.executeUpdate();

    Statement stmt = createStatement();
    ResultSet rs =
            stmt.executeQuery("select blobcol from blobtable2 where n = 1000");
    assertTrue(rs.next());

    blob = rs.getBlob(1);
    assertEquals(beforeUpdateList.size(), blob.length());

    //Get the InputStream from this Blob.
    InputStream in = blob.getBinaryStream();
    ArrayList<Integer> afterUpdateList = new ArrayList<Integer>();

    b = in.read();

    while (b > -1) {
        afterUpdateList.add(b);
        b = in.read();
    }

    assertEquals(beforeUpdateList.size(), afterUpdateList.size());

    //Now check if the two InputStreams
    //match
    for (int i = 0; i < blob.length(); i++) {
        assertEquals(beforeUpdateList.get(i), afterUpdateList.get(i));
    }

    os.close();
    is.close();
}