use of org.h2.dev.util.BitStream.Out in project h2database by h2database.
the class ValueLob method createFromStream.
private void createFromStream(byte[] buff, int len, InputStream in, long remaining, DataHandler h) throws IOException {
try (FileStoreOutputStream out = initLarge(h)) {
boolean compress = h.getLobCompressionAlgorithm(Value.BLOB) != null;
while (true) {
precision += len;
out.write(buff, 0, len);
remaining -= len;
if (remaining <= 0) {
break;
}
len = getBufferSize(h, compress, remaining);
len = IOUtils.readFully(in, buff, len);
if (len <= 0) {
break;
}
}
}
}
use of org.h2.dev.util.BitStream.Out in project h2database by h2database.
the class JdbcUtils method serialize.
/**
* Serialize the object to a byte array, using the serializer specified by
* the connection info if set, or the default serializer.
*
* @param obj the object to serialize
* @param dataHandler provides the object serializer (may be null)
* @return the byte array
*/
public static byte[] serialize(Object obj, DataHandler dataHandler) {
try {
JavaObjectSerializer handlerSerializer = null;
if (dataHandler != null) {
handlerSerializer = dataHandler.getJavaObjectSerializer();
}
if (handlerSerializer != null) {
return handlerSerializer.serialize(obj);
}
if (serializer != null) {
return serializer.serialize(obj);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(out);
os.writeObject(obj);
return out.toByteArray();
} catch (Throwable e) {
throw DbException.get(ErrorCode.SERIALIZATION_FAILED_1, e, e.toString());
}
}
use of org.h2.dev.util.BitStream.Out in project h2database by h2database.
the class IOUtils method readBytesAndClose.
/**
* Read a number of bytes from an input stream and close the stream.
*
* @param in the input stream
* @param length the maximum number of bytes to read, or -1 to read until
* the end of file
* @return the bytes read
*/
public static byte[] readBytesAndClose(InputStream in, int length) throws IOException {
try {
if (length <= 0) {
length = Integer.MAX_VALUE;
}
int block = Math.min(Constants.IO_BUFFER_SIZE, length);
ByteArrayOutputStream out = new ByteArrayOutputStream(block);
copy(in, out, length);
return out.toByteArray();
} catch (Exception e) {
throw DbException.convertToIOException(e);
} finally {
in.close();
}
}
use of org.h2.dev.util.BitStream.Out in project h2database by h2database.
the class TestCompress method testByteArray.
private void testByteArray(int len) throws IOException {
Random r = new Random(len);
for (int pattern = 0; pattern < 4; pattern++) {
byte[] b = new byte[len];
switch(pattern) {
case 0:
// leave empty
break;
case 1:
{
r.nextBytes(b);
break;
}
case 2:
{
for (int x = 0; x < len; x++) {
b[x] = (byte) (x & 10);
}
break;
}
case 3:
{
for (int x = 0; x < len; x++) {
b[x] = (byte) (x / 10);
}
break;
}
default:
}
if (r.nextInt(2) < 1) {
for (int x = 0; x < len; x++) {
if (r.nextInt(20) < 1) {
b[x] = (byte) (r.nextInt(255));
}
}
}
CompressTool utils = CompressTool.getInstance();
// level 9 is highest, strategy 2 is huffman only
for (String a : new String[] { "LZF", "No", "Deflate", "Deflate level 9 strategy 2" }) {
long time = System.nanoTime();
byte[] out = utils.compress(b, a);
byte[] test = utils.expand(out);
if (testPerformance) {
System.out.println("p:" + pattern + " len: " + out.length + " time: " + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - time) + " " + a);
}
assertEquals(b.length, test.length);
assertEquals(b, test);
Arrays.fill(test, (byte) 0);
CompressTool.expand(out, test, 0);
assertEquals(b, test);
}
for (String a : new String[] { null, "LZF", "DEFLATE", "ZIP", "GZIP" }) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStream out2 = CompressTool.wrapOutputStream(out, a, "test");
IOUtils.copy(new ByteArrayInputStream(b), out2);
out2.close();
InputStream in = new ByteArrayInputStream(out.toByteArray());
in = CompressTool.wrapInputStream(in, a, "test");
out.reset();
IOUtils.copy(in, out);
assertEquals(b, out.toByteArray());
}
}
}
use of org.h2.dev.util.BitStream.Out in project h2database by h2database.
the class TestBitStream method testHuffman.
private void testHuffman() {
int[] freq = { 36, 18, 12, 9, 7, 6, 5, 4 };
BitStream.Huffman huff = new BitStream.Huffman(freq);
final StringBuilder buff = new StringBuilder();
Out o = new Out(null) {
@Override
public void writeBit(int bit) {
buff.append(bit == 0 ? '0' : '1');
}
};
for (int i = 0; i < freq.length; i++) {
buff.append(i + ": ");
huff.write(o, i);
buff.append("\n");
}
assertEquals("0: 0\n" + "1: 110\n" + "2: 100\n" + "3: 1110\n" + "4: 1011\n" + "5: 1010\n" + "6: 11111\n" + "7: 11110\n", buff.toString());
}
Aggregations