1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.server.protocol;
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;
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) {
32 buffer = ByteBuffer.allocate(20);
34 case DirectAllocation:
35 buffer = ByteBuffer.allocate(20);
39 DataBuffer(byte[] bytes, int size) {
40 buffer = ByteBuffer.allocate(size);
41 buffer.put(bytes, 0, size);
44 DataBuffer(byte[] bytes) {
45 buffer = ByteBuffer.wrap(bytes);
48 public DataBuffer(ByteBuffer byteBuffer) {
49 buffer = byteBuffer.duplicate();
50 buffer.order(byteBuffer.order());
52 public DataBuffer(ByteBuffer byteBuffer, int dummy) {
55 public ByteBuffer getByteBuffer() {
58 private void checkCapacity(int a) {
59 if (buffer.capacity() - buffer.position() >= a)
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);
68 buffer.position(position);
69 buffer.order(t.order());
74 public void order(ByteOrder byteOrder) {
75 buffer.order(byteOrder);
80 public void position(int position) {
81 buffer.position(position);
83 public byte[] getBytes() {
84 byte[] t = new byte[buffer.position()];
89 public short get(short a) {
90 return buffer.getShort();
92 public int get(int a) {
93 return buffer.getInt();
95 public void put(int a) {
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);
106 public void put(int[] a) {
109 checkCapacity(4 + 4*a.length);
111 for (int i=0; i<a.length; ++i)
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);
121 public void put(long[] a) {
122 checkCapacity(4 + 8*a.length);
124 for (int i=0; i<a.length; ++i)
125 buffer.putLong(a[i]);
127 public boolean get(boolean a) {
128 byte b = buffer.get();
131 public void put(boolean a) {
133 byte b = a ? (byte)0xff : (byte)0;
136 public byte get(byte a) {
139 public void put(byte a) {
143 public byte[] get(byte[] a) {
144 int size = buffer.getInt();
145 byte[] t = new byte[size];
146 buffer.get(t, 0, size);
149 public void put(byte[] a) {
150 checkCapacity(4 + a.length);
154 public ByteBuffer get(ByteBuffer a) {
155 int size = buffer.getInt();
156 byte[] t = new byte[size];
157 buffer.get(t, 0, size);
161 public void put(ByteBuffer a) {
162 byte[] t = a.array();
163 checkCapacity(4 + t.length);
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);
174 if(c >= 32 && c < 128)
179 out.print("(" + val + ")");
183 public String get(String a) {
186 CharsetDecoder decoder = UTF8.newDecoder();
187 ByteBuffer bbuf = ByteBuffer.wrap(t);
191 cbuf = decoder.decode(bbuf);
193 } catch (CharacterCodingException e) {
196 printChars(System.err, bbuf, 0);
200 .onMalformedInput(CodingErrorAction.REPLACE)
201 .onUnmappableCharacter(CodingErrorAction.REPLACE)
204 } catch (CharacterCodingException e1) {
205 throw new Error("not possible", e1);
210 public void put(String a) {
212 put(a.getBytes(UTF8.name()));
213 } catch (UnsupportedEncodingException e) {
214 throw new Error("UnsupportedEncoding: " + UTF8.name());
217 public long get(long a) {
218 return buffer.getLong();
220 public void put(long a) {
224 public float get(float f) {
225 return buffer.getFloat();
227 public void put(float a) {
231 public double get(double f) {
232 return buffer.getDouble();
234 public void put(double a) {
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]);
245 public void put(String[] a) {
248 for (int i=0; i<a.length; ++i)