KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ .Q.gz – Not in Gzip format in java

  • pgyorok

    Member
    September 27, 2024 at 9:12 am
  • rocuinneagain

    Member
    September 27, 2024 at 10:36 am

    The question is vague on what you are trying to do and how (no example code etc.)

    Below is an example which when run results in:

    HelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHello

    Java can decompress the data which kdb+ compressed with .Q.gz without issue:

    import java.io.IOException;
    import java.nio.charset.StandardCharsets;
    import java.util.Arrays;
    import java.util.zip.*;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.lang.String;
    import com.kx.c;
    public class Main {
        public static String decompress(final byte[] bytes) throws IOException {
            if (bytes == null || bytes.length == 0) {
                return "";
            }
            try (final GZIPInputStream ungzip = new GZIPInputStream(new ByteArrayInputStream(bytes))) {
                final ByteArrayOutputStream out = new ByteArrayOutputStream();
                final byte[] data = new byte[8192];
                int nRead;
                while ((nRead = ungzip.read(data)) != -1) {
                    out.write(data, 0, nRead);
                }
                return out.toString();
            }
        }
        public static void main(String[] args) {
            c c = null;
            Object result = null;
            byte[] bytes = null;
            try {
                c = new c("localhost", 5001);
                String query="`byte$.Q.gz(9;400#"Hello")";
                result = c.k(query);
                bytes = ((byte[]) result);
                System.out.println(decompress(bytes));
            } catch (Exception e){
                System.err.println(e.toString());
            }
        }
    }

Log in to reply.