Search in sources :

Example 1 with InputStreamFactory

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);
    }
}
Also used : TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) InputStreamFactory(org.teiid.core.types.InputStreamFactory) Timestamp(java.sql.Timestamp) GeometryType(org.teiid.core.types.GeometryType) ClobImpl(org.teiid.core.types.ClobImpl) BlobImpl(org.teiid.core.types.BlobImpl) Blob(java.sql.Blob) SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) TransformationException(org.teiid.core.types.TransformationException) BinaryType(org.teiid.core.types.BinaryType) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Date(java.sql.Date) BigInteger(java.math.BigInteger) BlobType(org.teiid.core.types.BlobType) ByteArrayInputStream(java.io.ByteArrayInputStream) BigInteger(java.math.BigInteger) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with InputStreamFactory

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);
}
Also used : Blob(java.sql.Blob) BlobType(org.teiid.core.types.BlobType) PushbackInputStream(java.io.PushbackInputStream) SQLException(java.sql.SQLException) PushbackInputStream(java.io.PushbackInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) InputStreamFactory(org.teiid.core.types.InputStreamFactory) BlobImpl(org.teiid.core.types.BlobImpl)

Example 3 with InputStreamFactory

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));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InputStreamFactory(org.teiid.core.types.InputStreamFactory) FileStoreInputStreamFactory(org.teiid.common.buffer.FileStoreInputStreamFactory) Test(org.junit.Test)

Example 4 with InputStreamFactory

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));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InputStreamFactory(org.teiid.core.types.InputStreamFactory) FileStoreInputStreamFactory(org.teiid.common.buffer.FileStoreInputStreamFactory) Test(org.junit.Test)

Example 5 with InputStreamFactory

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()));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BufferManager(org.teiid.common.buffer.BufferManager) Clob(java.sql.Clob) InputStreamFactory(org.teiid.core.types.InputStreamFactory) Test(org.junit.Test)

Aggregations

InputStreamFactory (org.teiid.core.types.InputStreamFactory)27 ByteArrayInputStream (java.io.ByteArrayInputStream)13 IOException (java.io.IOException)12 BlobImpl (org.teiid.core.types.BlobImpl)12 InputStream (java.io.InputStream)11 ClobImpl (org.teiid.core.types.ClobImpl)11 Test (org.junit.Test)10 BlobType (org.teiid.core.types.BlobType)8 SQLException (java.sql.SQLException)7 SQLXMLImpl (org.teiid.core.types.SQLXMLImpl)7 ClobType (org.teiid.core.types.ClobType)6 Blob (java.sql.Blob)5 ArrayList (java.util.ArrayList)5 FileStoreInputStreamFactory (org.teiid.common.buffer.FileStoreInputStreamFactory)4 StringReader (java.io.StringReader)3 BinaryType (org.teiid.core.types.BinaryType)3 ReaderInputStream (org.teiid.core.util.ReaderInputStream)3 N1qlQueryRow (com.couchbase.client.java.query.N1qlQueryRow)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 BigInteger (java.math.BigInteger)2