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

The following examples show how to use java.sql.Blob#getBinaryStream() . 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: JDBCLinkStore.java    From qpid-broker-j with Apache License 2.0 7 votes vote down vote up
private String getBlobValueAsString(final ResultSet resultSet, final int index) throws SQLException
{
    if (_isUseBytesMethodsForBlob)
    {
        return new String(resultSet.getBytes(index), UTF_8);
    }

    Blob blob = resultSet.getBlob(index);
    try (InputStream is = blob.getBinaryStream();
         InputStreamReader isr = new InputStreamReader(is, UTF_8))
    {
        return CharStreams.toString(isr);
    }
    catch (IOException e)
    {
        throw new StoreException("Cannot convert blob to string", e);
    }
    finally
    {
        blob.free();
    }
}
 
Example 2
Source File: SelectColumnTest.java    From herddb with Apache License 2.0 6 votes vote down vote up
private static void checkBlob(ResultSet rs, Object expected) {
    try {
        byte[] actual = rs.getBytes(1);
        Assert.assertArrayEquals((byte[]) expected, actual);

        if (expected != null) {
            DataInputStream dataInputStream = new DataInputStream(rs.getBinaryStream(1));
            byte[] actualFromStream = new byte[actual.length];
            dataInputStream.readFully(actualFromStream);
            Assert.assertArrayEquals((byte[]) expected, actualFromStream);

            Blob blob = rs.getBlob(1);
            assertEquals(blob.length(), actual.length);

            DataInputStream dataInputStream2 = new DataInputStream(blob.getBinaryStream());
            byte[] actualFromStream2 = new byte[actual.length];
            dataInputStream2.readFully(actualFromStream2);
            Assert.assertArrayEquals((byte[]) expected, actualFromStream2);
        }

        byte[] object = (byte[]) rs.getObject(1);
        Assert.assertArrayEquals((byte[]) expected, object);
    } catch (SQLException | IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 3
Source File: PatchedStdJDBCDelegate.java    From tomee with Apache License 2.0 6 votes vote down vote up
@Override
protected Object getObjectFromBlob(final ResultSet rs, final String colName)
    throws ClassNotFoundException, IOException, SQLException {
    Object obj = null;

    final Blob blobLocator = rs.getBlob(colName);
    if (blobLocator != null && blobLocator.length() != 0) {
        final InputStream binaryInput = blobLocator.getBinaryStream();

        if (null != binaryInput) {
            if (!(binaryInput instanceof ByteArrayInputStream) || ((ByteArrayInputStream) binaryInput).available() != 0) {
                try (ObjectInputStream in = new QuartzObjectInputStream(binaryInput, classLoadHelper)) {
                    obj = in.readObject();
                }
            }
        }

    }
    return obj;
}
 
Example 4
Source File: JdbcUtil.java    From datacollector with Apache License 2.0 6 votes vote down vote up
private byte[] getBlobBytes(Blob data, int maxBlobSize) throws IOException, SQLException {
  if (data == null) {
    return null;
  }

  ByteArrayOutputStream os = new ByteArrayOutputStream();
  int bufLen = 1024;
  byte[] buf = new byte[bufLen];

  // Read up to max blob length
  long maxRemaining = maxBlobSize;
  int count;
  try(InputStream is = data.getBinaryStream()) {
    while ((count = is.read(buf)) > -1 && maxRemaining > 0) {
      // If count is more then the remaining bytes we want to read, read only as many are available
      if (count > maxRemaining) {
        count = (int) maxRemaining;
      }
      os.write(buf, 0, count);
      // decrement available according to the number of bytes we've read
      maxRemaining -= count;
    }
  }
  return os.toByteArray();
}
 
Example 5
Source File: ComUploadDaoImpl.java    From openemm with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void sendDataToStream(int uploadId, OutputStream stream) throws Exception {
	try(final Connection connection = getDataSource().getConnection()) {
		try(final PreparedStatement stmt = connection.prepareStatement("SELECT payload FROM upload_tbl WHERE upload_id = ?")) {
			stmt.setInt(1, uploadId);
	
			try(final ResultSet rs = stmt.executeQuery()) {
				if (rs.next()) {
					final Blob blob = rs.getBlob("payload");
	
					try {
						try(final InputStream inStream = blob.getBinaryStream()) {
							IOUtils.copy(inStream, stream);
						}
					} finally {
						blob.free();
					}
				} else {
					logger.warn("No data found for upload ID " + uploadId);
					throw new Exception("No data found for upload ID " + uploadId);
				}
			}
		}
	}
}
 
Example 6
Source File: MysqlStateStore.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
/**
 * An helper function extracted from getAll method originally that has side effects:
 * - Executing queryStatement
 * - Put the result into List<state> object.
 * @throws SQLException
 * @throws Exception
 */
private void execGetAllStatement(PreparedStatement queryStatement, List<T> states) throws SQLException, Exception {
  try (ResultSet rs = queryStatement.executeQuery()) {
    while (rs.next()) {
      Blob blob = rs.getBlob(1);
      Text key = new Text();

      try (InputStream is = StreamUtils.isCompressed(blob.getBytes(1, 2)) ?
          new GZIPInputStream(blob.getBinaryStream()) : blob.getBinaryStream();
          DataInputStream dis = new DataInputStream(is)) {
        // keep deserializing while we have data
        while (dis.available() > 0) {
          T state = this.stateClass.newInstance();
          String stateId = key.readString(dis);
          state.readFields(dis);
          state.setId(stateId);
          states.add(state);
        }
      } catch (EOFException e) {
        // no more data. GZIPInputStream.available() doesn't return 0 until after EOF.
      }
    }
  }
}
 
Example 7
Source File: FBLongVarCharField.java    From jaybird with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public byte[] getBytes() throws SQLException {
    final Blob blob = getBlob();
    if (blob == null) return null;

    try (final InputStream in = blob.getBinaryStream()) {
        if (in == null) return null;
        final ByteArrayOutputStream bout = new ByteArrayOutputStream();

        final byte[] buff = new byte[BUFF_SIZE];
        int counter;
        while((counter = in.read(buff)) != -1) {
            bout.write(buff, 0, counter);
        }
        return bout.toByteArray();
    } catch(IOException ioex) {
        throw new TypeConversionException(BYTES_CONVERSION_ERROR + " " + ioex.getMessage());
    }
}
 
Example 8
Source File: OracleLobHandler.java    From effectivejava with Apache License 2.0 5 votes vote down vote up
@Override
public InputStream getBlobAsBinaryStream(ResultSet rs, int columnIndex) throws SQLException {
	logger.debug("Returning Oracle BLOB as binary stream");
	Blob blob = rs.getBlob(columnIndex);
	initializeResourcesBeforeRead(rs.getStatement().getConnection(), blob);
	InputStream retVal = (blob != null ? blob.getBinaryStream() : null);
	releaseResourcesAfterRead(rs.getStatement().getConnection(), blob);
	return retVal;
}
 
Example 9
Source File: CacheDelegate.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * Caché requires {@code java.sql.Blob} instances to be explicitly freed.
 */
@Override
protected Object getJobDataFromBlob(ResultSet rs, String colName) throws ClassNotFoundException, IOException, SQLException {
    if (canUseProperties()) {
        Blob blob = rs.getBlob(colName);
        if (blob == null) {
            return null;
        } else {
            return new BlobFreeingStream(blob, blob.getBinaryStream());
        }
    } else {
        return getObjectFromBlob(rs, colName);
    }
}
 
Example 10
Source File: ESSyncUtil.java    From canal with Apache License 2.0 5 votes vote down vote up
/**
 * Blob转byte[]
 */
private static byte[] blobToBytes(Blob blob) {
    try (InputStream is = blob.getBinaryStream()) {
        byte[] b = new byte[(int) blob.length()];
        if (is.read(b) != -1) {
            return b;
        } else {
            return new byte[0];
        }
    } catch (IOException | SQLException e) {
        logger.error(e.getMessage());
        return null;
    }
}
 
Example 11
Source File: DefaultLobHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public InputStream getBlobAsBinaryStream(ResultSet rs, int columnIndex) throws SQLException {
	logger.debug("Returning BLOB as binary stream");
	if (this.wrapAsLob) {
		Blob blob = rs.getBlob(columnIndex);
		return blob.getBinaryStream();
	}
	else {
		return rs.getBinaryStream(columnIndex);
	}
}
 
Example 12
Source File: DefaultLobHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@Nullable
public InputStream getBlobAsBinaryStream(ResultSet rs, int columnIndex) throws SQLException {
	logger.debug("Returning BLOB as binary stream");
	if (this.wrapAsLob) {
		Blob blob = rs.getBlob(columnIndex);
		return blob.getBinaryStream();
	}
	else {
		return rs.getBinaryStream(columnIndex);
	}
}
 
Example 13
Source File: H2DatabaseResults.java    From ormlite-core with ISC License 5 votes vote down vote up
@Override
public InputStream getBlobStream(int columnIndex) throws SQLException {
	Blob blob = resultSet.getBlob(columnIndex + 1);
	if (blob == null) {
		return null;
	} else {
		return blob.getBinaryStream();
	}
}
 
Example 14
Source File: JtdsResultSet.java    From jTDS with GNU Lesser General Public License v2.1 5 votes vote down vote up
public InputStream getBinaryStream(int columnIndex) throws SQLException {
    Blob blob = getBlob(columnIndex);

    if (blob == null) {
        return null;
    }

    return blob.getBinaryStream();
}
 
Example 15
Source File: ComMailingComponentDaoImpl.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public MailingComponent mapRow(ResultSet resultSet, int index) throws SQLException {
	MailingComponent component = mailingComponentFactory.newMailingComponent();

	component.setCompanyID(resultSet.getInt("company_id"));
	component.setMailingID(resultSet.getInt("mailing_id"));
	component.setId(resultSet.getInt("component_id"));
	component.setComponentName(resultSet.getString("compname"));
	component.setType(resultSet.getInt("comptype"));
	component.setPresent(resultSet.getInt("comppresent"));
	component.setTargetID(resultSet.getInt("target_id"));
	component.setUrlID(resultSet.getInt("url_id"));
	component.setDescription(resultSet.getString("description"));
	component.setTimestamp(resultSet.getTimestamp("timestamp"));

	if (includeContent) {
		Blob blob = resultSet.getBlob("binblock");
		// binblock sometimes contains an array "byte[1] = {0}", which also signals empty binary data
		
		if (blob != null && blob.length() > 1) {
			try (InputStream dataStream = blob.getBinaryStream()) {
				byte[] data = IOUtils.toByteArray(dataStream);
				component.setBinaryBlock(data, resultSet.getString("mtype"));
			} catch (Exception ex) {
				logger.error("Error:" + ex, ex);
			}
		} else {
			component.setEmmBlock(resultSet.getString("emmblock"), resultSet.getString("mtype"));
		}
	}

	return component;
}
 
Example 16
Source File: DefaultStorageDataTypeContext.java    From registry with Apache License 2.0 5 votes vote down vote up
protected Object getJavaObject(Class columnJavaType, String columnLabel, ResultSet resultSet) throws SQLException {
    if (columnJavaType.equals(String.class)) {
        return resultSet.getString(columnLabel);
    } else if (columnJavaType.equals(Byte.class)) {
        return resultSet.getByte(columnLabel);
    } else if (columnJavaType.equals(Integer.class)) {
        return resultSet.getInt(columnLabel);
    } else if (columnJavaType.equals(Double.class)) {
        return resultSet.getDouble(columnLabel);
    } else if (columnJavaType.equals(Float.class)) {
        return resultSet.getFloat(columnLabel);
    } else if (columnJavaType.equals(Short.class)) {
        return resultSet.getShort(columnLabel);
    } else if (columnJavaType.equals(Boolean.class)) {
        return resultSet.getBoolean(columnLabel);
    } else if (columnJavaType.equals(byte[].class)) {
        return resultSet.getBytes(columnLabel);
    } else if (columnJavaType.equals(Long.class)) {
        return resultSet.getLong(columnLabel);
    } else if (columnJavaType.equals(Date.class)) {
        return resultSet.getDate(columnLabel);
    } else if (columnJavaType.equals(Time.class)) {
        return resultSet.getTime(columnLabel);
    } else if (columnJavaType.equals(Timestamp.class)) {
        return resultSet.getTimestamp(columnLabel);
    } else if (columnJavaType.equals(InputStream.class)) {
        Blob blob = resultSet.getBlob(columnLabel);
        return blob != null ? blob.getBinaryStream() : null;
    } else {
        throw new StorageException("type =  [" + columnJavaType + "] for column [" + columnLabel + "] not supported.");
    }
}
 
Example 17
Source File: DefaultLobHandler.java    From effectivejava with Apache License 2.0 5 votes vote down vote up
@Override
public InputStream getBlobAsBinaryStream(ResultSet rs, int columnIndex) throws SQLException {
	logger.debug("Returning BLOB as binary stream");
	if (this.wrapAsLob) {
		Blob blob = rs.getBlob(columnIndex);
		return blob.getBinaryStream();
	}
	else {
		return rs.getBinaryStream(columnIndex);
	}
}
 
Example 18
Source File: JdbcTypesConverter.java    From scriptella-etl with Apache License 2.0 4 votes vote down vote up
protected void setBlob(final PreparedStatement ps, final int index, final Blob blob) throws SQLException {
    InputStream stream = blob.getBinaryStream();
    ps.setBinaryStream(index, stream, (int) blob.length());
    registerResource(stream);
}
 
Example 19
Source File: LargeObjectLoader.java    From aliyun-maxcompute-data-collectors with Apache License 2.0 4 votes vote down vote up
/**
 * Actually read a BlobRef instance from the ResultSet and materialize
 * the data either inline or to a file.
 *
 * @param colNum the column of the ResultSet's current row to read.
 * @param r the ResultSet to read from.
 * @return a BlobRef encapsulating the data in this field.
 * @throws IOException if an error occurs writing to the FileSystem.
 * @throws SQLException if an error occurs reading from the database.
 */
public com.cloudera.sqoop.lib.BlobRef readBlobRef(int colNum, ResultSet r)
    throws IOException, InterruptedException, SQLException {

  long maxInlineLobLen = conf.getLong(
      MAX_INLINE_LOB_LEN_KEY,
      DEFAULT_MAX_LOB_LENGTH);

  Blob b = r.getBlob(colNum);
  if (null == b) {
    return null;
  } else if (b.length() > maxInlineLobLen) {
    // Deserialize very large BLOBs into separate files.
    long len = b.length();
    LobFile.Writer lobWriter = getBlobWriter();

    long recordOffset = lobWriter.tell();
    InputStream is = null;
    OutputStream os = lobWriter.writeBlobRecord(len);
    try {
      is = b.getBinaryStream();
      copyAll(is, os);
    } finally {
      if (null != os) {
        os.close();
      }

      if (null != is) {
        is.close();
      }

      // Mark the record as finished.
      lobWriter.finishRecord();
    }

    return new com.cloudera.sqoop.lib.BlobRef(
        getRelativePath(curBlobWriter), recordOffset, len);
  } else {
    // This is a 1-based array.
    return new com.cloudera.sqoop.lib.BlobRef(
        b.getBytes(1, (int) b.length()));
  }
}
 
Example 20
Source File: BlobTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Test the contents of the testBlob table or ResultSet with identical shape.
 */
public void checkBlobContents(ResultSet rs) throws Exception {
  int nullCount = 0;
  int rowCount = 0;
  byte[] buff = new byte[128];
  // fetch row back, get the long varbinary column as a blob.
  Blob blob;
  int blobLength = 0, i = 0;
  while (rs.next()) {
    i++;
    // get the first column as a clob
    blob = rs.getBlob(1);
    long crc32 = rs.getLong(3);
    boolean crc2Null = rs.wasNull();
    if (blob == null) {
      assertTrue("FAIL - NULL BLOB but non-NULL checksum", crc2Null);
      nullCount++;
    }
    else {
      rowCount++;

      long blobcrc32 = getStreamCheckSum(blob.getBinaryStream());
      assertEquals("FAIL - mismatched checksums for blob with " + "length "
          + blob.length(), blobcrc32, crc32);

      InputStream fin = blob.getBinaryStream();
      int columnSize = 0;
      for (;;) {
        int size = fin.read(buff);
        if (size == -1)
          break;
        columnSize += size;
      }
      blobLength = rs.getInt(2);
      assertEquals("FAIL - wrong column size", blobLength, columnSize);
      assertEquals("FAIL - wrong column length", blobLength, blob.length());
    }
  }
  assertEquals("FAIL - wrong not null row count null:" + nullCount, 9,
      rowCount);
  assertEquals("FAIL - wrong null blob count", 1, nullCount);
}