use of org.teiid.core.types.InputStreamFactory.BlobInputStreamFactory 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.InputStreamFactory.BlobInputStreamFactory in project teiid by teiid.
the class FunctionMethods method toChars.
@TeiidFunction(category = FunctionCategoryConstants.CONVERSION, name = "to_chars")
public static ClobType toChars(BlobType value, String encoding, boolean wellFormed) throws SQLException, IOException {
Charset cs = getCharset(encoding);
BlobInputStreamFactory bisf = new BlobInputStreamFactory(value.getReference());
ClobImpl clob = new ClobImpl(bisf, -1);
clob.setCharset(cs);
if (!wellFormed && !CharsetUtils.BASE64_NAME.equalsIgnoreCase(encoding) && !CharsetUtils.HEX_NAME.equalsIgnoreCase(encoding)) {
// validate that the charcter conversion is possible
// TODO: cache the result in a filestore
Reader r = clob.getCharacterStream();
try {
while (r.read() != -1) {
}
} catch (IOException e) {
CharacterCodingException cce = ExceptionUtil.getExceptionOfType(e, CharacterCodingException.class);
if (cce != null) {
throw new IOException(CorePlugin.Util.gs(CorePlugin.Event.TEIID10082, cs.displayName()), cce);
}
throw e;
} finally {
r.close();
}
}
return new ClobType(clob);
}
use of org.teiid.core.types.InputStreamFactory.BlobInputStreamFactory in project teiid by teiid.
the class LobManager method persistLob.
public static void persistLob(final Streamable<?> lob, final FileStore store, byte[] bytes, boolean inlineLobs, int maxMemoryBytes) throws TeiidComponentException {
long byteLength = Integer.MAX_VALUE;
try {
byteLength = lob.length() * (lob instanceof ClobType ? 2 : 1);
} catch (SQLException e) {
// just ignore for now - for a single read resource computing the length invalidates
// TODO - inline small persisted lobs
}
try {
// inline
if (lob.getReferenceStreamId() == null || (inlineLobs && (byteLength <= maxMemoryBytes))) {
lob.setReferenceStreamId(null);
if (InputStreamFactory.getStorageMode(lob) == StorageMode.MEMORY) {
return;
}
if (lob instanceof BlobType) {
BlobType b = (BlobType) lob;
byte[] blobBytes = b.getBytes(1, (int) byteLength);
b.setReference(new SerialBlob(blobBytes));
} else if (lob instanceof ClobType) {
ClobType c = (ClobType) lob;
// $NON-NLS-1$
String s = "";
// some clob impls return null for 0 length
if (byteLength != 0) {
s = c.getSubString(1, (int) (byteLength >>> 1));
}
c.setReference(new ClobImpl(s));
} else {
XMLType x = (XMLType) lob;
String s = x.getString();
x.setReference(new SQLXMLImpl(s));
}
return;
}
InputStream is = null;
if (lob instanceof BlobType) {
is = new BlobInputStreamFactory((Blob) lob).getInputStream();
} else if (lob instanceof ClobType) {
is = new ClobInputStreamFactory((Clob) lob).getInputStream();
} else {
is = new SQLXMLInputStreamFactory((SQLXML) lob).getInputStream();
}
long offset = store.getLength();
OutputStream fsos = store.createOutputStream();
byteLength = ObjectConverterUtil.write(fsos, is, bytes, -1);
// re-construct the new lobs based on the file store
final long lobOffset = offset;
final long lobLength = byteLength;
/*
* Using an inner class here will hold a reference to the LobManager
* which prevents the removal of the FileStore until all of the
* lobs have been gc'd
*/
InputStreamFactory isf = new InputStreamFactory() {
@Override
public InputStream getInputStream() throws IOException {
return store.createInputStream(lobOffset, lobLength);
}
@Override
public StorageMode getStorageMode() {
return StorageMode.PERSISTENT;
}
};
isf.setLength(byteLength);
if (lob instanceof BlobType) {
((BlobType) lob).setReference(new BlobImpl(isf));
} else if (lob instanceof ClobType) {
long length = -1;
try {
length = ((ClobType) lob).length();
} catch (SQLException e) {
// could be streaming
}
((ClobType) lob).setReference(new ClobImpl(isf, length));
} else {
((XMLType) lob).setReference(new SQLXMLImpl(isf));
}
} catch (SQLException e) {
throw new TeiidComponentException(QueryPlugin.Event.TEIID30037, e);
} catch (IOException e) {
throw new TeiidComponentException(QueryPlugin.Event.TEIID30036, e);
}
}
Aggregations