]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.server/src/org/simantics/db/server/protocol/DataBuffer.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.server / src / org / simantics / db / server / protocol / DataBuffer.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.server.protocol;\r
13 \r
14 import java.io.PrintStream;\r
15 import java.io.UnsupportedEncodingException;\r
16 import java.nio.ByteBuffer;\r
17 import java.nio.ByteOrder;\r
18 import java.nio.CharBuffer;\r
19 import java.nio.charset.CharacterCodingException;\r
20 import java.nio.charset.Charset;\r
21 import java.nio.charset.CharsetDecoder;\r
22 import java.nio.charset.CodingErrorAction;\r
23 \r
24 public class DataBuffer {\r
25     private static final Charset UTF8 = Charset.forName("UTF-8");\r
26     private boolean DEBUG = false;\r
27     private ByteBuffer buffer = null;\r
28     enum Allocation { JavaAllocation, DirectAllocation }\r
29     DataBuffer(Allocation a) {\r
30         switch (a) {\r
31         case JavaAllocation:\r
32              buffer = ByteBuffer.allocate(20);\r
33             break;\r
34         case DirectAllocation:\r
35             buffer = ByteBuffer.allocate(20);\r
36             break;\r
37         }\r
38     }\r
39     DataBuffer(byte[] bytes, int size) {\r
40         buffer = ByteBuffer.allocate(size);\r
41         buffer.put(bytes, 0, size);\r
42         buffer.rewind();\r
43     }\r
44     DataBuffer(byte[] bytes) {\r
45         buffer = ByteBuffer.wrap(bytes);\r
46         buffer.rewind();\r
47     }\r
48     public DataBuffer(ByteBuffer byteBuffer) {\r
49         buffer = byteBuffer.duplicate();\r
50         buffer.order(byteBuffer.order());\r
51     }\r
52     public DataBuffer(ByteBuffer byteBuffer, int dummy) {\r
53         buffer = byteBuffer;\r
54     }\r
55     public ByteBuffer getByteBuffer() {\r
56         return buffer;\r
57     }\r
58     private void checkCapacity(int a) {\r
59         if (buffer.capacity() - buffer.position() >= a)\r
60             return;\r
61         ByteBuffer t = buffer;\r
62         int capacity = t.capacity();\r
63         int position = t.position();\r
64         int newCapacity = capacity + Math.max(capacity/2, a);\r
65         buffer = ByteBuffer.allocate(newCapacity);\r
66         t.clear();\r
67         buffer.put(t);\r
68         buffer.position(position);\r
69         buffer.order(t.order());\r
70     }\r
71     public void clear() {\r
72         buffer.clear();\r
73     }\r
74     public void order(ByteOrder byteOrder) {\r
75         buffer.order(byteOrder);\r
76     }\r
77     public void mark() {\r
78         buffer.mark();\r
79     }\r
80     public void position(int position) {\r
81         buffer.position(position);\r
82     }\r
83     public byte[] getBytes() {\r
84         byte[] t = new byte[buffer.position()];\r
85         buffer.clear();\r
86         buffer.get(t);\r
87         return t;\r
88     }\r
89     public short get(short a) {\r
90         return buffer.getShort();\r
91     }\r
92     public int get(int a) {\r
93         return buffer.getInt();\r
94     }\r
95     public void put(int a) {\r
96         checkCapacity(4);\r
97         buffer.putInt(a);\r
98     }\r
99     public int[] get(int[] a) {\r
100         int size = buffer.getInt();\r
101         int[] t = new int[size];\r
102         buffer.asIntBuffer().get(t);\r
103         buffer.position(buffer.position() + size * 4);\r
104         return t;\r
105     }\r
106     public void put(int[] a) {\r
107         if (null == a)\r
108             a = new int[0];\r
109         checkCapacity(4 + 4*a.length);\r
110         this.put(a.length);\r
111         for (int i=0; i<a.length; ++i)\r
112             buffer.putInt(a[i]);\r
113     }\r
114     public long[] get(long[] a) {\r
115         int size = buffer.getInt();\r
116         long[] t = new long[size];\r
117         buffer.asLongBuffer().get(t, 0, size);\r
118         buffer.position(buffer.position() + 8 * size);\r
119         return t;\r
120     }\r
121     public void put(long[] a) {\r
122         checkCapacity(4 + 8*a.length);\r
123         this.put(a.length);\r
124         for (int i=0; i<a.length; ++i)\r
125             buffer.putLong(a[i]);\r
126     }\r
127     public boolean get(boolean a) {\r
128         byte b = buffer.get();\r
129         return !(0 == b);\r
130     }\r
131     public void put(boolean a) {\r
132         checkCapacity(1);\r
133         byte b = a ? (byte)0xff : (byte)0;\r
134         buffer.put(b);\r
135     }\r
136     public byte get(byte a) {\r
137         return buffer.get();\r
138     }\r
139     public void put(byte a) {\r
140         checkCapacity(1);\r
141         buffer.put(a);\r
142     }\r
143     public byte[] get(byte[] a) {\r
144         int size = buffer.getInt();\r
145         byte[] t = new byte[size];\r
146         buffer.get(t, 0, size);\r
147         return t;\r
148     }\r
149     public void put(byte[] a) {\r
150         checkCapacity(4 + a.length);\r
151         this.put(a.length);\r
152         buffer.put(a);\r
153     }\r
154     public ByteBuffer get(ByteBuffer a) {\r
155         int size = buffer.getInt();\r
156         byte[] t = new byte[size];\r
157         buffer.get(t, 0, size);\r
158         a.put(t);\r
159         return a;\r
160     }\r
161     public void put(ByteBuffer a) {\r
162         byte[] t = a.array();\r
163         checkCapacity(4 + t.length);\r
164         this.put(t.length);\r
165         buffer.put(t);\r
166     }\r
167     public static void printChars(PrintStream out, ByteBuffer buf, int pos) {\r
168         out.print("[" + buf.limit() + "]");\r
169         for(int i=pos;i<buf.limit();++i) {\r
170             int val = (int)buf.get(i);\r
171             if(val < 0)\r
172                 val += 256;\r
173             char c = (char)val;\r
174             if(c >= 32 && c < 128)\r
175                 out.print(c);\r
176             else if(c==0)\r
177                 out.print('\u00A4');\r
178             else\r
179                 out.print("(" + val + ")");\r
180         }\r
181         out.println();\r
182     }\r
183     public String get(String a) {\r
184         byte[] t = null;\r
185         t = this.get(t);\r
186         CharsetDecoder decoder = UTF8.newDecoder();\r
187         ByteBuffer bbuf = ByteBuffer.wrap(t);\r
188         CharBuffer cbuf;\r
189         String s = null;\r
190         try {\r
191             cbuf = decoder.decode(bbuf);\r
192             s = cbuf.toString();\r
193         } catch (CharacterCodingException e) {\r
194             bbuf.rewind();\r
195             if (DEBUG)\r
196                 printChars(System.err, bbuf, 0);\r
197             try {\r
198                 cbuf = UTF8\r
199                 .newDecoder()\r
200                 .onMalformedInput(CodingErrorAction.REPLACE)\r
201                 .onUnmappableCharacter(CodingErrorAction.REPLACE)\r
202                 .decode(bbuf);\r
203                 s = cbuf.toString();\r
204             } catch (CharacterCodingException e1) {\r
205                 throw new Error("not possible", e1);\r
206             }\r
207         }\r
208         return s;\r
209     }\r
210     public void put(String a) {\r
211         try {\r
212             put(a.getBytes(UTF8.name()));\r
213         } catch (UnsupportedEncodingException e) {\r
214             throw new Error("UnsupportedEncoding: " + UTF8.name());\r
215         }\r
216     }\r
217     public long get(long a) {\r
218         return buffer.getLong();\r
219     }\r
220     public void put(long a) {\r
221         checkCapacity(8);\r
222         buffer.putLong(a);\r
223     }\r
224     public float get(float f) {\r
225         return buffer.getFloat();\r
226     }\r
227     public void put(float a) {\r
228         checkCapacity(4);\r
229         buffer.putFloat(a);\r
230     }\r
231     public double get(double f) {\r
232         return buffer.getDouble();\r
233     }\r
234     public void put(double a) {\r
235         checkCapacity(8);\r
236         buffer.putDouble(a);\r
237     }\r
238     public String[] get(String[] a) {\r
239         int size = buffer.getInt();\r
240         String[] t = new String[size];\r
241         for (int i=0; i<size; ++i)\r
242             t[i] = this.get(t[i]);\r
243         return t;\r
244     }\r
245     public void put(String[] a) {\r
246         checkCapacity(4);\r
247         this.put(a.length);\r
248         for (int i=0; i<a.length; ++i)\r
249             this.put(a[i]);\r
250     }\r
251 }\r