c backend refact, implementing serilizer
[PxpRpc.git] / java / src / pursuer / pxprpc / Utils.java
blobe170330668cb37efbe2953fac06f589a959ab4ee
1 package pursuer.pxprpc;
3 import java.io.EOFException;
4 import java.io.IOException;
5 import java.nio.Buffer;
6 import java.nio.ByteBuffer;
7 import java.nio.ByteOrder;
8 import java.nio.channels.ByteChannel;
10 public class Utils {
11 // read enough data or EOFException
12 public static void readf(ByteChannel in,ByteBuffer b) throws IOException {
13 //java9 method signature change, which may cause error when runing on lower java version(usually happen on android).
14 Buffer b2=b;
15 for(b2.mark();b2.remaining()>0 && in.isOpen();) {
16 if(in.read(b)<0) {
17 throw new EOFException();
20 b2.reset();
23 public static int readInt32(ByteChannel in) throws IOException {
24 ByteBuffer b = ByteBuffer.allocate(4);
25 readf(in,b);
26 return b.order(ByteOrder.LITTLE_ENDIAN).getInt();
28 public static long readInt64(ByteChannel in) throws IOException {
29 ByteBuffer b = ByteBuffer.allocate(8);
30 readf(in,b);
31 return b.order(ByteOrder.LITTLE_ENDIAN).getLong();
33 public static float readFloat32(ByteChannel in) throws IOException {
34 ByteBuffer b = ByteBuffer.allocate(4);
35 readf(in,b);
36 return b.order(ByteOrder.LITTLE_ENDIAN).getFloat();
38 public static double readFloat64(ByteChannel in) throws IOException {
39 ByteBuffer b = ByteBuffer.allocate(8);
40 readf(in,b);
41 return b.order(ByteOrder.LITTLE_ENDIAN).getDouble();
44 public static void writef(ByteChannel out,ByteBuffer b) throws IOException {
45 Buffer b2=b;
46 for(b2.mark();
47 b2.remaining()>0 && out.isOpen();
48 out.write(b)) {}
49 b2.reset();
51 public static void writeInt32(ByteChannel out,int d) throws IOException {
52 ByteBuffer b=ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(d);
53 ((Buffer)b).flip();
54 writef(out,b);
56 public static void writeInt64(ByteChannel out,long d) throws IOException {
57 ByteBuffer b=ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN).putLong(d);
58 ((Buffer)b).flip();
59 writef(out,b);
61 public static void writeFloat32(ByteChannel out,float d) throws IOException {
62 ByteBuffer b=ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putFloat(d);
63 ((Buffer)b).flip();
64 writef(out,b);
66 public static void writeFloat64(ByteChannel out,double d) throws IOException {
67 ByteBuffer b=ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN).putDouble(d);
68 ((Buffer)b).flip();
69 writef(out,b);
71 public static ByteBuffer setPos(ByteBuffer bb,int pos){
72 Buffer b=bb;
73 b.position(pos);
74 return bb;
76 public static int getPos(ByteBuffer bb){
77 Buffer b=bb;
78 return b.position();
80 public static byte[] toBytes(ByteBuffer bb){
81 byte[] b2=new byte[bb.remaining()];
82 bb.get(b2);
83 return b2;
85 public static ByteBuffer ensureBuffer(ByteBuffer bb,int remainSize){
86 Buffer b=bb;
87 int size=remainSize+b.position();
88 if(b.capacity()<size){
89 ByteBuffer newbuf=ByteBuffer.allocate(size+(size>>1));
90 newbuf.put(newbuf.array(),0,b.position());
91 return newbuf;
93 return bb;
96 public static byte[] bytesGet(ByteBuffer bb,int off,int len) {
97 Buffer bb2=bb;
98 bb2.mark();
99 bb2.position(bb2.position()+off);
100 byte[] b = new byte[len];
101 bb.get(b);
102 bb2.reset();
103 return b;
105 public static void bytesSet(ByteBuffer bb,int off,int len,byte[] b) {
106 Buffer bb2=bb;
107 bb2.mark();
108 bb2.position(bb2.position()+off);
109 bb.put(b);
110 bb2.reset();
112 public static String stringJoin(String delim,Iterable<String> iter){
113 StringBuilder sb=new StringBuilder();
114 for(String e:iter) {
115 sb.append(e);
116 sb.append(delim);
118 return sb.substring(0,sb.length()-delim.length());