1 package javacli;
2 
3 /**
4  * Communication buffer used by Java Gigabase CLI to communicate with server
5  */
6 class ComBuffer {
packShort(byte[] buf, int offs, int val)7     static final int packShort(byte[] buf, int offs, int val) {
8 	 buf[offs++] = (byte)(val >> 8);
9 	 buf[offs++] = (byte)val;
10 	return offs;
11     }
packInt(byte[] buf, int offs, int val)12     static final int packInt(byte[] buf, int offs, int val) {
13 	 buf[offs++] = (byte)(val >> 24);
14 	 buf[offs++] = (byte)(val >> 16);
15 	 buf[offs++] = (byte)(val >> 8);
16 	 buf[offs++] = (byte)val;
17 	return offs;
18     }
packLong(byte[] buf, int offs, long val)19     static final int packLong(byte[] buf, int offs, long val) {
20         return packInt(buf, packInt(buf, offs, (int)(val >> 32)), (int)val);
21     }
packFloat(byte[] buf, int offs, float value)22     static final int packFloat(byte[] buf, int offs, float value) {
23 	return packInt(buf, offs, Float.floatToIntBits(value));
24     }
packDouble(byte[] buf, int offs, double value)25     static final int packDouble(byte[] buf, int offs, double value) {
26 	return packLong(buf, offs, Double.doubleToLongBits(value));
27     }
28 
unpackShort(byte[] buf, int offs)29     static final short unpackShort(byte[] buf, int offs) {
30         return (short)((buf[offs] << 8) + ( buf[offs+1] & 0xFF));
31     }
unpackInt(byte[] buf, int offs)32     static final int unpackInt(byte[] buf, int offs) {
33         return (buf[offs] << 24) + (( buf[offs+1] & 0xFF) << 16)
34             +  ((buf[offs+2] & 0xFF) << 8) + ( buf[offs+3] & 0xFF);
35     }
unpackLong(byte[] buf, int offs)36     static final long unpackLong(byte[] buf, int offs) {
37         return ((long)unpackInt(buf, offs) << 32)
38             + ((long)unpackInt(buf, offs+4) & 0xFFFFFFFFL);
39     }
unpackFloat(byte[] buf, int offs)40     static final float unpackFloat(byte[] buf, int offs) {
41         return Float.intBitsToFloat(unpackInt(buf, offs));
42     }
unpackDouble(byte[] buf, int offs)43     static final double unpackDouble(byte[] buf, int offs) {
44         return Double.longBitsToDouble(unpackLong(buf, offs));
45     }
46 
ComBuffer(int cmd, int id)47     ComBuffer(int cmd, int id) {
48         int size = 12;
49         buf = new byte[size];
50         pos = 0;
51         putInt(size);
52         putInt(cmd);
53         putInt(id);
54     }
55 
ComBuffer(int cmd)56     ComBuffer(int cmd) {
57         this(cmd, 0);
58     }
59 
reset(int size)60     void reset(int size) {
61         if (buf.length < size) {
62             buf = new byte[size];
63         }
64         pos = 0;
65     }
66 
67 
end()68     void end() {
69         packInt(buf, 0, pos);
70     }
71 
extend(int len)72     void extend(int len) {
73         if (pos + len > buf.length) {
74             int newLen = pos + len > len*2 ? pos + len : len*2;
75             byte[] newBuf = new byte[newLen];
76             System.arraycopy(buf, 0, newBuf, 0, buf.length);
77             buf = newBuf;
78         }
79     }
80 
putByte(int val)81     void putByte(int val) {
82         extend(1);
83         buf[pos++] = (byte)val;
84     }
85 
putShort(int val)86     void putShort(int val) {
87         extend(2);
88         pos = packShort(buf, pos, val);
89     }
90 
putInt(int val)91     void putInt(int val) {
92         extend(4);
93         pos = packInt(buf, pos, val);
94     }
95 
putLong(long val)96     void putLong(long val) {
97         extend(8);
98         pos = packLong(buf, pos, val);
99     }
100 
putDouble(double val)101     void putDouble(double val) {
102         extend(8);
103         pos = packDouble(buf, pos, val);
104     }
105 
putFloat(float val)106     void putFloat(float val) {
107         extend(4);
108         pos = packFloat(buf, pos, val);
109     }
110 
putRectangle(Rectangle r)111     void putRectangle(Rectangle r) {
112         extend(16);
113         pos = packInt(buf, pos, r.x0);
114         pos = packInt(buf, pos, r.y0);
115         pos = packInt(buf, pos, r.x1);
116         pos = packInt(buf, pos, r.y1);
117     }
118 
putAsciiz(String str)119     void putAsciiz(String str) {
120         if (str != null) {
121             byte[] bytes = str.getBytes();
122             int len = bytes.length;
123             int i, j;
124             extend(len+1);
125             byte[] dst = buf;
126             for (i = pos, j = 0; j < len; dst[i++] = bytes[j++]);
127             dst[i++] = (byte)'\0';
128             pos = i;
129         } else {
130             extend(1);
131             buf[pos++] = (byte)'\0';
132         }
133     }
134 
putByteArray(byte[] arr)135     void putByteArray(byte[] arr) {
136         int len = arr == null ? 0 : arr.length;
137         extend(len+4);
138         pos = packInt(buf, pos, len);
139         System.arraycopy(arr, 0, buf, pos, len);
140         pos += len;
141     }
putString(String str)142     void putString(String str) {
143         if (str == null) {
144             extend(5);
145             pos = packInt(buf, pos, 1);
146         } else {
147             byte[] bytes = str.getBytes();
148             int len = bytes.length;
149             extend(len+5);
150             pos = packInt(buf, pos, len+1);
151             System.arraycopy(bytes, 0, buf, pos, len);
152             pos += len;
153         }
154         buf[pos++] = (byte)'\0';
155     }
156 
getByte()157     byte getByte() {
158         return buf[pos++];
159     }
160 
getShort()161     short getShort() {
162         short value = unpackShort(buf, pos);
163         pos += 2;
164         return value;
165     }
166 
getInt()167     int getInt() {
168         int value = unpackInt(buf, pos);
169         pos += 4;
170         return value;
171     }
172 
getLong()173     long getLong() {
174         long value = unpackLong(buf, pos);
175         pos += 8;
176         return value;
177     }
178 
getFloat()179     float getFloat() {
180         float value = unpackFloat(buf, pos);
181         pos += 4;
182         return value;
183     }
184 
getDouble()185     double getDouble() {
186         double value = unpackDouble(buf, pos);
187         pos += 8;
188         return value;
189     }
190 
getAsciiz()191     String getAsciiz() {
192         byte[] p = buf;
193         int i = pos, j = i;
194         while (p[j++] != '\0');
195         pos = j;
196         return new String(p, i, j-i-1);
197     }
198 
getString()199     String getString() {
200         int len = getInt();
201         String value = new String(buf, pos, len-1);
202         pos += len;
203         return value;
204     }
205 
getRectangle()206     Rectangle getRectangle() {
207         Rectangle r = new Rectangle(unpackInt(buf, pos),
208                                     unpackInt(buf, pos+4),
209                                     unpackInt(buf, pos+8),
210                                     unpackInt(buf, pos+12));
211         pos += 16;
212         return r;
213     }
214 
215     byte buf[];
216     int  pos;
217 }
218