Search in sources :

Example 1 with RangeReader

use of org.h2.store.RangeReader 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 RangeReader

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

the class JdbcClob method setString.

/**
 * Fills the Clob. This is only supported for new, empty Clob objects that
 * were created with Connection.createClob() or createNClob(). The position
 * must be 1, meaning the whole Clob data is set.
 *
 * @param pos where to start writing (the first character is at position 1)
 * @param str the string to add
 * @param offset the string offset
 * @param len the number of characters to read
 * @return the length of the added text
 */
@Override
public int setString(long pos, String str, int offset, int len) throws SQLException {
    try {
        if (isDebugEnabled()) {
            debugCode("setString(" + pos + ", " + quote(str) + ", " + offset + ", " + len + ");");
        }
        checkClosed();
        if (pos != 1) {
            throw DbException.getInvalidValueException("pos", pos);
        } else if (str == null) {
            throw DbException.getInvalidValueException("str", str);
        }
        value = conn.createClob(new RangeReader(new StringReader(str), offset, len), -1);
        return (int) value.getPrecision();
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : RangeReader(org.h2.store.RangeReader) StringReader(java.io.StringReader) IOException(java.io.IOException) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Aggregations

IOException (java.io.IOException)2 RangeReader (org.h2.store.RangeReader)2 BufferedReader (java.io.BufferedReader)1 StringReader (java.io.StringReader)1 SQLException (java.sql.SQLException)1 DbException (org.h2.message.DbException)1