Search in sources :

Example 51 with Out

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;
            }
        }
    }
}
Also used : FileStoreOutputStream(org.h2.store.FileStoreOutputStream)

Example 52 with Out

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());
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) JavaObjectSerializer(org.h2.api.JavaObjectSerializer)

Example 53 with Out

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();
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DbException(org.h2.message.DbException) EOFException(java.io.EOFException)

Example 54 with Out

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());
        }
    }
}
Also used : Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CompressTool(org.h2.tools.CompressTool) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 55 with Out

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());
}
Also used : BitStream(org.h2.dev.util.BitStream) Out(org.h2.dev.util.BitStream.Out)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)27 IOException (java.io.IOException)23 OutputStream (java.io.OutputStream)19 ByteArrayInputStream (java.io.ByteArrayInputStream)17 SQLException (java.sql.SQLException)17 DbException (org.h2.message.DbException)17 Random (java.util.Random)11 ResultSet (java.sql.ResultSet)10 InputStream (java.io.InputStream)9 Statement (java.sql.Statement)9 Connection (java.sql.Connection)7 PrintStream (java.io.PrintStream)6 Properties (java.util.Properties)6 Task (org.h2.util.Task)6 BufferedOutputStream (java.io.BufferedOutputStream)5 File (java.io.File)5 FileOutputStream (java.io.FileOutputStream)5 InputStreamReader (java.io.InputStreamReader)5 PipedInputStream (java.io.PipedInputStream)5 OutputStreamWriter (java.io.OutputStreamWriter)4