@Override public List<?> next() throws TranslatorException, DataNotAvailableException { final DBObject value = nextRow(); if (value == null) { return null; } BlobType result = new BlobType(new BlobImpl(new InputStreamFactory() { @Override public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(JSON.serialize(value).getBytes(Streamable.CHARSET)); } })); if (returnsArray) { List<Object[]> row = new ArrayList<Object[]>(1); row.add(new Object[] {result}); return row; } return Arrays.asList(result); }
private static Object asLob(InputStreamFactory value, Class<?> desiredType) { if (desiredType == DataTypeManager.DefaultDataClasses.CLOB) { //assumes UTF-8 return new ClobType(new ClobImpl(value, -1)); } return new BlobType(new BlobImpl(value)); }
private static Object asLob(InputStreamFactory value, Class<?> desiredType) { if (desiredType == DataTypeManager.DefaultDataClasses.CLOB) { //assumes UTF-8 return new ClobType(new ClobImpl(value, -1)); } return new BlobType(new BlobImpl(value)); }
void setBlob(Object parameterIndex, final InputStream inputStream) throws SQLException { if (inputStream == null) { this.setObject(parameterIndex, null); return; } this.setObject(parameterIndex, new BlobImpl(new InputStreamFactory() { @Override public InputStream getInputStream() throws IOException { return inputStream; } })); }
void setBlob(Object parameterIndex, final InputStream inputStream) throws SQLException { if (inputStream == null) { this.setObject(parameterIndex, null); return; } this.setObject(parameterIndex, new BlobImpl(new InputStreamFactory() { @Override public InputStream getInputStream() throws IOException { return inputStream; } })); }
public BlobImpl getVDBResourceAsBlob(String resourcePath) { final VDBResources.Resource f = getFile(resourcePath); if (f == null) { return null; } return new BlobImpl(new VirtualFileInputStreamFactory(f)); }
public static Object convertByteArrayToTeiidRuntimeType(final Class<?> type, final byte[] contents, final String odataType, String srid) throws TeiidException { if (contents == null) { return null; } Object value = null; if (DataTypeManager.isLOB(type)) { InputStreamFactory isf = new InputStreamFactory() { @Override public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(contents); } }; if (type.isAssignableFrom(SQLXML.class)) { value = new SQLXMLImpl(isf); } else if (type.isAssignableFrom(ClobType.class)) { value = new ClobImpl(isf, -1); } else if (type.isAssignableFrom(BlobType.class)) { value = new BlobImpl(isf); } } else if (DataTypeManager.DefaultDataClasses.VARBINARY.equals(type)) { value = contents; } else { value = convertToTeiidRuntimeType(type, new String(contents), odataType, srid); } return value; }
public BlobImpl getVDBResourceAsBlob(String resourcePath) { final VDBResources.Resource f = getFile(resourcePath); if (f == null) { return null; } return new BlobImpl(new VirtualFileInputStreamFactory(f)); }
public BlobImpl getVDBResourceAsBlob(String resourcePath) { final VDBResources.Resource f = getFile(resourcePath); if (f == null) { return null; } return new BlobImpl(new VirtualFileInputStreamFactory(f)); }
@TeiidFunction(category=FunctionCategoryConstants.CONVERSION, name="to_bytes") public static BlobType toBytes(ClobType value, String encoding, boolean wellFormed) throws IOException, SQLException { Charset cs = CharsetUtils.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)); }
@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)); }
@TeiidFunction(category=FunctionCategoryConstants.CONVERSION, name="to_bytes") public static BlobType toBytes(ClobType value, String encoding, boolean wellFormed) throws IOException, SQLException { Charset cs = CharsetUtils.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)); }
@Override public List<?> next() throws TranslatorException, DataNotAvailableException { if (files == null || index >= files.length) { return null; } ArrayList<Object> result = new ArrayList<>(2); final VirtualFile file = files[index++]; LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Getting", file.getName()); //$NON-NLS-1$ InputStreamFactory isf = file.createInputStreamFactory(); Object value = null; if (isText) { ClobImpl clob = new ClobImpl(isf, -1); clob.setCharset(encoding); value = new ClobType(clob); } else { value = new BlobType(new BlobImpl(isf)); } result.add(value); result.add(file.getName()); if (command.getMetadataObject().getResultSet().getColumns().size() > 2) { result.add(new Timestamp(file.getLastModified())); result.add(new Timestamp(file.getCreationTime())); result.add(file.getSize()); } return result; }
static Object convertFromExcelType(final String value, final Class<?> expectedType) throws TranslatorException { if (value == null) { return null; } if (expectedType.isAssignableFrom(String.class)) { return value; } if (expectedType.isAssignableFrom(Blob.class)) { return new BlobType(new BlobImpl(new InputStreamFactory() { @Override public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(value.getBytes()); } })); } else if (expectedType.isAssignableFrom(Clob.class)) { return new ClobType(new ClobImpl(value)); } else if (expectedType.isAssignableFrom(SQLXML.class)) { return new XMLType(new SQLXMLImpl(value.getBytes())); } else if (DataTypeManager.isTransformable(String.class, expectedType)) { try { return DataTypeManager.transformValue(value, expectedType); } catch (TransformationException e) { throw new TranslatorException(e); } } else { throw new TranslatorException(ExcelPlugin.Event.TEIID23003, ExcelPlugin.Util.gs(ExcelPlugin.Event.TEIID23003, expectedType.getName())); } }
public static BlobType blobFromFile(final String file) { return new BlobType(new BlobImpl(new InputStreamFactory.FileInputStreamFactory(UnitTestUtil.getTestDataFile(file)))); }
/** * @see org.teiid.query.function.aggregate.AggregateFunction#getResult(CommandContext) */ public Object getResult(CommandContext commandContext) throws TeiidProcessingException{ if (this.result == null) { this.result = buildResult(commandContext); } try { FileStoreOutputStream fs = this.result.getOuputStream(); fs.close(); if (fs.bytesWritten()) { return new BlobType(new BlobImpl(result)); } return new BlobType(new SerialBlob(Arrays.copyOf(fs.getBuffer(), fs.getCount()))); } catch (IOException e) { throw new TeiidProcessingException(QueryPlugin.Event.TEIID30422, e); } catch (SQLException e) { throw new TeiidProcessingException(QueryPlugin.Event.TEIID30423, e); } } }
/** * @see org.teiid.query.function.aggregate.AggregateFunction#getResult(CommandContext) */ public Object getResult(CommandContext commandContext) throws TeiidProcessingException{ if (this.result == null) { this.result = buildResult(commandContext); } try { FileStoreOutputStream fs = this.result.getOuputStream(); fs.close(); if (fs.bytesWritten()) { return new BlobType(new BlobImpl(result)); } return new BlobType(new SerialBlob(Arrays.copyOf(fs.getBuffer(), fs.getCount()))); } catch (IOException e) { throw new TeiidProcessingException(QueryPlugin.Event.TEIID30422, e); } catch (SQLException e) { throw new TeiidProcessingException(QueryPlugin.Event.TEIID30423, e); } } }
/** * @see org.teiid.query.function.aggregate.AggregateFunction#getResult(CommandContext) */ public Object getResult(CommandContext commandContext) throws TeiidProcessingException{ if (this.result == null) { this.result = buildResult(commandContext); } try { FileStoreOutputStream fs = this.result.getOuputStream(); fs.close(); if (fs.bytesWritten()) { return new BlobType(new BlobImpl(result)); } return new BlobType(new SerialBlob(Arrays.copyOf(fs.getBuffer(), fs.getCount()))); } catch (IOException e) { throw new TeiidProcessingException(QueryPlugin.Event.TEIID30422, e); } catch (SQLException e) { throw new TeiidProcessingException(QueryPlugin.Event.TEIID30423, e); } } }
/** * @see org.teiid.query.function.aggregate.AggregateFunction#getResult(CommandContext) */ public Object getResult(CommandContext commandContext) throws TeiidProcessingException{ if (this.result == null) { return null; } try { this.result.getWriter().close(); FileStoreOutputStream fs = this.result.getOuputStream(); fs.close(); if (binary) { if (fs.bytesWritten()) { return new BlobType(new BlobImpl(result)); } return new BlobType(new SerialBlob(Arrays.copyOf(fs.getBuffer(), fs.getCount()))); } if (fs.bytesWritten()) { return new ClobType(new ClobImpl(result, -1)); } return new ClobType(new ClobImpl(new String(Arrays.copyOf(fs.getBuffer(), fs.getCount()), Streamable.ENCODING))); } catch (IOException e) { throw new TeiidProcessingException(QueryPlugin.Event.TEIID30422, e); } catch (SQLException e) { throw new TeiidProcessingException(QueryPlugin.Event.TEIID30423, e); } } }
public void testBlobImplGetBytes() throws Exception { BlobImpl b = new BlobImpl(new InputStreamFactory() { @Override public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(new byte[0]); } }); byte[] b1 = b.getBytes(1, 0); assertEquals(0, b1.length); byte[] b2 = b.getBytes(1, 1); assertEquals(0, b2.length); b = new BlobImpl(new InputStreamFactory() { @Override public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(new byte[]{1,2}); } }); byte[] b3 = b.getBytes(1, 1); assertEquals(1, b3.length); }