Search in sources :

Example 1 with DataHandler

use of org.h2.store.DataHandler in project h2database by h2database.

the class ValueLobDb method createTempClob.

/**
 * Create a temporary CLOB value from a stream.
 *
 * @param in the reader
 * @param length the number of characters to read, or -1 for no limit
 * @param handler the data handler
 * @return the lob value
 */
public static ValueLobDb createTempClob(Reader in, long length, DataHandler handler) {
    if (length >= 0) {
        // blocks the network level
        try {
            in = new RangeReader(in, 0, length);
        } catch (IOException e) {
            throw DbException.convert(e);
        }
    }
    BufferedReader reader;
    if (in instanceof BufferedReader) {
        reader = (BufferedReader) in;
    } else {
        reader = new BufferedReader(in, Constants.IO_BUFFER_SIZE);
    }
    try {
        boolean compress = handler.getLobCompressionAlgorithm(Value.CLOB) != null;
        long remaining = Long.MAX_VALUE;
        if (length >= 0 && length < remaining) {
            remaining = length;
        }
        int len = getBufferSize(handler, compress, remaining);
        char[] buff;
        if (len >= Integer.MAX_VALUE) {
            String data = IOUtils.readStringAndClose(reader, -1);
            buff = data.toCharArray();
            len = buff.length;
        } else {
            buff = new char[len];
            reader.mark(len);
            len = IOUtils.readFully(reader, buff, len);
        }
        if (len <= handler.getMaxLengthInplaceLob()) {
            byte[] small = new String(buff, 0, len).getBytes(StandardCharsets.UTF_8);
            return ValueLobDb.createSmallLob(Value.CLOB, small, len);
        }
        reader.reset();
        return new ValueLobDb(handler, reader, remaining);
    } catch (IOException e) {
        throw DbException.convertIOException(e, null);
    }
}
Also used : RangeReader(org.h2.store.RangeReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException)

Example 2 with DataHandler

use of org.h2.store.DataHandler in project h2database by h2database.

the class JdbcUtils method deserialize.

/**
 * De-serialize the byte array to an object, eventually using the serializer
 * specified by the connection info.
 *
 * @param data the byte array
 * @param dataHandler provides the object serializer (may be null)
 * @return the object
 * @throws DbException if serialization fails
 */
public static Object deserialize(byte[] data, DataHandler dataHandler) {
    try {
        JavaObjectSerializer dbJavaObjectSerializer = null;
        if (dataHandler != null) {
            dbJavaObjectSerializer = dataHandler.getJavaObjectSerializer();
        }
        if (dbJavaObjectSerializer != null) {
            return dbJavaObjectSerializer.deserialize(data);
        }
        if (serializer != null) {
            return serializer.deserialize(data);
        }
        ByteArrayInputStream in = new ByteArrayInputStream(data);
        ObjectInputStream is;
        if (SysProperties.USE_THREAD_CONTEXT_CLASS_LOADER) {
            final ClassLoader loader = Thread.currentThread().getContextClassLoader();
            is = new ObjectInputStream(in) {

                @Override
                protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
                    try {
                        return Class.forName(desc.getName(), true, loader);
                    } catch (ClassNotFoundException e) {
                        return super.resolveClass(desc);
                    }
                }
            };
        } else {
            is = new ObjectInputStream(in);
        }
        return is.readObject();
    } catch (Throwable e) {
        throw DbException.get(ErrorCode.DESERIALIZATION_FAILED_1, e, e.toString());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectStreamClass(java.io.ObjectStreamClass) IOException(java.io.IOException) ObjectStreamClass(java.io.ObjectStreamClass) JavaObjectSerializer(org.h2.api.JavaObjectSerializer) ObjectInputStream(java.io.ObjectInputStream)

Example 3 with DataHandler

use of org.h2.store.DataHandler in project h2database by h2database.

the class Recover method readBlobDb.

/**
 * INTERNAL
 */
public static Value.ValueBlob readBlobDb(Connection conn, long lobId, long precision) {
    DataHandler h = ((JdbcConnection) conn).getSession().getDataHandler();
    verifyPageStore(h);
    ValueLobDb lob = ValueLobDb.create(Value.BLOB, h, LobStorageFrontend.TABLE_TEMP, lobId, null, precision);
    lob.setRecoveryReference(true);
    return lob;
}
Also used : ValueLobDb(org.h2.value.ValueLobDb) DataHandler(org.h2.store.DataHandler)

Example 4 with DataHandler

use of org.h2.store.DataHandler in project h2database by h2database.

the class Recover method readClobDb.

/**
 * INTERNAL
 */
public static Value.ValueClob readClobDb(Connection conn, long lobId, long precision) {
    DataHandler h = ((JdbcConnection) conn).getSession().getDataHandler();
    verifyPageStore(h);
    ValueLobDb lob = ValueLobDb.create(Value.CLOB, h, LobStorageFrontend.TABLE_TEMP, lobId, null, precision);
    lob.setRecoveryReference(true);
    return lob;
}
Also used : ValueLobDb(org.h2.value.ValueLobDb) DataHandler(org.h2.store.DataHandler)

Example 5 with DataHandler

use of org.h2.store.DataHandler in project h2database by h2database.

the class ValueLob method createFromReader.

private void createFromReader(char[] buff, int len, Reader in, long remaining, DataHandler h) throws IOException {
    try (FileStoreOutputStream out = initLarge(h)) {
        boolean compress = h.getLobCompressionAlgorithm(Value.CLOB) != null;
        while (true) {
            precision += len;
            byte[] b = new String(buff, 0, len).getBytes(StandardCharsets.UTF_8);
            out.write(b, 0, b.length);
            remaining -= len;
            if (remaining <= 0) {
                break;
            }
            len = getBufferSize(h, compress, remaining);
            len = IOUtils.readFully(in, buff, len);
            if (len == 0) {
                break;
            }
        }
    }
}
Also used : FileStoreOutputStream(org.h2.store.FileStoreOutputStream)

Aggregations

IOException (java.io.IOException)2 JavaObjectSerializer (org.h2.api.JavaObjectSerializer)2 DataHandler (org.h2.store.DataHandler)2 FileStoreOutputStream (org.h2.store.FileStoreOutputStream)2 ValueLobDb (org.h2.value.ValueLobDb)2 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 ObjectStreamClass (java.io.ObjectStreamClass)1 RangeReader (org.h2.store.RangeReader)1