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

The following examples show how to use java.sql.Blob#setBytes() . 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: TemporaryLobCreator.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public void setBlobAsBytes(PreparedStatement ps, int paramIndex, @Nullable byte[] content)
		throws SQLException {

	if (content != null) {
		Blob blob = ps.getConnection().createBlob();
		blob.setBytes(1, content);
		this.temporaryBlobs.add(blob);
		ps.setBlob(paramIndex, blob);
	}
	else {
		ps.setBlob(paramIndex, (Blob) null);
	}

	if (logger.isDebugEnabled()) {
		logger.debug(content != null ? "Copied bytes into temporary BLOB with length " + content.length :
				"Set BLOB to null");
	}
}
 
Example 2
Source File: TemporaryLobCreator.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void setBlobAsBytes(PreparedStatement ps, int paramIndex, byte[] content)
		throws SQLException {

	if (content != null) {
		Blob blob = ps.getConnection().createBlob();
		blob.setBytes(1, content);
		this.temporaryBlobs.add(blob);
		ps.setBlob(paramIndex, blob);
	}
	else {
		ps.setBlob(paramIndex, (Blob) null);
	}

	if (logger.isDebugEnabled()) {
		logger.debug(content != null ? "Copied bytes into temporary BLOB with length " + content.length :
				"Set BLOB to null");
	}
}
 
Example 3
Source File: TemporaryLobCreator.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void setBlobAsBytes(PreparedStatement ps, int paramIndex, @Nullable byte[] content)
		throws SQLException {

	if (content != null) {
		Blob blob = ps.getConnection().createBlob();
		blob.setBytes(1, content);
		this.temporaryBlobs.add(blob);
		ps.setBlob(paramIndex, blob);
	}
	else {
		ps.setBlob(paramIndex, (Blob) null);
	}

	if (logger.isDebugEnabled()) {
		logger.debug(content != null ? "Copied bytes into temporary BLOB with length " + content.length :
				"Set BLOB to null");
	}
}
 
Example 4
Source File: BlobSetBytesBoundaryTest.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
public void testSetBytesWithNonPositiveLength() throws SQLException {
    Statement stmt = getConnection().createStatement();
    ResultSet rs = stmt.executeQuery(
            "select dBlob, length from BlobTable");
    rs.next();
    Blob blob = rs.getBlob(1);
    
    try{
        blob.setBytes(1, new byte[] {0x69}, 0, -1);
        fail("Nonpositive Length is not sccepted!");
    } catch (SQLException sqle) {
        assertSQLState("XJ071", sqle);
    }
    
    stmt.close();
}
 
Example 5
Source File: BlobSetBytesBoundaryTest.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
public void testSetBytesWithTooLongLength() throws SQLException {
    Statement stmt = getConnection().createStatement();
    ResultSet rs = stmt.executeQuery(
            "select dBlob, length from BlobTable");
    rs.next();
    Blob blob = rs.getBlob(1);
    
    try {
        blob.setBytes(1, new byte[] {0x69}, 0, 2);
        fail("Wrong long length is not accepted!");
    } catch (SQLException sqle) {
        assertSQLState("XJ079", sqle);
    }
    
    stmt.close();
}
 
Example 6
Source File: ContextualLobCreator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Blob createBlob(byte[] bytes) {
	try {
		final Blob blob = createBlob();
		blob.setBytes( 1, bytes );
		return blob;
	}
	catch ( SQLException e ) {
		throw new JDBCException( "Unable to set BLOB bytes after creation", e );
	}
}
 
Example 7
Source File: BlobRegressionTest.java    From r-course with MIT License 5 votes vote down vote up
/**
 * @throws Exception
 */
public void testBug2670() throws Exception {
    byte[] blobData = new byte[32];

    for (int i = 0; i < blobData.length; i++) {
        blobData[i] = 1;
    }

    createTable("testBug2670", "(blobField LONGBLOB)");

    PreparedStatement pStmt = this.conn.prepareStatement("INSERT INTO testBug2670 (blobField) VALUES (?)");
    pStmt.setBytes(1, blobData);
    pStmt.executeUpdate();

    this.rs = this.stmt.executeQuery("SELECT blobField FROM testBug2670");
    this.rs.next();

    Blob blob = this.rs.getBlob(1);

    //
    // Test mid-point insertion
    //
    blob.setBytes(4, new byte[] { 2, 2, 2, 2 });

    byte[] newBlobData = blob.getBytes(1L, (int) blob.length());

    assertTrue("Blob changed length", blob.length() == blobData.length);

    assertTrue("New data inserted wrongly", ((newBlobData[3] == 2) && (newBlobData[4] == 2) && (newBlobData[5] == 2) && (newBlobData[6] == 2)));

    //
    // Test end-point insertion
    //
    blob.setBytes(32, new byte[] { 2, 2, 2, 2 });

    assertTrue("Blob length should be 3 larger", blob.length() == (blobData.length + 3));
}
 
Example 8
Source File: BlobSetBytesBoundaryTest.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
public void testSetBytesWithZeroLength() throws SQLException {
    Statement stmt = getConnection().createStatement();
    ResultSet rs = stmt.executeQuery(
            "select dBlob, length from BlobTable");
    rs.next();
    Blob blob = rs.getBlob(1);
    
    int actualLength = blob.setBytes(1, new byte[] {0x69}, 0, 0);
    assertEquals("return zero for zero length", 0, actualLength);            
    
    stmt.close();
}
 
Example 9
Source File: TemporaryLobCreator.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void setBlobAsBytes(PreparedStatement ps, int paramIndex, byte[] content)
		throws SQLException {

	Blob blob = ps.getConnection().createBlob();
	blob.setBytes(1, content);

	this.temporaryBlobs.add(blob);
	ps.setBlob(paramIndex, blob);

	if (logger.isDebugEnabled()) {
		logger.debug(content != null ? "Copied bytes into temporary BLOB with length " + content.length :
				"Set BLOB to null");
	}
}
 
Example 10
Source File: TemporaryLobCreator.java    From effectivejava with Apache License 2.0 5 votes vote down vote up
@Override
public void setBlobAsBytes(PreparedStatement ps, int paramIndex, byte[] content)
		throws SQLException {

	Blob blob = ps.getConnection().createBlob();
	blob.setBytes(1, content);

	this.temporaryBlobs.add(blob);
	ps.setBlob(paramIndex, blob);

	if (logger.isDebugEnabled()) {
		logger.debug(content != null ? "Copied bytes into temporary BLOB with length " + content.length :
				"Set BLOB to null");
	}
}
 
Example 11
Source File: JDBCSequentialFileFactoryDriver.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/**
 * Persists data to this files associated database mapping.
 *
 * @param file
 * @param data
 * @return
 * @throws SQLException
 */
public int writeToFile(JDBCSequentialFile file, byte[] data, boolean append) throws SQLException {
   synchronized (connection) {
      connection.setAutoCommit(false);
      appendToLargeObject.setLong(1, file.getId());

      int bytesWritten = 0;
      try (ResultSet rs = appendToLargeObject.executeQuery()) {
         if (rs.next()) {
            Blob blob = rs.getBlob(1);
            if (blob == null) {
               blob = connection.createBlob();
            }
            if (append) {
               bytesWritten = blob.setBytes(blob.length() + 1, data);
            } else {
               blob.truncate(0);
               bytesWritten = blob.setBytes(1, data);
            }
            rs.updateBlob(1, blob);
            rs.updateRow();
         }
         connection.commit();
         return bytesWritten;
      } catch (SQLException e) {
         connection.rollback();
         throw e;
      }
   }
}
 
Example 12
Source File: BlobRegressionTest.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @throws Exception
 */
public void testBug2670() throws Exception {
    byte[] blobData = new byte[32];

    for (int i = 0; i < blobData.length; i++) {
        blobData[i] = 1;
    }

    createTable("testBug2670", "(blobField LONGBLOB)");

    PreparedStatement pStmt = this.conn.prepareStatement("INSERT INTO testBug2670 (blobField) VALUES (?)");
    pStmt.setBytes(1, blobData);
    pStmt.executeUpdate();

    this.rs = this.stmt.executeQuery("SELECT blobField FROM testBug2670");
    this.rs.next();

    Blob blob = this.rs.getBlob(1);

    //
    // Test mid-point insertion
    //
    blob.setBytes(4, new byte[] { 2, 2, 2, 2 });

    byte[] newBlobData = blob.getBytes(1L, (int) blob.length());

    assertTrue("Blob changed length", blob.length() == blobData.length);

    assertTrue("New data inserted wrongly", ((newBlobData[3] == 2) && (newBlobData[4] == 2) && (newBlobData[5] == 2) && (newBlobData[6] == 2)));

    //
    // Test end-point insertion
    //
    blob.setBytes(32, new byte[] { 2, 2, 2, 2 });

    assertTrue("Blob length should be 3 larger", blob.length() == (blobData.length + 3));

}
 
Example 13
Source File: BlobSetMethodsTest.java    From spliceengine with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Tests large blob (more than 4k) to ensure LOBStreamControl uses file.
 */
public void testSetBytesLargeBlob () throws SQLException {
    Connection con = getConnection();
    con.setAutoCommit (false);
    PreparedStatement pstmt = con.prepareStatement("insert into " +
            "blobtest (id, data) values (?,?)");
    Blob blob = con.createBlob();
    byte [] data = new byte [BUFFER_SIZE];
    for (int i = 0; i < BUFFER_SIZE; i++) {
        data [i] = (byte) (i % 255);
    }
 //now add more than 4k so file get in use
    for (int i = 0; i < 5; i++)
        blob.setBytes (i * BUFFER_SIZE + 1, data);
    assertEquals (BUFFER_SIZE * 5 , blob.length());
                //update blob in the middle
    byte [] data1 = new byte [UPDATE_SIZE];
    for (int i = 0; i < UPDATE_SIZE; i++)
        data1 [i] = 120;//just any value
    blob.setBytes (BUFFER_SIZE + 1, data1);
    blob.setBytes (BUFFER_SIZE * 5 + 1, data1);
    assertEquals (5 * BUFFER_SIZE + UPDATE_SIZE, blob.length());
    //insert it into table
    pstmt.setInt (1, 3);
    pstmt.setBlob (2, blob);
    pstmt.executeUpdate ();
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("select data from blobtest where " +
                            "id = 3");
    assertEquals(true, rs.next());
    blob = rs.getBlob (1);
    byte [] data2 = blob.getBytes (BUFFER_SIZE + 1, UPDATE_SIZE);
    assertEquals (5 * BUFFER_SIZE + UPDATE_SIZE, blob.length());
    for (int i = 0; i < UPDATE_SIZE; i++)
        assertEquals (data1 [i], data2 [i]);
    data2 = blob.getBytes (5 * BUFFER_SIZE + 1, UPDATE_SIZE);
    for (int i = 0; i < UPDATE_SIZE; i++)
        assertEquals (data1 [i], data2 [i]);
    //test truncate
    blob.truncate (BUFFER_SIZE);
    assertEquals ("truncate failed", BUFFER_SIZE, blob.length());
    rs.close();
    con.commit();
    stmt.close();
    pstmt.close();
}
 
Example 14
Source File: BlobSetMethodsTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * tests set bytes method of blob in memory only mode (less than 4k)
 */
public void testSetBytesSmallBlob () throws SQLException {
  Connection con = TestUtil.getConnection();
  Statement stmt = con.createStatement();
  stmt.execute ("create table blobtest (id integer, data Blob)"+ getSuffix());
  stmt.close();
  
  con.setAutoCommit (false);
  PreparedStatement pstmt = con.prepareStatement("insert into " +
                                                 "blobtest (id, data) values (?,?)");
  pstmt.setInt (1,1);
  Blob blob = con.createBlob();
  //add 1024 bytes
  byte [] data = new byte [BUFFER_SIZE];
  for (int i = 0; i < BUFFER_SIZE; i++) {
    data [i] = (byte) (i % 255);
  }
  blob.setBytes (1, data);
  assertEquals (BUFFER_SIZE, blob.length());
  pstmt.setBlob (2, blob);
  pstmt.executeUpdate();
  
  stmt = con.createStatement();
  ResultSet rs = stmt.executeQuery("select data from blobtest where id = 1");
  assertEquals(true, rs.next());
  blob = rs.getBlob (1);
  assertEquals (BUFFER_SIZE, blob.length());
  //update blob in the middle
  byte [] data1 = new byte [UPDATE_SIZE];
  for (int i = 0; i < UPDATE_SIZE; i++)
    data1 [i] = 120;//just any value
  blob.setBytes (UPDATE_SIZE, data1);
  byte [] data2 = blob.getBytes (100, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals (data1 [i], data2 [i]);
 
  //update it at the end
  blob.setBytes (BUFFER_SIZE + 1, data1);
  assertEquals (BUFFER_SIZE + UPDATE_SIZE, blob.length());
  data2 = blob.getBytes (BUFFER_SIZE + 1, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals (data1 [i], data2 [i]);
  
  //insert the blob and test again
  pstmt.setInt (1, 2);
  pstmt.setBlob (2, blob);
  pstmt.executeUpdate();
  
  rs = stmt.executeQuery("select data from blobtest where " +
                         "id = 2");
  assertEquals(true, rs.next());
  blob = rs.getBlob (1);
  assertEquals (BUFFER_SIZE + UPDATE_SIZE, blob.length());
  data2 = blob.getBytes (100, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals (data1 [i], data2 [i]);
  data2 = blob.getBytes (BUFFER_SIZE + 1, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals (data1 [i], data2 [i]);
  
  //test truncate on small size blob
  blob = con.createBlob();
  data = new byte [100];
  for (int i = 0; i < 100; i++) {
    data [i] = (byte) i;
  }
  blob.setBytes (1, data);
  assertEquals (blob.length(), 100);
  blob.truncate (50);
  assertEquals (blob.length(), 50);
  blob.setBytes (1, data);
  assertEquals ("set failed", blob.length(), 100);
  blob.truncate (50);
  assertEquals ("truncation failed", blob.length(), 50);
  rs.close();
  con.commit();
  stmt.close();
  pstmt.close();
  
  stmt = con.createStatement();
  stmt.execute ("drop table blobtest");
  this.waitTillAllClear();
  stmt.close();
}
 
Example 15
Source File: BlobSetMethodsTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Tests large blob (more than 4k) to ensure LOBStreamControl uses file.
 */
public void testSetBytesLargeBlob() throws SQLException {
  Connection con = TestUtil.getConnection();
  Statement stmt = con.createStatement();
  stmt.execute ("create table blobtest (id integer, data Blob)" + getSuffix());
  stmt.close();

  con.setAutoCommit (false);
  PreparedStatement pstmt = con.prepareStatement("insert into " +
                                                 "blobtest (id, data) values (?,?)");
  Blob blob = con.createBlob();
  byte [] data = new byte [BUFFER_SIZE];
  for (int i = 0; i < BUFFER_SIZE; i++) {
    data [i] = (byte) (i % 255);
  }
  //now add more than 4k so file get in use
  for (int i = 0; i < 5; i++)
    blob.setBytes (i * BUFFER_SIZE + 1, data);
  assertEquals (BUFFER_SIZE * 5 , blob.length());
  //update blob in the middle
  byte [] data1 = new byte [UPDATE_SIZE];
  for (int i = 0; i < UPDATE_SIZE; i++)
    data1 [i] = 120;//just any value
  blob.setBytes (BUFFER_SIZE + 1, data1);
  blob.setBytes (BUFFER_SIZE * 5 + 1, data1);
  assertEquals (5 * BUFFER_SIZE + UPDATE_SIZE, blob.length());
  //insert it into table
  pstmt.setInt (1, 3);
  pstmt.setBlob (2, blob);
  pstmt.executeUpdate ();
  
  stmt = con.createStatement();
  ResultSet rs = stmt.executeQuery("select data from blobtest where " +
                                   "id = 3");
  assertEquals(true, rs.next());
  blob = rs.getBlob (1);
  byte [] data2 = blob.getBytes (BUFFER_SIZE + 1, UPDATE_SIZE);
  assertEquals (5 * BUFFER_SIZE + UPDATE_SIZE, blob.length());
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals (data1 [i], data2 [i]);
  data2 = blob.getBytes (5 * BUFFER_SIZE + 1, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals (data1 [i], data2 [i]);
  //test truncate
  blob.truncate (BUFFER_SIZE);
  assertEquals ("truncate failed", BUFFER_SIZE, blob.length());
  rs.close();
  con.commit();
  stmt.close();
  pstmt.close();
}
 
Example 16
Source File: BlobTestsDUnit.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * tests set bytes method of blob in memory only mode (less than 4k)
 */
public void testSetBytesSmallBlob() throws Exception {
  startVMs(1, 4);

  Connection con = TestUtil.getConnection();
  Statement stmt = con.createStatement();
  stmt.execute("create table blobtest (id integer, data Blob)");
  stmt.close();

  con.setAutoCommit(false);
  PreparedStatement pstmt = con.prepareStatement("insert into "
      + "blobtest (id, data) values (?,?)");
  pstmt.setInt(1, 1);
  Blob blob = con.createBlob();
  // add 1024 bytes
  byte[] data = new byte[BUFFER_SIZE];
  for (int i = 0; i < BUFFER_SIZE; i++) {
    data[i] = (byte)(i % 255);
  }
  blob.setBytes(1, data);
  assertEquals(BUFFER_SIZE, blob.length());
  pstmt.setBlob(2, blob);
  pstmt.executeUpdate();

  stmt = con.createStatement();
  ResultSet rs = stmt.executeQuery("select data from blobtest where id = 1");
  assertEquals(true, rs.next());
  blob = rs.getBlob(1);
  assertEquals(BUFFER_SIZE, blob.length());
  // update blob in the middle
  byte[] data1 = new byte[UPDATE_SIZE];
  for (int i = 0; i < UPDATE_SIZE; i++)
    data1[i] = 120;// just any value
  blob.setBytes(UPDATE_SIZE, data1);
  byte[] data2 = blob.getBytes(100, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals(data1[i], data2[i]);
  // update it at the end
  blob.setBytes(BUFFER_SIZE + 1, data1);
  assertEquals(BUFFER_SIZE + UPDATE_SIZE, blob.length());
  data2 = blob.getBytes(BUFFER_SIZE + 1, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals(data1[i], data2[i]);
  // insert the blob and test again
  pstmt.setInt(1, 2);
  pstmt.setBlob(2, blob);
  pstmt.executeUpdate();
  rs = stmt.executeQuery("select data from blobtest where id = 2");
  assertEquals(true, rs.next());
  blob = rs.getBlob(1);
  assertEquals(BUFFER_SIZE + UPDATE_SIZE, blob.length());
  data2 = blob.getBytes(100, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals(data1[i], data2[i]);
  data2 = blob.getBytes(BUFFER_SIZE + 1, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals(data1[i], data2[i]);

  // test truncate on small size blob
  blob = con.createBlob();
  data = new byte[100];
  for (int i = 0; i < 100; i++) {
    data[i] = (byte)i;
  }
  blob.setBytes(1, data);
  assertEquals(blob.length(), 100);
  blob.truncate(50);
  assertEquals(blob.length(), 50);
  blob.setBytes(1, data);
  assertEquals("set failed", blob.length(), 100);
  blob.truncate(50);
  assertEquals("truncation failed", blob.length(), 50);
  rs.close();
  con.commit();
  stmt.close();
  pstmt.close();

  stmt = con.createStatement();
  stmt.execute("drop table blobtest");
  stmt.close();
}
 
Example 17
Source File: BlobTestsDUnit.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Tests large blob (more than 4k) to ensure LOBStreamControl uses file.
 */
public void testSetBytesLargeBlob() throws Exception {
  startVMs(1, 4);

  Connection con = TestUtil.getConnection();
  Statement stmt = con.createStatement();
  stmt.execute("create table blobtest (id integer, data Blob)");
  stmt.close();

  con.setAutoCommit(false);
  PreparedStatement pstmt = con.prepareStatement("insert into "
      + "blobtest (id, data) values (?,?)");
  Blob blob = con.createBlob();
  byte[] data = new byte[BUFFER_SIZE];
  for (int i = 0; i < BUFFER_SIZE; i++) {
    data[i] = (byte)(i % 255);
  }
  // now add more than 4k so file get in use
  for (int i = 0; i < 5; i++)
    blob.setBytes(i * BUFFER_SIZE + 1, data);
  assertEquals(BUFFER_SIZE * 5, blob.length());
  // update blob in the middle
  byte[] data1 = new byte[UPDATE_SIZE];
  for (int i = 0; i < UPDATE_SIZE; i++)
    data1[i] = 120;// just any value
  blob.setBytes(BUFFER_SIZE + 1, data1);
  blob.setBytes(BUFFER_SIZE * 5 + 1, data1);
  assertEquals(5 * BUFFER_SIZE + UPDATE_SIZE, blob.length());
  // insert it into table
  pstmt.setInt(1, 3);
  pstmt.setBlob(2, blob);
  pstmt.executeUpdate();

  stmt = con.createStatement();
  ResultSet rs = stmt.executeQuery("select data from blobtest where "
      + "id = 3");
  assertEquals(true, rs.next());
  blob = rs.getBlob(1);
  byte[] data2 = blob.getBytes(BUFFER_SIZE + 1, UPDATE_SIZE);
  assertEquals(5 * BUFFER_SIZE + UPDATE_SIZE, blob.length());
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals(data1[i], data2[i]);
  data2 = blob.getBytes(5 * BUFFER_SIZE + 1, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals(data1[i], data2[i]);
  // test truncate
  blob.truncate(BUFFER_SIZE);
  assertEquals("truncate failed", BUFFER_SIZE, blob.length());
  rs.close();
  con.commit();
  stmt.close();
  pstmt.close();
}
 
Example 18
Source File: BlobSetMethodsTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * tests set bytes method of blob in memory only mode (less than 4k)
 */
public void testSetBytesSmallBlob () throws SQLException {
    Connection con = getConnection();
    con.setAutoCommit (false);
    PreparedStatement pstmt = con.prepareStatement("insert into " +
            "blobtest (id, data) values (?,?)");
    pstmt.setInt (1,1);
    Blob blob = con.createBlob();
    //add 1024 bytes
    byte [] data = new byte [BUFFER_SIZE];
    for (int i = 0; i < BUFFER_SIZE; i++) {
        data [i] = (byte) (i % 255);
    }
    blob.setBytes (1, data);
    assertEquals (BUFFER_SIZE, blob.length());
    pstmt.setBlob (2, blob);
    pstmt.executeUpdate();
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery(
            "select data from blobtest where id = 1");
    assertEquals(true, rs.next());
    blob = rs.getBlob (1);
    assertEquals (BUFFER_SIZE, blob.length());
    //update blob in the middle
    byte [] data1 = new byte [UPDATE_SIZE];
    for (int i = 0; i < UPDATE_SIZE; i++)
        data1 [i] = 120;//just any value
    blob.setBytes (UPDATE_SIZE, data1);
    byte [] data2 = blob.getBytes (100, UPDATE_SIZE);
    for (int i = 0; i < UPDATE_SIZE; i++)
        assertEquals (data1 [i], data2 [i]);
    //update it at the end
    blob.setBytes (BUFFER_SIZE + 1, data1);
    assertEquals (BUFFER_SIZE + UPDATE_SIZE, blob.length());
    data2 = blob.getBytes (BUFFER_SIZE + 1, UPDATE_SIZE);
    for (int i = 0; i < UPDATE_SIZE; i++)
        assertEquals (data1 [i], data2 [i]);
    //insert the blob and test again
    pstmt.setInt (1, 2);
    pstmt.setBlob (2, blob);
    pstmt.executeUpdate();
    rs = stmt.executeQuery("select data from blobtest where " +
            "id = 2");
    assertEquals(true, rs.next());
    blob = rs.getBlob (1);
    assertEquals (BUFFER_SIZE + UPDATE_SIZE, blob.length());
    data2 = blob.getBytes (100, UPDATE_SIZE);
    for (int i = 0; i < UPDATE_SIZE; i++)
        assertEquals (data1 [i], data2 [i]);
    data2 = blob.getBytes (BUFFER_SIZE + 1, UPDATE_SIZE);
    for (int i = 0; i < UPDATE_SIZE; i++)
        assertEquals (data1 [i], data2 [i]);

    //test truncate on small size blob
    blob = con.createBlob();
    data = new byte [100];
    for (int i = 0; i < 100; i++) {
        data [i] = (byte) i;
    }
    blob.setBytes (1, data);
    assertEquals (blob.length(), 100);
    blob.truncate (50);
    assertEquals (blob.length(), 50);
    blob.setBytes (1, data);
    assertEquals ("set failed", blob.length(), 100);
    blob.truncate (50);
    assertEquals ("truncation failed", blob.length(), 50);
    rs.close();
    con.commit();
    stmt.close();
    pstmt.close();
}
 
Example 19
Source File: BlobSetMethodsTest.java    From spliceengine with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * tests set bytes method of blob in memory only mode (less than 4k)
 */
public void testSetBytesSmallBlob () throws SQLException {
    Connection con = getConnection();
    con.setAutoCommit (false);
    PreparedStatement pstmt = con.prepareStatement("insert into " +
            "blobtest (id, data) values (?,?)");
    pstmt.setInt (1,1);
    Blob blob = con.createBlob();
    //add 1024 bytes
    byte [] data = new byte [BUFFER_SIZE];
    for (int i = 0; i < BUFFER_SIZE; i++) {
        data [i] = (byte) (i % 255);
    }
    blob.setBytes (1, data);
    assertEquals (BUFFER_SIZE, blob.length());
    pstmt.setBlob (2, blob);
    pstmt.executeUpdate();
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery(
            "select data from blobtest where id = 1");
    assertEquals(true, rs.next());
    blob = rs.getBlob (1);
    assertEquals (BUFFER_SIZE, blob.length());
    //update blob in the middle
    byte [] data1 = new byte [UPDATE_SIZE];
    for (int i = 0; i < UPDATE_SIZE; i++)
        data1 [i] = 120;//just any value
    blob.setBytes (UPDATE_SIZE, data1);
    byte [] data2 = blob.getBytes (100, UPDATE_SIZE);
    for (int i = 0; i < UPDATE_SIZE; i++)
        assertEquals (data1 [i], data2 [i]);
    //update it at the end
    blob.setBytes (BUFFER_SIZE + 1, data1);
    assertEquals (BUFFER_SIZE + UPDATE_SIZE, blob.length());
    data2 = blob.getBytes (BUFFER_SIZE + 1, UPDATE_SIZE);
    for (int i = 0; i < UPDATE_SIZE; i++)
        assertEquals (data1 [i], data2 [i]);
    //insert the blob and test again
    pstmt.setInt (1, 2);
    pstmt.setBlob (2, blob);
    pstmt.executeUpdate();
    rs = stmt.executeQuery("select data from blobtest where " +
            "id = 2");
    assertEquals(true, rs.next());
    blob = rs.getBlob (1);
    assertEquals (BUFFER_SIZE + UPDATE_SIZE, blob.length());
    data2 = blob.getBytes (100, UPDATE_SIZE);
    for (int i = 0; i < UPDATE_SIZE; i++)
        assertEquals (data1 [i], data2 [i]);
    data2 = blob.getBytes (BUFFER_SIZE + 1, UPDATE_SIZE);
    for (int i = 0; i < UPDATE_SIZE; i++)
        assertEquals (data1 [i], data2 [i]);

    //test truncate on small size blob
    blob = con.createBlob();
    data = new byte [100];
    for (int i = 0; i < 100; i++) {
        data [i] = (byte) i;
    }
    blob.setBytes (1, data);
    assertEquals (blob.length(), 100);
    blob.truncate (50);
    assertEquals (blob.length(), 50);
    blob.setBytes (1, data);
    assertEquals ("set failed", blob.length(), 100);
    blob.truncate (50);
    assertEquals ("truncation failed", blob.length(), 50);
    rs.close();
    con.commit();
    stmt.close();
    pstmt.close();
}
 
Example 20
Source File: BlobTestsDUnit.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Tests large blob (more than 4k) to ensure LOBStreamControl uses file.
 */
public void testSetBytesLargeBlob() throws Exception {
  startVMs(1, 4);

  Connection con = TestUtil.getConnection();
  Statement stmt = con.createStatement();
  stmt.execute("create table blobtest (id integer, data Blob)");
  stmt.close();

  con.setAutoCommit(false);
  PreparedStatement pstmt = con.prepareStatement("insert into "
      + "blobtest (id, data) values (?,?)");
  Blob blob = con.createBlob();
  byte[] data = new byte[BUFFER_SIZE];
  for (int i = 0; i < BUFFER_SIZE; i++) {
    data[i] = (byte)(i % 255);
  }
  // now add more than 4k so file get in use
  for (int i = 0; i < 5; i++)
    blob.setBytes(i * BUFFER_SIZE + 1, data);
  assertEquals(BUFFER_SIZE * 5, blob.length());
  // update blob in the middle
  byte[] data1 = new byte[UPDATE_SIZE];
  for (int i = 0; i < UPDATE_SIZE; i++)
    data1[i] = 120;// just any value
  blob.setBytes(BUFFER_SIZE + 1, data1);
  blob.setBytes(BUFFER_SIZE * 5 + 1, data1);
  assertEquals(5 * BUFFER_SIZE + UPDATE_SIZE, blob.length());
  // insert it into table
  pstmt.setInt(1, 3);
  pstmt.setBlob(2, blob);
  pstmt.executeUpdate();

  stmt = con.createStatement();
  ResultSet rs = stmt.executeQuery("select data from blobtest where "
      + "id = 3");
  assertEquals(true, rs.next());
  blob = rs.getBlob(1);
  byte[] data2 = blob.getBytes(BUFFER_SIZE + 1, UPDATE_SIZE);
  assertEquals(5 * BUFFER_SIZE + UPDATE_SIZE, blob.length());
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals(data1[i], data2[i]);
  data2 = blob.getBytes(5 * BUFFER_SIZE + 1, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals(data1[i], data2[i]);
  // test truncate
  blob.truncate(BUFFER_SIZE);
  assertEquals("truncate failed", BUFFER_SIZE, blob.length());
  rs.close();
  con.commit();
  stmt.close();
  pstmt.close();
}