use of org.teiid.core.types.InputStreamFactory 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.InputStreamFactory 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.InputStreamFactory in project teiid by teiid.
the class TestSaveOnReadInputStream method testStorageMode.
@Test
public void testStorageMode() throws IOException {
SaveOnReadInputStream soris = getSaveOnReadInputStream();
InputStreamFactory isf = soris.getInputStreamFactory();
assertEquals(StorageMode.MEMORY, isf.getStorageMode());
InputStream is = isf.getInputStream();
assertEquals("hello world", new String(ObjectConverterUtil.convertToByteArray(is), Streamable.CHARSET));
}
use of org.teiid.core.types.InputStreamFactory in project teiid by teiid.
the class TestSaveOnReadInputStream method testPartialReadSave.
@Test
public void testPartialReadSave() throws IOException {
SaveOnReadInputStream soris = getSaveOnReadInputStream();
InputStreamFactory isf = soris.getInputStreamFactory();
InputStream is = isf.getInputStream();
is.read();
InputStream is2 = isf.getInputStream();
assertEquals("ello world", new String(ObjectConverterUtil.convertToByteArray(is), Streamable.CHARSET));
assertEquals("hello world", new String(ObjectConverterUtil.convertToByteArray(is2), Streamable.CHARSET));
InputStream is3 = isf.getInputStream();
assertEquals("hello world", new String(ObjectConverterUtil.convertToByteArray(is3), Streamable.CHARSET));
}
use of org.teiid.core.types.InputStreamFactory in project teiid by teiid.
the class TestConnectorWorkItem method testTypeConversionClob.
@Test
public void testTypeConversionClob() throws Exception {
BufferManager bm = BufferManagerFactory.getStandaloneBufferManager();
String str = "hello world";
Clob clob = (Clob) ConnectorWorkItem.convertToRuntimeType(bm, new InputStreamFactory() {
@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(str.getBytes(Streamable.CHARSET));
}
}, DataTypeManager.DefaultDataClasses.CLOB, null);
assertEquals(str, clob.getSubString(1, str.length()));
}
Aggregations