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