Search in sources :

Example 1 with FatalError

use of io.questdb.std.ex.FatalError in project questdb by bluestreak01.

the class ZipTest method testGzip.

@Test
public void testGzip() throws Exception {
    try (Path path = new Path()) {
        File outFile = temp.newFile("x");
        File expected = new File(ZipTest.class.getResource("/zip-test/large.csv").getFile());
        final int available = 64 * 1024;
        long in = Unsafe.malloc(available, MemoryTag.NATIVE_DEFAULT);
        long out = Unsafe.malloc(available / 2, MemoryTag.NATIVE_DEFAULT);
        try {
            long strm = Zip.deflateInit();
            try {
                long pIn = 0;
                long pOut = 0;
                long fdIn = Files.openRO(path.of(expected.getAbsolutePath()).$());
                try {
                    long fdOut = Files.openRW(path.of(outFile.getAbsolutePath()).$());
                    try {
                        // header
                        Files.write(fdOut, Zip.gzipHeader, Zip.gzipHeaderLen, pOut);
                        pOut += Zip.gzipHeaderLen;
                        int len;
                        int crc = 0;
                        while ((len = (int) Files.read(fdIn, in, available, pIn)) > 0) {
                            pIn += len;
                            Zip.setInput(strm, in, len);
                            crc = Zip.crc32(crc, in, len);
                            do {
                                int ret;
                                if ((ret = Zip.deflate(strm, out, available, false)) < 0) {
                                    throw new FatalError("Error in deflator: " + ret);
                                }
                                int have = available - Zip.availOut(strm);
                                if (have > 0) {
                                    Files.write(fdOut, out, have, pOut);
                                    pOut += have;
                                }
                            } while (Zip.availIn(strm) > 0);
                        }
                        int ret;
                        do {
                            if ((ret = Zip.deflate(strm, out, available, true)) < 0) {
                                throw new FatalError("Error in deflator: " + ret);
                            }
                            int have = available - Zip.availOut(strm);
                            if (have > 0) {
                                Files.write(fdOut, out, have, pOut);
                                pOut += have;
                            }
                        } while (ret != 1);
                        // write trailer
                        Unsafe.getUnsafe().putInt(out, crc);
                        Unsafe.getUnsafe().putInt(out + 4, (int) pIn);
                        Files.write(fdOut, out, 8, pOut);
                    } finally {
                        Files.close(fdOut);
                    }
                } finally {
                    Files.close(fdIn);
                }
            } finally {
                Zip.deflateEnd(strm);
            }
        } finally {
            Unsafe.free(in, available, MemoryTag.NATIVE_DEFAULT);
            Unsafe.free(out, available / 2, MemoryTag.NATIVE_DEFAULT);
        }
        // ok. read what we produced
        File actual = temp.newFile();
        try (GZIPInputStream is = new GZIPInputStream(new FileInputStream(outFile));
            FileOutputStream fos = new FileOutputStream(actual)) {
            byte[] buf = new byte[16 * 1024];
            int l;
            while ((l = is.read(buf)) > 0) {
                fos.write(buf, 0, l);
            }
        }
        TestUtils.assertEquals(expected, actual);
    }
}
Also used : Path(io.questdb.std.str.Path) GZIPInputStream(java.util.zip.GZIPInputStream) FatalError(io.questdb.std.ex.FatalError) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 2 with FatalError

use of io.questdb.std.ex.FatalError in project questdb by bluestreak01.

the class Os method loadLib.

private static void loadLib(String lib) {
    InputStream is = Os.class.getResourceAsStream(lib);
    if (is == null) {
        throw new FatalError("Internal error: cannot find " + lib + ", broken package?");
    }
    try {
        File tempLib = null;
        try {
            int dot = lib.indexOf('.');
            tempLib = File.createTempFile(lib.substring(0, dot), lib.substring(dot));
            // copy to tempLib
            try (FileOutputStream out = new FileOutputStream(tempLib)) {
                byte[] buf = new byte[4096];
                while (true) {
                    int read = is.read(buf);
                    if (read == -1) {
                        break;
                    }
                    out.write(buf, 0, read);
                }
            } finally {
                tempLib.deleteOnExit();
            }
            System.load(tempLib.getAbsolutePath());
        } catch (IOException e) {
            throw new FatalError("Internal error: cannot unpack " + tempLib, e);
        }
    } finally {
        Misc.free(is);
    }
}
Also used : FatalError(io.questdb.std.ex.FatalError) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File)

Aggregations

FatalError (io.questdb.std.ex.FatalError)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 Path (io.questdb.std.str.Path)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 Test (org.junit.Test)1