use of org.teiid.core.types.BlobType in project teiid by teiid.
the class S3ProcedureExecution method next.
@Override
public List<?> next() throws TranslatorException, DataNotAvailableException {
if (this.command.getProcedureName().equalsIgnoreCase(S3ExecutionFactory.SAVEFILE) || this.command.getProcedureName().equalsIgnoreCase(S3ExecutionFactory.DELETEFILE)) {
return null;
}
if (this.execution == null || this.execution.getResponseCode() < 200 || this.execution.getResponseCode() > 300) {
return null;
}
Blob contents = (Blob) execution.getOutputParameterValues().get(0);
BlobInputStreamFactory isf = new BlobInputStreamFactory(contents);
String length = getHeader("Content-Length");
if (length != null) {
isf.setLength(Long.parseLong(length));
}
Object value = null;
if (isText) {
ClobImpl clob = new ClobImpl(isf, -1);
clob.setCharset(Charset.forName(this.ef.getEncoding()));
value = new ClobType(clob);
if (!streaming) {
value = new InputStreamFactory.ClobInputStreamFactory(clob);
}
} else {
if (streaming) {
value = new BlobType(contents);
} else {
value = isf;
}
}
String lastModified = getHeader("Last-Modified");
ArrayList<Object> result = new ArrayList<Object>(2);
result.add(value);
if (!isList) {
result.add(endpoint);
try {
SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss zzz");
result.add(lastModified == null ? null : new Timestamp(df.parse(lastModified).getTime()));
} catch (ParseException e) {
result.add(null);
}
result.add(getHeader("ETag"));
result.add(length);
}
this.execution = null;
return result;
}
use of org.teiid.core.types.BlobType in project teiid by teiid.
the class AccumuloDataTypeManager method deserialize.
public static Object deserialize(final byte[] value, final Class<?> expectedType) {
if (value == null || Arrays.equals(value, EMPTY_BYTES)) {
return null;
}
try {
if (expectedType.isAssignableFrom(Clob.class)) {
return new ClobImpl(new InputStreamFactory() {
@Override
public InputStream getInputStream() throws IOException {
return ObjectConverterUtil.convertToInputStream(value);
}
}, -1);
} else if (expectedType.isAssignableFrom(Blob.class)) {
return new BlobType(new BlobImpl(new InputStreamFactory() {
@Override
public InputStream getInputStream() throws IOException {
return ObjectConverterUtil.convertToInputStream(value);
}
}));
} else if (expectedType.isAssignableFrom(SQLXML.class)) {
return new SQLXMLImpl(new InputStreamFactory() {
@Override
public InputStream getInputStream() throws IOException {
return ObjectConverterUtil.convertToInputStream(value);
}
});
} else if (expectedType.isAssignableFrom(BinaryType.class)) {
return new BinaryType(value);
} else if (expectedType.isAssignableFrom(GeometryType.class)) {
GeometryType result = new GeometryType(Arrays.copyOf(value, value.length - 4));
int srid = (((value[value.length - 4] & 0xff) << 24) + ((value[value.length - 3] & 0xff) << 16) + ((value[value.length - 2] & 0xff) << 8) + ((value[value.length - 1] & 0xff) << 0));
result.setSrid(srid);
return result;
} else if (expectedType.isAssignableFrom(byte[].class)) {
return value;
} else if (expectedType.isAssignableFrom(String.class) || expectedType.isAssignableFrom(Boolean.class) || expectedType.isAssignableFrom(Boolean.class) || expectedType.isAssignableFrom(Byte.class) || expectedType.isAssignableFrom(Short.class) || expectedType.isAssignableFrom(Character.class) || expectedType.isAssignableFrom(Integer.class) || expectedType.isAssignableFrom(Long.class) || expectedType.isAssignableFrom(BigInteger.class) || expectedType.isAssignableFrom(BigDecimal.class) || expectedType.isAssignableFrom(Float.class) || expectedType.isAssignableFrom(Double.class) || expectedType.isAssignableFrom(Date.class) || expectedType.isAssignableFrom(Time.class) || expectedType.isAssignableFrom(Timestamp.class)) {
return DataTypeManager.transformValue(new String(value, UTF_8), expectedType);
} else {
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(value));
Object obj = ois.readObject();
ois.close();
return obj;
}
} catch (ClassNotFoundException e) {
throw new TeiidRuntimeException(e);
} catch (IOException e) {
throw new TeiidRuntimeException(e);
} catch (TransformationException e) {
throw new TeiidRuntimeException(e);
}
}
use of org.teiid.core.types.BlobType in project teiid by teiid.
the class FunctionMethods method toBytes.
@TeiidFunction(category = FunctionCategoryConstants.CONVERSION, name = "to_bytes")
public static BlobType toBytes(ClobType value, String encoding, boolean wellFormed) throws IOException, SQLException {
Charset cs = getCharset(encoding);
ClobInputStreamFactory cisf = new ClobInputStreamFactory(value.getReference());
cisf.setCharset(cs);
if (!wellFormed || CharsetUtils.BASE64_NAME.equalsIgnoreCase(encoding) || CharsetUtils.HEX_NAME.equalsIgnoreCase(encoding)) {
// validate that the binary conversion is possible
// TODO: cache the result in a filestore
InputStream is = new ReaderInputStream(value.getCharacterStream(), cs.newEncoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT));
try {
while (is.read() != -1) {
}
} catch (IOException e) {
CharacterCodingException cce = ExceptionUtil.getExceptionOfType(e, CharacterCodingException.class);
if (cce != null) {
throw new IOException(CorePlugin.Util.gs(CorePlugin.Event.TEIID10083, cs.displayName()), cce);
}
throw e;
} finally {
is.close();
}
}
return new BlobType(new BlobImpl(cisf));
}
use of org.teiid.core.types.BlobType in project teiid by teiid.
the class GeometryUtils method geometryToEwkb.
/**
* We'll take the wkb format and add the extended flag/srid
* @param geometry
* @return
*/
public static BlobType geometryToEwkb(final GeometryType geometry) {
final Blob b = geometry.getReference();
BlobImpl blobImpl = new BlobImpl(new InputStreamFactory() {
@Override
public InputStream getInputStream() throws IOException {
PushbackInputStream pbis;
try {
pbis = new PushbackInputStream(b.getBinaryStream(), 9);
} catch (SQLException e) {
throw new IOException(e);
}
int byteOrder = pbis.read();
if (byteOrder == -1) {
return pbis;
}
byte[] typeInt = new byte[4];
int bytesRead = pbis.read(typeInt);
if (bytesRead == 4) {
int srid = geometry.getSrid();
byte[] sridInt = new byte[4];
ByteOrderValues.putInt(srid, sridInt, byteOrder == 0 ? ByteOrderValues.BIG_ENDIAN : ByteOrderValues.LITTLE_ENDIAN);
pbis.unread(sridInt);
typeInt[byteOrder == 0 ? 0 : 3] |= 0x20;
}
pbis.unread(typeInt, 0, bytesRead);
pbis.unread(byteOrder);
return pbis;
}
});
return new BlobType(blobImpl);
}
use of org.teiid.core.types.BlobType in project teiid by teiid.
the class TestDQPCore method testLobConcurrency.
@Test
public void testLobConcurrency() throws Exception {
RequestMessage reqMsg = exampleRequestMessage("select to_bytes(stringkey, 'utf-8') FROM BQT1.SmallA");
reqMsg.setTxnAutoWrapMode(RequestMessage.TXN_WRAP_OFF);
agds.setSleep(100);
ResultsFuture<ResultsMessage> message = core.executeRequest(reqMsg.getExecutionId(), reqMsg);
final LobThread t = new LobThread(reqMsg);
t.start();
message.addCompletionListener(new ResultsFuture.CompletionListener<ResultsMessage>() {
@Override
public void onCompletion(ResultsFuture<ResultsMessage> future) {
try {
final BlobType bt = (BlobType) future.get().getResultsList().get(0).get(0);
synchronized (t) {
t.bt = bt;
t.workContext = DQPWorkContext.getWorkContext();
t.notify();
}
// give the Thread a chance to run
Thread.sleep(100);
} catch (Exception e) {
t.interrupt();
throw new RuntimeException(e);
}
}
});
message.get();
t.join();
assertNotNull(t.chunkFuture.get().getBytes());
}
Aggregations