use of org.h2.value.ValueClob in project SpringStudy by myounghaklee.
the class ValueClob 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 ValueClob 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 {
long remaining = Long.MAX_VALUE;
if (length >= 0 && length < remaining) {
remaining = length;
}
int len = ValueLob.getBufferSize(handler, 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()) {
return ValueClob.createSmall(new String(buff, 0, len));
}
reader.reset();
return createTemporary(handler, reader, remaining);
} catch (IOException e) {
throw DbException.convertIOException(e, null);
}
}
use of org.h2.value.ValueClob in project 468H2Project by lukeunderwood42.
the class ValueClob method copy.
@Override
public ValueLob copy(DataHandler database, int tableId) {
if (lobData instanceof LobDataInMemory) {
byte[] small = ((LobDataInMemory) lobData).getSmall();
if (small.length > database.getMaxLengthInplaceLob()) {
LobStorageInterface s = database.getLobStorage();
ValueClob v = s.createClob(getReader(), charLength);
ValueLob v2 = v.copy(database, tableId);
v.remove();
return v2;
}
return this;
} else if (lobData instanceof LobDataDatabase) {
return database.getLobStorage().copyLob(this, tableId);
} else {
throw new UnsupportedOperationException();
}
}
use of org.h2.value.ValueClob in project 468H2Project by lukeunderwood42.
the class ValueClob method compare.
/**
* Compares two CLOB values directly.
*
* @param v1
* first CLOB value
* @param v2
* second CLOB value
* @return result of comparison
*/
private static int compare(ValueClob v1, ValueClob v2) {
long minPrec = Math.min(v1.charLength, v2.charLength);
try (Reader reader1 = v1.getReader();
Reader reader2 = v2.getReader()) {
char[] buf1 = new char[BLOCK_COMPARISON_SIZE];
char[] buf2 = new char[BLOCK_COMPARISON_SIZE];
for (; minPrec >= BLOCK_COMPARISON_SIZE; minPrec -= BLOCK_COMPARISON_SIZE) {
if (IOUtils.readFully(reader1, buf1, BLOCK_COMPARISON_SIZE) != BLOCK_COMPARISON_SIZE || IOUtils.readFully(reader2, buf2, BLOCK_COMPARISON_SIZE) != BLOCK_COMPARISON_SIZE) {
throw DbException.getUnsupportedException("Invalid LOB");
}
int cmp = Bits.compareNotNull(buf1, buf2);
if (cmp != 0) {
return cmp;
}
}
for (; ; ) {
int c1 = reader1.read(), c2 = reader2.read();
if (c1 < 0) {
return c2 < 0 ? 0 : -1;
}
if (c2 < 0) {
return 1;
}
if (c1 != c2) {
return c1 < c2 ? -1 : 1;
}
}
} catch (IOException ex) {
throw DbException.convert(ex);
}
}
use of org.h2.value.ValueClob in project 468H2Project by lukeunderwood42.
the class ValueClob method convertPrecision.
/**
* Convert the precision to the requested value.
*
* @param precision
* the new precision
* @return the truncated or this value
*/
ValueClob convertPrecision(long precision) {
if (this.charLength <= precision) {
return this;
}
ValueClob lob;
DataHandler handler = lobData.getDataHandler();
if (handler != null) {
lob = createTempClob(getReader(), precision, handler);
} else {
try {
lob = createSmall(IOUtils.readStringAndClose(getReader(), MathUtils.convertLongToInt(precision)));
} catch (IOException e) {
throw DbException.convertIOException(e, null);
}
}
return lob;
}
use of org.h2.value.ValueClob in project 468H2Project by lukeunderwood42.
the class ValueClob method createTemporary.
/**
* Create a CLOB in a temporary file.
*/
private static ValueClob createTemporary(DataHandler handler, Reader in, long remaining) throws IOException {
String fileName = ValueLob.createTempLobFileName(handler);
FileStore tempFile = handler.openFile(fileName, "rw", false);
tempFile.autoDelete();
long octetLength = 0L, charLength = 0L;
try (FileStoreOutputStream out = new FileStoreOutputStream(tempFile, null)) {
char[] buff = new char[Constants.IO_BUFFER_SIZE];
while (true) {
int len = ValueLob.getBufferSize(handler, remaining);
len = IOUtils.readFully(in, buff, len);
if (len == 0) {
break;
}
// TODO reduce memory allocation
byte[] data = new String(buff, 0, len).getBytes(StandardCharsets.UTF_8);
out.write(data);
octetLength += data.length;
charLength += len;
}
}
return new ValueClob(new LobDataFile(handler, fileName, tempFile), octetLength, charLength);
}
Aggregations