Search in sources :

Example 1 with StreamFactoryReference

use of org.teiid.core.types.InputStreamFactory.StreamFactoryReference in project teiid by teiid.

the class CompactObjectOutputStream method replaceObject.

@Override
protected Object replaceObject(Object obj) throws IOException {
    if (obj instanceof BaseLob) {
        try {
            if (obj instanceof SQLXMLImpl) {
                streams.add(((SQLXMLImpl) obj).getBinaryStream());
                StreamFactoryReference sfr = new SQLXMLImpl();
                references.add(sfr);
                return sfr;
            } else if (obj instanceof ClobImpl) {
                streams.add(new ReaderInputStream(((ClobImpl) obj).getCharacterStream(), Charset.forName(Streamable.ENCODING)));
                StreamFactoryReference sfr = new ClobImpl();
                references.add(sfr);
                return sfr;
            } else if (obj instanceof BlobImpl) {
                streams.add(((Blob) obj).getBinaryStream());
                StreamFactoryReference sfr = new BlobImpl();
                references.add(sfr);
                return sfr;
            }
        } catch (SQLException e) {
            throw new IOException(e);
        }
    } else if (obj instanceof Serializable) {
        return obj;
    } else {
        try {
            if (obj instanceof Reader) {
                streams.add(new ReaderInputStream((Reader) obj, Charset.forName(Streamable.ENCODING)));
                StreamFactoryReference sfr = new SerializableReader();
                references.add(sfr);
                return sfr;
            } else if (obj instanceof InputStream) {
                streams.add((InputStream) obj);
                StreamFactoryReference sfr = new SerializableInputStream();
                references.add(sfr);
                return sfr;
            } else if (obj instanceof SQLXML) {
                streams.add(((SQLXML) obj).getBinaryStream());
                StreamFactoryReference sfr = new SQLXMLImpl();
                references.add(sfr);
                return sfr;
            } else if (obj instanceof Clob) {
                streams.add(new ReaderInputStream(((Clob) obj).getCharacterStream(), Charset.forName(Streamable.ENCODING)));
                StreamFactoryReference sfr = new ClobImpl();
                references.add(sfr);
                return sfr;
            } else if (obj instanceof Blob) {
                streams.add(((Blob) obj).getBinaryStream());
                StreamFactoryReference sfr = new BlobImpl();
                references.add(sfr);
                return sfr;
            }
        } catch (SQLException e) {
            throw new IOException(e);
        }
    }
    return super.replaceObject(obj);
}
Also used : Blob(java.sql.Blob) SQLException(java.sql.SQLException) ReaderInputStream(org.teiid.core.util.ReaderInputStream) ReaderInputStream(org.teiid.core.util.ReaderInputStream) SQLXML(java.sql.SQLXML) StreamFactoryReference(org.teiid.core.types.InputStreamFactory.StreamFactoryReference) Clob(java.sql.Clob)

Example 2 with StreamFactoryReference

use of org.teiid.core.types.InputStreamFactory.StreamFactoryReference in project teiid by teiid.

the class ObjectDecoder method decode.

@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception {
    if (result == null) {
        ByteBuf frame = null;
        try {
            frame = (ByteBuf) super.decode(ctx, buffer);
        } catch (TooLongFrameException e) {
            throw new IOException(RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40166), e);
        }
        if (frame == null) {
            return null;
        }
        CompactObjectInputStream cois = new CompactObjectInputStream(new ByteBufInputStream(frame), classLoader);
        result = cois.readObject();
        streams = ExternalizeUtil.readList(cois, StreamFactoryReference.class);
        streamIndex = 0;
    }
    while (streamIndex < streams.size()) {
        // read the new chunk size
        if (streamDataToRead == -1) {
            if (buffer.readableBytes() < 2) {
                return null;
            }
            streamDataToRead = buffer.readUnsignedShort();
        }
        if (stream == null) {
            // $NON-NLS-1$
            store = storageManager.createFileStore("temp-stream");
            StreamFactoryReference sfr = streams.get(streamIndex);
            sfr.setStreamFactory(new FileStoreInputStreamFactory(store, Streamable.ENCODING));
            this.stream = new BufferedOutputStream(store.createOutputStream());
        }
        // end of stream
        if (streamDataToRead == 0) {
            stream.close();
            stream = null;
            streamIndex++;
            streamDataToRead = -1;
            continue;
        }
        if (store.getLength() + streamDataToRead > maxLobSize) {
            if (error == null) {
                error = new StreamCorruptedException(// $NON-NLS-1$ //$NON-NLS-2$
                "lob too big: " + (store.getLength() + streamDataToRead) + " (max: " + maxLobSize + ')');
            }
        }
        int toRead = Math.min(buffer.readableBytes(), streamDataToRead);
        if (toRead == 0) {
            return null;
        }
        if (error == null) {
            buffer.readBytes(this.stream, toRead);
        } else {
            buffer.skipBytes(toRead);
        }
        streamDataToRead -= toRead;
        if (streamDataToRead == 0) {
            // get the next chunk
            streamDataToRead = -1;
        }
    }
    Object toReturn = result;
    result = null;
    streams = null;
    stream = null;
    store = null;
    if (error != null) {
        StreamCorruptedException sce = error;
        error = null;
        throw sce;
    }
    return toReturn;
}
Also used : CompactObjectInputStream(org.teiid.netty.handler.codec.serialization.CompactObjectInputStream) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) FileStoreInputStreamFactory(org.teiid.common.buffer.FileStoreInputStreamFactory) StreamCorruptedException(java.io.StreamCorruptedException) IOException(java.io.IOException) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) StreamFactoryReference(org.teiid.core.types.InputStreamFactory.StreamFactoryReference) ByteBuf(io.netty.buffer.ByteBuf) BufferedOutputStream(java.io.BufferedOutputStream)

Example 3 with StreamFactoryReference

use of org.teiid.core.types.InputStreamFactory.StreamFactoryReference in project teiid by teiid.

the class ObjectDecoderInputStream method readObjectOverride.

@Override
protected final Object readObjectOverride() throws IOException, ClassNotFoundException {
    if (result == null) {
        if (!foundLength) {
            clearRemaining();
            remaining = dis.readInt();
            foundLength = true;
            if (remaining <= 0) {
                // $NON-NLS-1$
                throw new StreamCorruptedException("invalid data length: " + remaining);
            }
            if (remaining > maxObjectSize) {
                throw new StreamCorruptedException(JDBCPlugin.Util.gs(JDBCPlugin.Event.TEIID20028, remaining, maxObjectSize));
            }
        }
        foundLength = false;
        CompactObjectInputStream cois = new CompactObjectInputStream(subStream, classLoader);
        result = cois.readObject();
        streams = ExternalizeUtil.readList(cois, StreamFactoryReference.class);
        streamIndex = 0;
    }
    while (streamIndex < streams.size()) {
        if (!foundLength) {
            clearRemaining();
            // convert to unsigned
            remaining = 0xffff & dis.readShort();
            foundLength = true;
            if (remaining < 0) {
                // $NON-NLS-1$
                throw new StreamCorruptedException("Invalid stream chunk length");
            }
        }
        if (stream == null) {
            // $NON-NLS-1$
            final File f = File.createTempFile("teiid", null);
            StreamFactoryReference sfr = streams.get(streamIndex);
            sfr.setStreamFactory(new InputStreamFactory() {

                @Override
                public InputStream getInputStream() throws IOException {
                    return new BufferedInputStream(new FileInputStream(f));
                }

                @Override
                protected void finalize() throws Throwable {
                    super.finalize();
                    f.delete();
                }
            });
            this.stream = new FileOutputStream(f);
        }
        foundLength = false;
        if (remaining != 0) {
            int available = Math.min(remaining, in.getCount() - in.getPosition());
            if (available > 0) {
                this.stream.write(in.getBuffer(), in.getPosition(), available);
                in.setPosition(in.getPosition() + available);
                remaining -= available;
            }
            if (remaining > 0) {
                ObjectConverterUtil.write(this.stream, in, in.getBuffer(), remaining, false);
                remaining = 0;
            }
            continue;
        }
        stream.close();
        stream = null;
        streamIndex++;
    }
    Object toReturn = result;
    result = null;
    streams = null;
    stream = null;
    return toReturn;
}
Also used : AccessibleBufferedInputStream(org.teiid.core.util.AccessibleBufferedInputStream) InputStreamFactory(org.teiid.core.types.InputStreamFactory) AccessibleBufferedInputStream(org.teiid.core.util.AccessibleBufferedInputStream) StreamFactoryReference(org.teiid.core.types.InputStreamFactory.StreamFactoryReference)

Aggregations

StreamFactoryReference (org.teiid.core.types.InputStreamFactory.StreamFactoryReference)3 ByteBuf (io.netty.buffer.ByteBuf)1 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)1 TooLongFrameException (io.netty.handler.codec.TooLongFrameException)1 BufferedOutputStream (java.io.BufferedOutputStream)1 IOException (java.io.IOException)1 StreamCorruptedException (java.io.StreamCorruptedException)1 Blob (java.sql.Blob)1 Clob (java.sql.Clob)1 SQLException (java.sql.SQLException)1 SQLXML (java.sql.SQLXML)1 FileStoreInputStreamFactory (org.teiid.common.buffer.FileStoreInputStreamFactory)1 InputStreamFactory (org.teiid.core.types.InputStreamFactory)1 AccessibleBufferedInputStream (org.teiid.core.util.AccessibleBufferedInputStream)1 ReaderInputStream (org.teiid.core.util.ReaderInputStream)1 CompactObjectInputStream (org.teiid.netty.handler.codec.serialization.CompactObjectInputStream)1