import java.sql.Blob; import javax.sql.rowset.serial.SerialBlob; byte[] byteArray = .....; Blob blob = new SerialBlob(byteArray);
/** * String 2 blob blob. * * @param str the str * @return the blob * @throws SQLException the sql exception */ public static Blob string2blob(String str) throws SQLException { if (str == null) { return null; } return new SerialBlob(str.getBytes()); }
@Override public Blob toDatabaseType(byte[] value) { if (value == null) { return null; } try { return new SerialBlob(value); } catch (SQLException e) { throw new RuntimeException(e); } }
@Override public Blob read(ResultSet results, int column) throws SQLException { byte[] value = results.getBytes(column); return results.wasNull() ? null : new SerialBlob(value); }
@Override public Blob toDatabaseType(BigInteger value) { if (value == null) return null; try { return new SerialBlob(value.toByteArray()); } catch (final SQLException ex) { throw new SpeedmentTypeMapperException( "Error mapping BigInteger to Blob.", ex); } }
/** * String 2 blob blob. * * @param str the str * @return the blob */ public static Blob string2blob(String str) { if (str == null) { return null; } try { return new SerialBlob(str.getBytes()); } catch (Exception e) { throw new ShouldNeverHappenException(e); } }
Blob blob = new javax.sql.rowset.serial.SerialBlob(bytes);
@Override protected Blob getBlob(ResultSet resultSet, int columnIndex, Metadata m) throws SQLException { byte[] bytes = resultSet.getBytes(columnIndex); if (!resultSet.wasNull()) { return new SerialBlob(bytes); } return null; } }
@Override protected Blob valueToDBType(String value) throws SQLException { if (value != null) { return new SerialBlob(value.getBytes(StandardCharsets.UTF_8)); } else { return null; } }
@Override protected Blob getBlob(ResultSet resultSet, int columnIndex, Metadata m) throws SQLException { byte[] bytes = resultSet.getBytes(columnIndex); if (!resultSet.wasNull()) { return new SerialBlob(bytes); } return null; } }
@Override public void executeSetPreparedStatement(Object pstmt, int index, Object data) throws Exception { if (data == null) { ((PreparedStatement) pstmt).setNull(index, Types.BLOB); } else { ((PreparedStatement) pstmt).setBlob(index, new SerialBlob((byte[]) data)); } }
public static SerialBlob createBlob(byte[] bytes) { if (bytes == null) { return null; } try { return new SerialBlob(bytes); } catch (SQLException e) { throw new TeiidRuntimeException(CorePlugin.Event.TEIID10047, e); } }
public Blob transform(byte[] value) throws TransformationException { if(value == null) return null; try{ return new SerialBlob(value); } catch(SQLException e){ throw new TransformationException(e); } } }
@Override public Blob getBlob(int index) throws SQLException { checkIndex(index); return new javax.sql.rowset.serial.SerialBlob(currentRow.getBytes(index-1).array()); }
@Override public Blob getBlob(int index) throws SQLException { checkIndex(index); return new javax.sql.rowset.serial.SerialBlob(currentRow.getBytes(index-1).array()); }
@Test public void testToChars4() throws Exception { byte[] stringBytes = "hello world".getBytes("UTF-8"); byte[] bytes = new byte[stringBytes.length+3]; bytes[0] = (byte)0xef; bytes[1] = (byte)0xbb; bytes[2] = (byte)0xbf; System.arraycopy(stringBytes, 0, bytes, 3, stringBytes.length); Clob result = (Clob)helpInvokeMethod("to_chars", new Class<?>[] {DefaultDataClasses.BLOB, DefaultDataClasses.STRING}, new Object[] { new BlobType(new SerialBlob(bytes)), "UTF-8-BOM" }, null); //$NON-NLS-1$ String string = result.getSubString(1, (int)result.length()); assertEquals("hello world", string); //bom is stripped }
@Test public void testToChars() throws Exception { Clob result = (Clob)helpInvokeMethod("to_chars", new Class<?>[] {DefaultDataClasses.BLOB, DefaultDataClasses.STRING}, new Object[] { new BlobType(new SerialBlob("hello world".getBytes("ASCII"))), "ASCII" }, null); //$NON-NLS-1$ String string = result.getSubString(1, (int)result.length()); assertEquals("hello world", string); }
@Test public void testJSONParseBlob() throws Exception { HardcodedDataManager dataManager = new HardcodedDataManager(); String sql = "select jsonParse(cast(? as blob), false) x"; //$NON-NLS-1$ String json = "{\"name\":123}"; List<?>[] expected = new List[] { Arrays.asList(json), }; processPreparedStatement(sql, expected, dataManager, new DefaultCapabilitiesFinder(), RealMetadataFactory.example1Cached(), Arrays.asList(new BlobType(new SerialBlob(json.getBytes(Charset.forName("UTF-16BE")))))); }
private Blob dataAsBlob(File file) throws IOException { byte[] byteArray = dataAsBytes( file ); try { return new SerialBlob( byteArray ); } catch (SQLException e) { throw new AssertionFailure( "Unexpected error creating a blob", e ); } }
private void helpTestJson(String json, String rootName, String expected) throws SQLException, TeiidComponentException, TeiidProcessingException, SerialException, IOException { CommandContext cc = new CommandContext(); cc.setBufferManager(BufferManagerFactory.getStandaloneBufferManager()); SQLXML xml = XMLSystemFunctions.jsonToXml(cc, rootName, new SerialClob(json.toCharArray())); assertEquals(expected, xml.getString()); xml = XMLSystemFunctions.jsonToXml(cc, rootName, new SerialBlob(json.getBytes(Charset.forName("UTF-8")))); assertEquals(expected, xml.getString()); xml = XMLSystemFunctions.jsonToXml(cc, rootName, new SerialBlob(json.getBytes(Charset.forName("UTF-32BE")))); assertEquals(expected, xml.getString()); }