com.kx.c Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-kdb-communication Show documentation
Show all versions of java-kdb-communication Show documentation
Java library to publish data into and query data out of kdb+ processes (c) 2017 Sport Trades Ltd
The newest version!
package com.kx; //jar cf c.jar kx/*.class
import java.net.*;import javax.net.ssl.*;import java.io.*;import java.sql.*;import java.lang.reflect.Array;import java.text.*;import java.util.UUID;
/*
Regarding SSL/TLS: To import a servers certificate into your keystore
keytool -printcert -rfc -sslserver localhost:5010 > example.pem
keytool -importcert -file example.pem -alias example.com -storepass changeit -keystore ./keystore
java -Djavax.net.ssl.trustStore=./keystore -Djavax.net.ssl.keystore=./keystore kx.c
To debug ssl, supply -Djavax.net.debug=ssl on cmd line
*/
@SuppressWarnings("all")
public class c{
private static String e="UTF-8";private static PrintStream out=System.out;private int sync=0;
public static void setEncoding(String e)throws UnsupportedEncodingException{c.e=e;out=new PrintStream(System.out,true,e);}
public Socket s;DataInputStream i;OutputStream o;byte[]b,B;int j,J,vt;boolean a,l,zip;
public void zip(boolean b){zip=b;}
void io(Socket x)throws IOException{s=x;s.setTcpNoDelay(true);{InetAddress a=s.getInetAddress();l=a.isAnyLocalAddress()||a.isLoopbackAddress();}i=new DataInputStream(s.getInputStream());o=s.getOutputStream();s.setKeepAlive(true);}public void close()throws IOException{if(null!=s){s.close();s=null;};if(null!=i){i.close();i=null;}if(null!=o){o.close();o=null;}}
public interface IAuthenticate{public boolean authenticate(String s);}
public c(ServerSocket s,IAuthenticate a)throws IOException{io(s.accept());int n=i.read(b=new byte[99]);if(a!=null&&!a.authenticate(new String(b,0,n>1?n-2:0))){close();throw new IOException("access");}vt=n>1?b[n-2]:0;b[0]=(byte)(vt<'\3'?vt:'\3');o.write(b,0,1);}
public c(ServerSocket s)throws IOException{this(s,null);}
public c(String h,int p,String u)throws KException,IOException{this(h,p,u,false);}
public c(String h,int p,String u,boolean useTLS)throws KException,IOException{B=new byte[2+ns(u)];s=new Socket(h,p);
if(useTLS){s=((SSLSocketFactory)SSLSocketFactory.getDefault()).createSocket(s,h,p,true);((SSLSocket)s).startHandshake();}
io(s);J=0;w(u+"\3");o.write(B);if(1!=i.read(B,0,1)){close();B=new byte[1+ns(u)];io(new Socket(h,p));J=0;w(u);o.write(B);if(1!=i.read(B,0,1)){close();throw new KException("access");}}vt=Math.min(B[0],3);}
public c(String h,int p)throws KException,IOException{this(h,p,System.getProperty("user.name"));}
protected c(){};
public static class Month implements Comparable{public int i;public Month(int x){i=x;}public String toString(){int m=i+24000,y=m/12;return i==ni?"":i2(y/100)+i2(y%100)+"-"+i2(1+m%12);}public boolean equals(final Object o){return(o instanceof Month)?((Month)o).i==i:false;}public int hashCode(){return i;}public int compareTo(Month m){return i-m.i;}}
public static class Minute implements Comparable{public int i;public Minute(int x){i=x;}public String toString(){return i==ni?"":i2(i/60)+":"+i2(i%60);}
public boolean equals(final Object o){return(o instanceof Minute)?((Minute)o).i==i:false;}public int hashCode(){return i;}public int compareTo(Minute m){return i-m.i;}}
public static class Second implements Comparable{public int i;public Second(int x){i=x;}public String toString(){return i==ni?"":new Minute(i/60).toString()+':'+i2(i%60);}public boolean equals(final Object o){return(o instanceof Second)?((Second)o).i==i:false;}public int hashCode(){return i;}public int compareTo(Second s){return i-s.i;}}
public static class Timespan implements Comparable{public long j;public Timespan(long x){j=x;}
public String toString(){if(j==nj)return "";String s=j<0?"-":"";long jj=j<0?-j:j;int d=((int)(jj/86400000000000L));if(d!=0)s+=d+"D";return s+i2((int)((jj%86400000000000L)/3600000000000L))+":"+i2((int)((jj%3600000000000L)/60000000000L))+":"+i2((int)((jj%60000000000L)/1000000000L))+"."+i9((int)(jj%1000000000L));}public int compareTo(Timespan t){return j>t.j?1:j>>32));}}
public static class Dict{public Object x;public Object y;public Dict(Object X,Object Y){x=X;y=Y;}}
public static class Flip{public String[]x;public Object[]y;public Flip(Dict X){x=(String[])X.x;y=(Object[])X.y;}public Object at(String s){return y[find(x,s)];}}
public static class KException extends Exception{KException(String s){super(s);}}
private void z(){byte i=0;boolean g;int j=J,f=0,h0=0,h=0;byte[]y=B;B=new byte[y.length/2];int c=12,d=c,e=B.length,p=0,q,r,s0=0,s=8,t=J,a[]=new int[256];System.arraycopy(y,0,B,0,4);B[2]=1;J=8;w(j);for(;se-17){J=j;B=y;return;}i=1;B[c]=(byte)f;c=d++;f=0;}g=(s>t-3)||(0==(p=a[h=0xFF&(y[s]^y[s+1])]))||(0!=(y[s]^y[p]));if(0>8));w((byte)h);}
int ri(){int x=rh(),y=rh();return a?x&0xffff|y<<16:x<<16|y&0xffff;} void w(int i){w((short)(i>>16));w((short)i);}
UUID rg(){boolean oa=a;a=false;UUID g=new UUID(rj(),rj());a=oa;return g;}
void w(UUID uuid){if(vt<3)throw new RuntimeException("Guid not valid pre kdb+3.0");w(uuid.getMostSignificantBits());w(uuid.getLeastSignificantBits());}
long rj(){int x=ri(),y=ri();return a?x&0xffffffffL|(long)y<<32:(long)x<<32|y&0xffffffffL;} void w(long j){w((int)(j>>32));w((int)j);}
float re(){return Float.intBitsToFloat(ri());} void w(float e){w(Float.floatToIntBits(e));}
double rf(){return Double.longBitsToDouble(rj());} void w(double f){w(Double.doubleToLongBits(f));}
Month rm(){return new Month(ri());} void w(Month m){w(m.i);} Minute ru(){return new Minute(ri());} void w(Minute u){w(u.i);}
Second rv(){return new Second(ri());} void w(Second v){w(v.i);}Timespan rn(){return new Timespan(rj());} void w(Timespan n){if(vt<1)throw new RuntimeException("Timespan not valid pre kdb+2.6");w(n.j);}
public java.util.TimeZone tz=java.util.TimeZone.getDefault();
static long k=86400000L*10957,n=1000000000L;long o(long x){return tz.getOffset(x);}long lg(long x){return x+o(x);}long gl(long x){return x-o(x-o(x));}
Date rd(){int i=ri();return new Date(i==ni?nj:gl(k+86400000L*i));} void w(Date d){long j=d.getTime();w(j==nj?ni:(int)(lg(j)/86400000-10957));}
Time rt(){int i=ri();return new Time(i==ni?nj:gl(i));} void w(Time t){long j=t.getTime();w(j==nj?ni:(int)(lg(j)%86400000));}
//Timestamp
java.util.Date rz(){double f=rf();return new java.util.Date(Double.isNaN(f)?nj:gl(k+Math.round(8.64e7*f)));} void w(java.util.Date z){long j=z.getTime();w(j==nj?nf:(lg(j)-k)/8.64e7);}
Timestamp rp(){long j=rj(),d=j<0?(j+1)/n-1:j/n;Timestamp p=new Timestamp(j==nj?j:gl(k+1000*d));if(j!=nj)p.setNanos((int)(j-n*d));return p;}
void w(Timestamp p){long j=p.getTime();if(vt<1)throw new RuntimeException("Timestamp not valid pre kdb+2.6");w(j==nj?j:1000000*(lg(j)-k)+p.getNanos()%1000000);}
String rs()throws UnsupportedEncodingException{int i=j;for(;b[j++]!=0;);return (i==j-1)?"":new String(b,i,j-1-i,e);}void w(String s)throws UnsupportedEncodingException{int i=0,n;if(s!=null){n=ns(s);byte[]b=s.getBytes(e);for(;i99){if(t==100){rs();return r();}if(t<104)return b[j++]==0&&t==101?null:"func";if(t>105)r();else for(n=ri();i2000&&!l)z();o.write(B,0,J);}}
public void kr(Object x)throws IOException{if(sync==0)throw new IOException("Unexpected response msg");sync--;w(2,x);}
public void ke(String s)throws IOException{if(sync==0)throw new IOException("Unexpected error msg");sync--;int n=2+ns(s)+8;synchronized(o){B=new byte[n];B[0]=0;B[1]=2;J=4;w(n);w((byte)-128);w(s);o.write(B);}}
public void ks(String s)throws IOException{w(0,cs(s));}public void ks(Object x)throws IOException{w(0,x);} char[]cs(String s){return s.toCharArray();}
public void ks(String s,Object x)throws IOException{Object[]a={cs(s),x};w(0,a);}
public void ks(String s,Object x,Object y)throws IOException{Object[]a={cs(s),x,y};w(0,a);}
public void ks(String s,Object x,Object y,Object z)throws IOException{Object[]a={cs(s),x,y,z};w(0,a);}
public Object k()throws KException,IOException,UnsupportedEncodingException{synchronized(i){i.readFully(b=new byte[8]);a=b[0]==1;if(b[1]==1)sync++;boolean c=b[2]==1;j=4;i.readFully(b=new byte[ri()-8]);if(c)u();else j=0;if(b[0]==-128){j=1;throw new KException(rs());}return r();}}
public synchronized Object k(Object x)throws KException,IOException{w(1,x);return k();}
public Object k(String s)throws KException,IOException{return k(cs(s));}
public Object k(String s,Object x)throws KException,IOException{Object[]a={cs(s),x};return k(a);}
public Object k(String s,Object x,Object y)throws KException,IOException{Object[]a={cs(s),x,y};return k(a);}
public Object k(String s,Object x,Object y,Object z)throws KException,IOException{Object[]a={cs(s),x,y,z};return k(a);}
public static Object[]NULL={null,new Boolean(false),new UUID(0,0),null,new Byte((byte)0),new Short(Short.MIN_VALUE),new Integer(ni),new Long(nj),new Float(nf),new Double(nf),new Character(' '),"",
new Timestamp(nj),new Month(ni),new Date(nj),new java.util.Date(nj),new Timespan(nj),new Minute(ni),new Second(ni),new Time(nj)};
public static Object NULL(char c){return NULL[" bg xhijefcspmdznuvt".indexOf(c)];}
public static boolean qn(Object x){int t=-t(x);return(t==2||t>4)&&x.equals(NULL[t]);}
public static Object at(Object x,int i){return qn(x=Array.get(x,i))?null:x;}
public static void set(Object x,int i,Object y){Array.set(x,i,null==y?NULL[t(x)]:y);}
static int find(String[]x,String y){int i=0;for(;i0)O(t-u);}static String i2(int i){return new DecimalFormat("00").format(i);}static String i9(int i){return new DecimalFormat("000000000").format(i);}
}
//2016.05.25 check for null arg to w(String s)
//2016.05.24 added s.setTcpNoDelay(true)
//2016.04.27 added ssl/tls support
//2015.08.13 always setKeepAlive(true) for sockets
//2015.05.11 added z() to optionally compress outgoing data
//2013.12.19 qn did not detect null guid
//2013.05.01 added compareTo() to temporal classes, timespan.toString(), kr, ke
//2013.04.29 added hashCode() to temporal classes
//2013.04.22 added x instanceof UUID[]?:
//2012.05.29 for use with kdb+v3.0, changed handshake and added UUID. boolean v6->vt reflects type version
//2012.03.01 added equals() for Month,Minute,Second,Timespan. null checks in close().
// empty constructor c() and changed w(int,Object) to protected
//2012.02.09 close() if connect fails
//2012.01.06 read datetime, rz(), was truncating mS rather than rounding
//2010.10.06 block sending timestamp/timespan types to versions prior to kdb+2.6
//2010.05.06 optimized rs() for reading null symbols
//2010.03.20 changed datetime to java.util.Date as it was incompatible with timestamp
//2010.02.01 added unicode support for char vectors and symbol
//2010.01.06 fixed 0Np
//2009.12.07 removed v6 dependencies
//2009.12.02 uncommented at, set and qn
//2009.10.29 u - uncompress, connect retry for v<=2.5
//2009.09.23 Timestamp,Timespan,v6 connect
//2008.08.14 String(,,,"ISO-8859-1") to avoid mutex
//2007.10.18 tz
//2007.08.06 kx
//2007.04.20 sql.{Date|Time|Timestamp}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy